Rust

Rust Ownership and Borrowing — Moves, Borrows, and Clones

Ownership is the rule set that decides who is responsible for a value and when it is dropped. This guide runs programs: a String is borrowed by a function that reads its length, cloned so two variables own a copy each, and then moved, after which the original variable is gone. A second program does the same assignment with an i32 and both variables still work. A third program makes the classic beginner mistake, using a value after moving it, and the compiler answers with error E0382, quoted in full.

Written November 11, 2026 · 9 min read

By the end of this guide
  • The three rules
  • The working program: borrow, clone, move
  • Move versus copy: a String moves, an i32 copies
  • Borrowing: read without taking ownership

What this guide is

Every value in Rust has one owner, and the owner decides when the value is dropped. That single rule explains most of the errors a beginner meets: a value used after it was moved, a function that takes a string and never gives it back, a field pulled out of a struct that is then used whole. This guide runs four small programs, edition 2021. One program works and shows borrowing, cloning, and moving in six lines. The others isolate one rule each: an integer assignment that copies instead of moving, a use-after-move that the compiler rejects, and a struct field that moves out on its own.

The three rules

The Rust Book states the ownership rules as three sentences. Each value in Rust has an owner. There can only be one owner at a time. When the owner goes out of scope, the value is dropped. The rest of this guide is those three sentences at work. The one owner rule is why assignment needs a second concept, the move: assigning a value to a new variable transfers ownership instead of sharing it, so the one-owner rule still holds after the assignment. The drop rule is why Rust needs no explicit free and no garbage collector pause: when the owner leaves its scope, the value and the memory behind it are released at that point.

The working program: borrow, clone, move

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

fn main() {
    let s1 = String::from("hello");
    let len = length(&s1);
    println!("{} has length {}", s1, len);
    let s2 = s1.clone();
    println!("s1={} s2={}", s1, s2);
    let s3 = s1;
    println!("s3={}", s3);
}

The exact output:

Output
hello has length 5
s1=hello s2=hello
s3=hello

Three ownership events happen in that program, one per output line. First, length(&s1) borrows s1: the & passes a reference, the function reads the length, and s1 still owns its string afterward, which is why the same line can print s1 itself. Second, s1.clone() copies the string data and gives the copy to s2, so two variables own two strings with the same contents, and the second line prints both. Third, let s3 = s1; moves the string: s3 is the owner now, and s1 is no longer usable. The third line prints only s3, because printing s1 after that move is exactly the error the next section makes on purpose.

Move versus copy: a String moves, an i32 copies

Whether an assignment moves or copies depends on the type. A String owns data on the heap, so assignment moves it: two owners of one heap buffer would free it twice, and the move rule prevents that by retiring the old variable. A small fixed-size type such as i32 implements the Copy trait instead, so assignment copies the bits and both variables stay usable. A second program shows the contrast:

Rust
fn main() {
    let a: i32 = 5;
    let b = a;
    println!("a={} b={}", a, b);
}

It compiles and prints a=5 b=5, exiting with code 0. The line let b = a; looks identical to the line that moved the string, and the difference is entirely in the type: i32 is Copy, String is not. The compiler error in the failing program below says so in its own note: a move occurs because String does not implement the Copy trait. When an assignment surprises you, check the type first: heap-owning types move, plain scalar types copy.

Borrowing: read without taking ownership

Passing a value to a function moves it, by the same assignment rule, unless the type is Copy. A function that only needs to read a string would consume it under that rule, and the caller would lose its value to a length check. Borrowing is the escape: &s1 passes a reference, the function signature fn length(s: &String) declares that it accepts a reference, and ownership never leaves the caller. The working program depends on this in its first line of output: length(&s1) returns 5, and s1 prints on the same line, still owned by main. Reach for a borrow whenever a function needs to look at a value rather than keep it: taking ownership to perform a read is the beginner pattern that produces the next section's error in real code, one function call later.

Clone: a second owner, at a price

clone makes a full second copy of the value, heap data included, and gives it its own owner. In the working program, let s2 = s1.clone(); leaves both variables printable, and the output line s1=hello s2=hello proves each owns a complete string. The price is the copy itself: cloning a five-byte string is nothing, and cloning a large buffer duplicates every byte of it. The compiler treats clone as the honest answer rather than the default one: in the error output below, its own suggestion is to clone, qualified with the words "if the performance cost is acceptable". Clone when two owners are genuinely needed, borrow when one of them only needs to read, and let the cost phrase in the compiler's hint be the reminder of which is which.

The E0382 error, read line by line

The classic beginner mistake is using a value after moving it:

Rust
fn main() {
    let s1 = String::from("hello");
    let s2 = s1;
    println!("{}", s1);
    println!("{}", s2);
}

The compiler rejects it, and the build exits with status 101. The exact error:

Output
error[E0382]: borrow of moved value: `s1`
 --> src/main.rs:4:20
  |
2 |     let s1 = String::from("hello");
  |         -- move occurs because `s1` has type `String`, which does not implement the `Copy` trait
3 |     let s2 = s1;
  |              -- value moved here
4 |     println!("{}", s1);
  |                    ^^ value borrowed here after move
  |
help: consider cloning the value if the performance cost is acceptable
  |
3 |     let s2 = s1.clone();
  |                ++++++++

Read it in order. The headline names the problem and the variable: a borrow of a moved value, s1. The first note explains why the assignment moved at all: String does not implement Copy. The next two markers point at the move site, line 3, and the use site, line 4, so the two lines that matter are identified exactly. The help block then offers the clone fix as a code edit, with its cost caveat attached. Three fixes follow from the same reading: clone at line 3 when both variables must own a string, borrow at line 4 by printing through a reference taken before the move, or reorder so the print happens before the move. Which one is right depends on which variable the rest of the program actually needs, and the error gives you both line numbers to decide with.

Key result
using s1 after let s2 = s1; fails with error[E0382]: borrow of moved value: s1, the build exits with status 101, and the error itself points at the move on line 3 and the use on line 4.

Trap: moving one field out of a struct

Trap
A move can happen below the variable level. Moving a String field out of a struct leaves the struct partially moved: the remaining fields are still usable, and the moved field and the whole struct are not. A third program builds a User with a name and an age:
Rust
struct User {
    name: String,
    age: u32,
}

fn main() {
    let user = User {
        name: String::from("ana"),
        age: 30,
    };
    let name = user.name;
    println!("name={} age={}", name, user.age);
}

It compiles and prints name=ana age=30: user.name moved into name, and user.age still reads fine, because age never moved and a u32 would copy in any case. Adding one more line that reads user.name again breaks the build with the same error code, now naming the field: error[E0382]: borrow of moved value: user.name, with the note that the move occurs because user.name has type String, which does not implement the Copy trait. The build exits with status 101. Partial moves surprise people because the struct variable still exists and most of it still works; the rule to carry is that a struct is usable whole only while every field is still in it, and after a field moves out, the remaining fields are reachable one by one and the whole value is not.

Which mechanism for which job

Job Mechanism Why
A function needs to read a value and hand it back borrow (&T) Ownership stays with the caller, as length(&s1) shows
Two variables must each own the same contents clone Each owns a full copy, at the cost of duplicating the data
One variable hands a value over for good move Ownership transfers, the old variable retires, nothing is copied
A plain number or flag is assigned copy (automatic for Copy types) The bits are copied and both variables stay usable, as the i32 program shows

When a signature takes a value and the caller still needs it afterward, that is a borrow waiting to happen. When two owners must outlive each other independently, that is the genuine clone case. Everything else is a move, and the compiler will point at the exact line when a program asks for a value it already gave away.

Checklist

  • Before using a variable after an assignment, ask whether the assignment moved it: a String moves, an i32 copies.
  • Before passing a value to a function that only reads it, pass a reference: length(&s1) leaves the caller owning its string.
  • Before cloning, check whether a borrow does the job: clone duplicates the heap data, and the compiler itself flags the cost.
  • Before reading a compiler error from the bottom, read the headline first: E0382 names the variable, and the markers name the move line and the use line.
  • Before using a struct whole, check whether a field moved out of it: the remaining fields still read, the whole value and the moved field do not.
  • Before silencing an ownership error with a copy, confirm the type is small and Copy: the trait, not the assignment syntax, decides move versus copy.

Sources

Keep Exploring