Skip to content

Chapter 1 of 8

Foundations of Rust Memory Safety

Rust is a systems programming language that targets three goals at once: safety, speed, and concurrency. What distinguishes it from most peers is that it achieves memory safety without a garbage collector, instead relying on a compile-time ownership system that the borrow checker enforces on every program.

The ownership model rests on three rules. Every value in Rust has exactly one owner, there can only be one owner at a time, and when that owner goes out of scope the value is dropped. Because these rules are checked statically, entire classes of bugs — use-after-free, double-free, and many memory leaks — become impossible rather than merely unlikely.

When code needs to access a value without taking ownership of it, it borrows the value through a reference. An immutable reference, written as `&T`, is a shared borrow: many immutable references may coexist freely. A mutable reference, written as `&mut T`, is an exclusive borrow: at most one such reference may exist, and no immutable references may exist alongside it. The compiler verifies these aliasing rules on every function, which means the same checks that prevent data races in single-threaded code also prevent them across threads.

Lifetimes are the annotations that describe how long references remain valid. A lifetime parameter such as `'a` ties a reference's validity to a particular scope, and the compiler uses these annotations to prevent dangling references. The `'static` lifetime is a special case that means the reference is valid for the entire duration of the program, which is precisely why string literals have the type `&'static str`. Together, ownership, borrowing, and lifetimes form the foundation that makes Rust's memory safety guarantees possible.

All chapters
  1. 1Foundations of Rust Memory Safety
  2. 2Structures, Enums, and Pattern Matching
  3. 3Error Handling with Option and Result
  4. 4Traits, Generics, and Polymorphism
  5. 5Iterators, Closures, and Functional Patterns
  6. 6Modules, Crates, Cargo, and Testing
  7. 7Concurrency in Rust
  8. 8Smart Pointers and Unsafe Rust

Drill it

Reading is not remembering. These come from the Rust Programming deck:

Q

What is Rust?

Rust is a systems programming language focused on safety, speed, and concurrency. It achieves memory safety without a garbage collector through its ownership sy...

Q

What is ownership in Rust?

Ownership is Rust's core memory management concept. Each value has exactly one owner, and when the owner goes out of scope, the value is dropped (freed). This p...

Q

What are the three ownership rules in Rust?

Each value in Rust has a single ownerThere can only be one owner at a timeWhen the owner goes out of scope, the value is dropped

Q

What is borrowing in Rust?

Borrowing lets you reference a value without taking ownership. You create a reference with &. The original owner retains ownership while the borrower can re...