Introduction to the borrow checker

The most important aspect of Rust is the ownership and borrowing model. Based on the strict enforcing of borrowing rules, the compiler can guarantee memory safety without an external garbage collector. This is done by the borrow checker, a subsystem of the compiler. By definition, every resource created has a lifetime and an owner associated with it, which operates under the following rules:

  • Each resource has exactly one owner at any point in time. By default, the owner is the variable that created that resource, and its lifetime is the lifetime of the enclosing scope. Others can borrow or copy the resource if they need to. Note that a resource can be anything from a variable or a function. A function takes ...

Get Network Programming with Rust now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.