Back to course

Ownership: The Heart of Rust

Rust for Systems & Web3 Security

The Three Rules of Ownership

Ownership is Rust's unique way of managing memory without a garbage collector.

  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 (freed from memory).

Move Semantics

When you assign a complex type (like String) to another variable, the data isn't copied. It's moved.

rust let s1 = String::from("hello"); let s2 = s1; // s1 is no longer valid here! // println!("{}", s1); // Error!

This prevents 'double free' errors, a common security vulnerability in C++.