On this page
Ownership, Borrowing, Lifetimes & RAII
Prev: ownership_and_internals | Up: ownership_and_internals | Next: Traits, Generics & Dispatch
Rel: Memory Management Models, Call Stack & Heap, Drop, Copy
Ref: The Rust Book โ Understanding Ownership
Tags: #public #prog #rust #internals #ownership #memory
Contents
- > The Three Ownership Rules & Move Semantics
- > The Borrow Checker (`&T` vs. `&mut T`)
- > Lifetimes (`'a`) & Elision Rules
- > Deterministic `Drop` (RAII) Without a Garbage Collector
The Three Ownership Rules & Move Semantics
- Every value in Rust has a single owner variable.
- Assigning or passing a non-
Copyvalue (String,Vec,File) moves ownership โ the previous binding is invalidated at compile time (preventing double-free). - When the owner goes out of scope, the compiler automatically inserts a
Drop::dropcall (RAII).
let s1 = String::from("hello");
let s2 = s1; // Ownership moves to s2; using s1 afterwards is a compile-time error
println!("{s2}");
The Borrow Checker (&T vs. &mut T)
At any point in a scope, for a given value you may hold either:
- Any number of shared immutable references (
&T), OR - Exactly one exclusive mutable reference (
&mut T) โ never both simultaneously.
This single compile-time aliasing XOR mutability invariant eliminates data races, iterator invalidation, and dangling pointers across both single-threaded and multi-threaded code.
Lifetimes ('a) & Elision Rules
Lifetimes ('a) are generic parameters telling the borrow checker how long a returned reference remains valid relative to input references. They are erased at compile time (zero runtime cost):
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
if x.len() >= y.len() { x } else { y }
}
When there is one input reference (or &self), Rust's lifetime elision rules infer 'a automatically.
Deterministic Drop (RAII) Without a Garbage Collector
Local variables are dropped in reverse declaration order at scope exit (or early via std::mem::drop(val)), releasing heap buffers, closing File handles, and unlocking MutexGuards deterministically.
Linked from
- tuts/programming/memory_and_execution/memory_management_models
- tuts/rust/builtins
- tuts/rust/builtins/Clone
- tuts/rust/builtins/Copy
- tuts/rust/builtins/Drop
- tuts/rust/builtins/From
- tuts/rust/builtins/Iterator
- tuts/rust/builtins/macro_rules
- tuts/rust/builtins/match
- tuts/rust/builtins/Option
- tuts/rust/builtins/Result
- tuts/rust/builtins/Send
- tuts/rust/builtins/str
- tuts/rust/builtins/String
- tuts/rust/builtins/Sync
- tuts/rust/builtins/try_macro
- tuts/rust/builtins/unsafe
- tuts/rust/builtins/Vec
- tuts/rust/ownership_and_internals
- tuts/rust/ownership_and_internals/ownership_borrowing_and_lifetimes
- tuts/rust/ownership_and_internals/traits_generics_and_dispatch
- tuts/rust/tutorials/zero_copy_parsing_with_lifetimes
Related notes
- tuts/programming/memory_and_execution/memory_management_models Linked here; links to this page; shared tags: #memory, #ownership, #prog
- tuts/rust/ownership_and_internals Linked here; links to this page; shared tags: #internals, #prog, #rust
- tuts/rust/ownership_and_internals/traits_generics_and_dispatch Linked here; links to this page; shared tags: #internals, #prog, #rust
- tuts/rust/builtins/Copy Linked here; links to this page; shared tags: #prog, #rust
- tuts/rust/builtins/Drop Linked here; links to this page; shared tags: #prog, #rust
- tuts/programming/memory_and_execution/call_stack_and_heap Linked here; shared tags: #memory, #prog
- tuts/rust/builtins links to this page; shared tags: #prog, #rust
- tuts/rust/builtins/Clone links to this page; shared tags: #prog, #rust
- tuts/rust/builtins/From links to this page; shared tags: #prog, #rust
- tuts/rust/builtins/Iterator links to this page; shared tags: #prog, #rust