Ownership and Borrowing

Ownership is, in the words of the Rust book, “Rust’s most unique feature, with deep implications for the rest of the language.” It is the mechanism that “enables Rust to make memory safety guarantees without needing a garbage collector,” shifting the work of preventing memory bugs from runtime to compile time.

Under the ownership model, each value in a Rust program has a single owner, and the compiler tracks when that value goes out of scope so its memory can be freed deterministically. Rather than relying on a runtime collector to decide when memory is no longer needed, the rules are checked statically as the program is compiled.

Borrowing is the companion mechanism: code can temporarily access a value through a reference, or “borrow” it, instead of taking ownership. The Rust book introduces borrowing alongside ownership and slices as the core ideas of this chapter, and the component of the compiler that enforces the borrowing rules is known as the borrow checker.

Together, ownership and borrowing let Rust deliver on the promise stated on its official site: that “Rust’s rich type system and ownership model guarantee memory-safety and thread-safety — enabling you to eliminate many classes of bugs at compile-time,” all without the overhead of a garbage collector.