23.03 Ownership and Borrowing
Ownership and Borrowing
Overview
Ownership is a key feature of Rust that ensures memory safety without a garbage collector.
Topics
- Ownership Rules
- Borrowing (immutable and mutable)
- Slices
Examples
let s1 = String::from("hello");
let s2 = s1; // Ownership moved
// println!("{}", s1); // Error: s1 is no longer valid
let s3 = String::from("world");
let s4 = &s3; // Immutable borrow
println!("{}", s4);
let mut s5 = String::from("mutable");
let s6 = &mut s5; // Mutable borrow
s6.push_str(" string");