Master Rust through interactive, visual learning
Rust's most unique feature — understanding who owns what
fn main() {
let s1 = String::from("hello");
let s2 = s1; // s1 moved to s2
// println!("{}", s1); // ERROR!
println!("{}", s2); // OK
}When s1 is assigned to s2, ownership moves. Only one owner at a time.
References let you use data without taking ownership
fn main() {
let mut s = String::from("hello");
// Multiple immutable borrows: OK
let r1 = &s;
let r2 = &s;
println!("{}, {}", r1, r2);
// Mutable borrow: exclusive
let r3 = &mut s;
r3.push_str(", world");
println!("{}", r3);
}Ensuring references are always valid
fn longest<'a>(
x: &'a str,
y: &'a str,
) -> &'a str {
if x.len() > y.len() { x }
else { y }
}
fn main() {
let s1 = String::from("long string");
let result;
{
let s2 = String::from("xyz");
result = longest(&s1, &s2);
println!("{}", result);
}
}High-level code, low-level performance
Relative execution time (lower is faster)
Start your Rust journey today. Zero-cost abstractions, memory safety without garbage collection, and fearless concurrency await.