Rust High Performance
上QQ阅读APP看书,第一时间看更新

Mastering the borrow checker

To ensure memory and thread safety, Rust's borrow checker has three simple rules. They are enforced all through the code except in unsafe scopes. Here they are:

  • Each binding will have an owner
  • There can only be one owner for a binding
  • When the owner goes out of the scope, the binding gets dropped

These three rules seem simple enough, but they have a great implication on how we code. The compiler can know beforehand when an owner goes out of scope, so it will always know when to drop/destruct a binding/variable. This means that you can write your code without having to think about where you create variables, where you call destructors, or whether you have already called a destructor or you are calling it twice.

Of course, this comes with an additional learning curve that can sometimes be difficult to catch up. The second rule is what most people find difficult to manage. Since there can only be one owner at a time, sharing information sometimes becomes somewhat difficult.

Let's see an example of this behavior with a known type, the Vec type:

    let mut my_vector = vec![0, 16, 34, 13, 95];
my_vector.push(22);
println!("{:?}", my_vector);

This will print the following:

At the end of the current scope (the main() function, for example), the vector will be dropped by calling its destructor. In this case, it will simply deallocate the memory cleanly and then destroy itself.