Skip to content

Rust Ownership

This sample shows a plain explanatory note without executable cells. Use this shape when a topic needs concepts, short code snippets, and takeaways more than runtime controls.

  • A focused concept page with no runtime dependency.
  • Short code blocks placed next to the explanation they support.
  • A final summary that gives readers the key model to remember.

In Rust, every value has an owner. When the owner goes out of scope, the value is dropped. This rule lets Rust manage memory without a garbage collector.

fn main() {
let name = String::from("Rust");
println!("{name}");
}

name owns the String inside the scope of main. When main ends, the String is dropped.

Assigning a non-copy value to another variable moves ownership. The original variable cannot be used after the move.

fn main() {
let name = String::from("Rust");
let moved = name;
println!("{moved}");
}

String owns heap data, so Rust does not create an implicit deep copy. After let moved = name;, moved is the owner.

Pass a reference when a function should read a value without taking ownership.

fn len(value: &str) -> usize {
value.len()
}
fn main() {
let name = String::from("Rust");
println!("{}", len(&name));
println!("{name}");
}

With a reference, the function reads the value but does not own it. The caller can still use the value after the function call.

Ownership, moves, and borrowing are core rules behind Rust’s safety model. Prose-first pages work well when they move from concept, to short example, to takeaway.