Rust Borrow Checker
2026-11-18 · garbage collection
notes / DOCS
- compile time ownership tracking
fn main() {
let a = String::from("hello");
let b = a; // a is unuseable now
println!("{}", a); // err
}
❯ cargo run
Compiling new v0.1.0 (/home/subzcuber/new)
error[E0382]: borrow of moved value: `a`
--> src/main.rs:4:20
|
2 | let a = String::from("hello");
| - move occurs because `a` has type `String`, which does not implement the `Copy` trait
3 | let b = a;
| - value moved here
4 | println!("{}", a);
| ^ value borrowed here after move
|
help: consider cloning the value if the performance cost is acceptable
|
3 | let b = a.clone();
| ++++++++
warning: unused variable: `b`
--> src/main.rs:3:9
|
3 | let b = a;
| ^ help: if this is intentional, prefix it with an underscore: `_b`
|
= note: `#[warn(unused_variables)]` (part of `#[warn(unused)]`) on by default
Rule 1: Every Toy has one owner
an owner is a variable binding that is responsible for the value's memory
Rule 2: Give it away
ownership can be moved
let mine = toy;
let friends = mine;
// mine is gone and does not affect the memory of "toy" anymore
Rule 3: Toys are tidied up instantly
When the owner goes out of scope the value is dropped
fn main() {
{
let s = String::from("hello");
// s is valid here
} // Rust calls drop(s)
// s is no longer valid here
}
drop() is rust's equivalent of a destructor
Rule 4: Borrowing
shared and mutable references
multiple shared references allowed
fn print_length(s: &String) {
println!("length = {}", s.len());
}
fn main() {
let s = String::from("hello");
let r1 = &s;
let r2 = &s;
print_length(r1);
print_length(r2);
// Both r1 and r2 are valid. s is still the owner.
}
only one mutable reference allowed, no shared reference can coexist
// allowed
fn main() {
let mut s = String::from("hwllo");
let r = &mut s; // single mutable reference
r.push_str(", world");
println!("{}", r);
}
// ERROR: now allowed
fn main() {
let mut s = String::from("hello");
let r1 = &mut s;
let r2 = &s;
println("{}", r1);
}
❯ cargo run
Compiling new v0.1.0 (/home/subzcuber/new)
error[E0502]: cannot borrow `s` as immutable because it is also borrowed as mutable
--> src/main.rs:5:15
|
4 | let r1 = &mut s;
| ------ mutable borrow occurs here
5 | let r2 = &s;
| ^^ immutable borrow occurs here
6 |
7 | println!("{}", r1);
| -- mutable borrow later used here
- compile time reader-writer lock
Rule 5: Sharing
Rust provides std::rc::Rc (reference count) to provide shared ownership
for example in trees
A
/ \
B C
\ /
D
both B and C must "own" the same subtree, borrowing it at runtime as and when required (immutable borrow only (shared ref))
use std::rc::Rc;
struct Node {
value: i32,
child: Option<Rc<Node>>,
}
fn main() {
let d = Rc::new(Node {
value: 4,
child: None,
});
let b = Node {
value: 2,
child: Some(Rc::clone(&d)),
};
let c = Node {
value: 3,
child: Some(Rc::clone(&d)),
};
println!(" {} {} ", b.value, c.value);
println!(" \\ / ",);
println!(" {}", b.child.as_ref().unwrap().value);
}
2 3
\ /
4
it is still immutable ownership though
theres a lot of other stuff, rust is fucking weird
- borrow checker and lifetimes etc
- entirely too many linked lists in rust (i recommend v nice)