JOURNAL

Notes on Rust's Ownership System

2026.08.022 MIN READ

Notes on Rust's Ownership System

Three weeks into learning Rust, I finally understood ownership — it is essentially a memory management discipline enforced at compile time.

Three Core Rules

  1. Every value in Rust has a single owner
  2. Only one owner exists at any given time
  3. When the owner goes out of scope, the value is dropped

Rule three is the biggest difference from GC languages: there is no garbage collector sweeping in the background. Deallocation happens deterministically at the end of a scope — predictable, fast, pause-free.

Borrowing: Take It on Loan

— 赞助商 · Sponsor —

If a function only needs to read data, it doesn't take ownership — it borrows:

fn count_chars(text: &String) -> usize {
    text.chars().count()
}

fn main() {
    let msg = String::from("florianio");
    let n = count_chars(&msg); // borrowed; ownership stays in main
    println!("{} has {} chars", msg, n); // still usable
}

The beauty of the borrowing rules: unlimited immutable borrows, but only one mutable borrow at a time. This eliminates data races entirely — not with locks, but by making the race impossible at the type level.

Lifetimes: The Map of Borrow Relationships

Lifetime annotations like 'a don't make code live longer; they tell the compiler how long borrow relationships stay valid:

fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
    if x.len() > y.len() { x } else { y }
}

From experience: you rarely write lifetimes by hand. Let the compiler's elision rules guide you, and follow the error messages — they're better teachers than the reference book.

Suggested Learning Path

  • Week 1: ownership, borrowing, slices (fully understand &str vs String)
  • Week 2: enums & pattern matching, error handling (Result has one of the best error models in any language)
  • Week 3: generics and traits — start replacing daily scripts with small Rust tools

Wrap-up

Rust's learning curve is steep for real, but not because of syntax. It demands you think through the ownership of every piece of memory before you write it. Once you do, the problems that "explode at runtime" in other languages simply don't compile here. That peace of mind is worth the climb.

— 赞助商 · Sponsor —