Lifetime annotations
Lifetimes name how long references live:
fn first<'a>(s: &'a str) -> &'a str {
&s[0..1]
}
The compiler checks that references never dangle. If a borrow could be invalid, compilation fails.
Why it works: this is what makes Rust safe without a garbage collector.
Smart pointers
Smart pointers own their data:
| Type | Behavior |
|---|---|
Box<T> | heap ownership |
Rc<T> | reference counting |
Arc<T> | atomic counting (safe across threads) |
RefCell<T> | interior mutability |
Each trades off safety and control. Box is the simplest and most common.