---
title: "Ownership and moves in Rust"
slug: "ownership-and-moves"
description: "Learn the three ownership rules, how a String moves when you assign it or pass it to a function, and when to clone. Fix a use of a moved value."
published: "2026-12-03"
updated: "2026-12-03"
canonical: "https://aifree.dev/courses/learn-rust/ownership-and-moves"
---

# Ownership and moves in Rust

Who owns a value in Rust, and when is it freed?

## Predict

```rust
fn main() {
    let a = 5;
    let b = a;
    println!("{a} {b}");
}
```

What does this program print?

- 5 5
- It does not compile, because a moved into b
- 5, then a blank for a

Answer: 5 5 — An integer is copied on assignment, so a and b are both usable.

Output:

```
5 5
```

Integers are simple values with a known size, so `let b = a` copies them. Nothing moves, and both variables stay valid.

## The ownership rules

Rust manages memory with three rules the compiler checks. **Each value has an owner. There can only be one owner at a time. When the owner goes out of scope, the value is dropped.** Dropped means Rust frees the memory for you, at the closing brace of the scope. You write no free call and there is no garbage collector.

## A String and a function that returns it

```rust
fn main() {
    let mut s = String::from("hello");
    s.push_str(", world");
    println!("{s}");
    let t = take_and_give_back(s);
    println!("{t}");
}

fn take_and_give_back(text: String) -> String {
    println!("got {text}");
    text
}
```

Output:

```
hello, world
got hello, world
hello, world
```

`String::from` makes a String you can grow with `push_str`. Passing `s` to the function moves it, so the function returns the String and `t` becomes its new owner.

Playground: https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=c807275c42657f985988de27d3e88899

## Move, Copy and clone

Assigning a `String` to another variable **moves** it: the new variable owns the data and the old one can no longer be used. Types with a fixed size, such as integers, implement **Copy** and are copied instead. If you want a second, independent `String`, call `.clone()`. It copies the data, so use it when you need both.

## clone, then move into a function

```rust
fn main() {
    let s1 = String::from("hello");
    let s2 = s1.clone();
    println!("{s1} and {s2}");
    let len = calculate_length(s2);
    println!("length {len}");
}

fn calculate_length(s: String) -> usize {
    s.len()
}
```

Output:

```
hello and hello
length 5
```

`s1.clone()` makes `s2` its own copy, so both print. `calculate_length(s2)` takes ownership of `s2`, which is why we do not use `s2` after that line.

Playground: https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=828a0e163f1f33ffb89e78a082ca060f

## Fix it

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

This program does not compile. What is the bug?

- println! cannot print a String twice
- s2 needs the type String written out
- let s2 = s1 moved the String, so s1 is no longer valid

Answer: let s2 = s1 moved the String, so s1 is no longer valid — The fix is to clone s1, or to use s2 and not s1 afterwards.

Compiler message:

```
error[E0382]: borrow of moved value: `s1`
```

Fixed:

```rust
fn main() {
    let s1 = String::from("hello");
    let s2 = s1.clone();
    println!("{s1}, world!");
    println!("{s2}");
}
```

Output:

```
hello, world!
hello
```

## Say it back

In your own words: what is a move, and why can you not use the old variable afterwards?

Our sentence: A move hands a value to a new owner; since a value can have only one owner at a time, the old variable no longer owns it and the compiler rejects any use of it.

## Mission (8 min)

Write a program that makes a String, moves it into a function that prints it and returns its length, and prints the length. Then try using the String after the call and read the error.

```sh
cargo new owners
cd owners
cargo run
```

## Common questions

### What is ownership in Rust?

Ownership is the set of rules Rust uses to manage memory without a garbage collector. Each value has one owner, there can be only one owner at a time, and when the owner goes out of scope the value is dropped. The compiler checks these rules, so a program that breaks them does not compile.

### What does move mean in Rust?

A move transfers ownership of a value. When you write let s2 = s1 with a String, s2 becomes the owner and s1 can no longer be used; the compiler reports an error if you try. Passing a String to a function moves it too. Simple types such as integers are copied instead of moved.

### When should I use clone in Rust?

Call clone when you need a second, independent copy of a value such as a String, and both copies must stay usable. It copies the data, so it costs time and memory for large values. If you only need to read a value in a function, borrow it with a reference instead of cloning.

## Sources

- https://doc.rust-lang.org/book/ch04-01-what-is-ownership.html (read 2026-12-03)
- https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html (read 2026-12-03)

Measured with rustc 1.100.1 (play.rust-lang.org, stable, edition 2024) on 2026-12-03.
