Rust Programming Cookbook
上QQ阅读APP看书,第一时间看更新

Shared ownership

Ownership and borrowing are fundamental concepts in Rust; they are the reason no runtime garbage collection is required. As a quick primer: how do they work? In short: scopes. Rust (and many other languages) use (nested) scopes to determine the validity of a variable, so it cannot be used outside of the scope (like a function). In Rust, these scopes own their variables, so they will be gone after the scope finishes. In order for the program to move around values, it can transfer ownership to a nested scope or return it to the parent scope. 

For temporary transfers (and multiple viewers), Rust has borrowing, which creates a reference back to the owned value. However, these references are less powerful, and sometimes more complex to maintain (for example, can the reference outlive the original value?), and they are probably the reason why the compiler complains.

In this recipe, we are getting around this problem by sharing ownership using a reference counter that only drops the variable after the counter reaches zero.