Learn the fundamentals of ownership, memory safety, and performance in Rust 2021 edition.
Master the principles that make Rust a systems programming language of the future.
Rust's ownership model ensures memory safety without a garbage collector, with rules enforced at compile time.
fn main() { let s1 = String::from("hello"); let s2 = s1; // s1 is no longer valid println!("{}", s2); }
The borrow checker enforces reference validity rules to prevent dangling pointers and data races.
fn main() { let mut s = String::from("hello"); let s2 = &s; // immutable borrow // s.push_str("!"); // error: cannot borrow `s` as mutable because it is also borrowed as immutable println!("{}", s2); }
Rust ensures memory safety without garbage collection through compile-time correctness guarantees and zero-cost abstractions.
Rust combines the low-level control of C++ with the safety guarantees of modern languages. It's perfect for systems programming, web assembly, and embedded systems.
Next Section →Take your Rust knowledge to the next level with pattern matching, lifetimes, and macros.
Rust's powerful pattern matching system allows you to destructure complex data types with precise control flow.
fn main() { let x = 5; match x { 1 => println!("one"), 2 => println!("two"), 3 => println!("three"), 4 => println!("four"), 5 => println!("five"), _ => println!("other"), } }
Explicit lifetime annotations allow precise control over how long references to data are valid.
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str { if x.len() > y.len() { x } else { y } } fn main() { let string1 = String::from("long string is long"); let string2 = "abc"; let result = longest(&string1, &string2); println!("The longest string is {}", result); }
Rust's macro system enables powerful metaprogramming capabilities with hygienic macro expansion.
macro_rules! create_function { ($func_name:ident) => { fn $func_name() { println!("Hello from {}!", stringify!($func_name)); } }; } create_function!(hello); // creates function hello() fn main() { hello(); }
The official Rust documentation covering all aspects of the language with examples and exercises.
Read DocsInteractive tutorial with 150+ working code examples to explore in your browser.
Explore ExamplesCollection of hands-on exercises to teach you Rust fundamentals through practice.
Practice Now