What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Rust ownership determines who is responsible for a value; borrowing lets code use a value without taking it over. If you know Ruby, assignment and method calls will look familiar, but Rust adds rules the compiler checks: a non-Copy value can have only one owner at a time, references must remain valid, and a mutable borrow requires exclusive access while it is active.

Start with familiar assignment, then notice what Rust adds

In Ruby, assignment is a familiar way to bind a name to an object. Ruby’s assignment documentation and Object documentation provide useful context for Ruby syntax, but they do not describe Rust’s ownership system. In Rust, the compiler checks who owns a value and whether references to it are valid.

Rust’s official rule is straightforward: each value has one owner at a time, and the value is dropped when its owner goes out of scope. Those rules let Rust manage cleanup without a garbage collector. The official ownership chapter introduces the model, and its detailed ownership section explains how it applies to values such as String.

What happens when a Rust value is assigned?

Consider a heap-owning String:

let s1 = String::from("hello");
let s2 = s1;

This does not automatically make a deep copy of the string. Ownership moves from s1 to s2, so s2 is now the usable binding and using s1 afterward is rejected by the compiler. When s2 leaves scope, Rust drops the string.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

If you genuinely need a separate copy of the heap data, request one explicitly with clone():

let s1 = String::from("hello");
let s2 = s1.clone();

Cloning duplicates the string’s data and can cost time and memory. Prefer a move when the old binding is no longer needed; clone when two independently usable values are required.

What does borrowing mean?

A reference lets a function use a value without becoming its owner. The official Rust book puts it simply: “We call the action of creating a reference borrowing.” (The Rust Programming Language, “References and Borrowing”.)

For example, a function can read a string through an immutable reference:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
fn calculate_length(s: &String) -> usize {
    s.len()
}

fn main() {
    let text = String::from("hello");
    let length = calculate_length(&text);
    println!("{text} has {length} characters");
}

The &text argument borrows text. The function can inspect the string, but it does not take ownership, so the caller can use text after the call. This avoids the need to return the original value just to make it available to the caller again.

Choose between ownership and a reference

Decide what access the function needs, not just what syntax is shortest:

  • Pass an owned value when the function should take responsibility for it or consume it. For a non-Copy value, passing it by value generally moves ownership.
  • Use &T when the function only needs to read the value temporarily. The caller retains ownership, and multiple immutable borrows can coexist.
  • Use &mut T when the function must mutate the value. Rust requires exclusive access while that mutable borrow is active.

The right choice depends on whether the callee needs to keep the value, whether it only reads or must mutate, whether other code needs access at the same time, and how long a reference needs to remain valid.

Why Rust restricts mutable borrowing

An immutable reference, &T, permits reading but not mutation through that reference. Several immutable references to the same value may be active at once. A mutable reference, &mut T, permits mutation, but while it is active, other references to that value cannot be used. In practical terms, Rust enforces “many readers or one writer at a time.”

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

This exclusivity prevents conflicting access from being accepted, including patterns that could cause data races. The rule is based on when references are actually used: a borrow can end after its last use, even if the surrounding lexical block has not closed. It is therefore possible to borrow a value mutably later in a block once earlier references are no longer in use.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

References must not outlive their owners

A reference is not an owner, so its validity depends on the value it points to. Rust rejects a reference that could be used after its value has gone out of scope. As the official book states, “References must always be valid.”

For example, a function cannot return a reference to a local String that is dropped when the function exits. The caller would receive a reference to a value that no longer exists. When a function needs to return data it created locally, returning an owned String is one direct solution.

Lifetimes describe how long references are valid; they do not make references owners of their data. The compiler uses lifetime rules to prevent dangling references rather than allowing a reference to outlast the value it borrows.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

One way to remember the model

  • Moving a non-Copy value transfers ownership; the previous binding cannot be used afterward.
  • Borrowing with &T gives temporary read access without transferring ownership.
  • Borrowing with &mut T gives temporary mutable access and requires exclusivity.
  • A reference must not remain usable beyond the lifetime of the value it refers to.

The current official book says it assumes Rust 1.97.0 or later, released July 9, 2026, and uses Rust 2024 Edition idioms with edition = "2024" in Cargo.toml. See the official book landing page for the current edition and available formats.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.