Rust: Memory Safety through Ownership

Rust combines C/C++ performance with memory safety — without a garbage collector. The core idea is the ownership model.

Ownership rules

  • Every value has exactly one owner.
  • When the owner goes out of scope, the value is freed.
  • Borrowing is allowed: either one mutable reference or many immutable ones.

Example

fn main() {
    let s = String::from("Hello");
    let len = calculate(&s);  // borrow
    println!("{}: {}", s, len);
}

fn calculate(s: &String) -> usize { s.len() }

Why it matters

Many security bugs (buffer overflows, use-after-free) exist in C/C++ because memory is managed manually. Rust catches these at compile time.

Use cases

  • Systems programming (kernel modules, drivers).
  • WebAssembly.
  • High-performance web services (Axum, Actix).

See also: Programming.