← Back to Notes

Learning Rust: Ownership

Ownership is Rust's most unique feature. It enables memory safety without a garbage collector.

The Rules

  1. Each value in Rust has a variable that's called its owner.
  2. There can only be one owner at a time.
  3. When the owner goes out of scope, the value will be dropped.
{
    let s = String::from("hello"); // s is valid from this point forward
    // do stuff with s
}                                  // this scope is over, and s is no longer valid