in ,

Rust Ownership Rules, Hacker News

If you have been following this blog, then it would have been obvious that at the beginning of this year, I started learning Rust. This blogpost is a breakaway from the journal style of capturing the main points I encountered while reading the Rust book. It instead captures my understanding thus far of Rust ownership rules.

One of Rust’s main differentiator is that it provides memory safety. This it does by providing compile-time guarantees that flag code that potentially could lead to memory bugs as a compile-time error. The compile-time guarantees enforce what is normally referred to as ownership rules. In this post, I took the opportunity to re-summarise what I consider to be the essence of this ownership rules in Rust. The key points can be outlined as follows:

  • Values ​​are owned by variables.
  • When the owning variables go out of scope, the memory the value is occupying will be deallocated.
  • The values ​​can be used by other variables, they just need to adhere to certain rules that are enforced by the compiler.

The ways that other variables make use of value can be grouped into 4 categories and these ways of usage will dictate the rules to be adhered to: