Borrowing and references in Rust
Learn Rust · Beginner · 10 min · After Ownership and moves in Rust
How do you use a value without taking ownership of it?
1Guess
fn len(s: &String) -> usize { s.len() } fn main() { let s = String::from("hello"); let n = len(&s); println!("{s} has {n} bytes"); }2Read
References borrow
A reference lets you use a value without owning it.
&screates one, and the type&Stringor&straccepts it. The owner keeps the value, and nothing is dropped when the reference goes out of scope. The Book calls creating a reference borrowing. To change what you borrowed, you need a mutable reference,&mut s, and the variable must be declaredmut.3Code
A shared and a mutable borrow
1 / 2fn main() { let mut name = String::from("Ferris"); greet(&name); add_title(&mut name); greet(&name); } fn greet(who: &str) { println!("Hello, {who}!"); } fn add_title(who: &mut String) { who.push_str(" the Crab"); }Read the program. What will it print?
4Read
The borrowing rule
At any given time you can have either one mutable reference or any number of immutable references to a value. References must also always be valid, so one cannot outlive the value it points to. A reference lasts from where it is created to where it is last used, not to the end of the block.
5Code
A mutable borrow after the shared ones end
1 / 2fn main() { let mut s = String::from("hello"); let r1 = &s; let r2 = &s; println!("{r1} and {r2}"); let r3 = &mut s; r3.push_str(" world"); println!("{r3}"); }Read the program. What will it print?
6Fix
fn main() { let mut s = String::from("hello"); let r1 = &s; let r2 = &mut s; println!("{r1} {r2}"); }7Say it back
8Mission · 8 min
Write a function that takes &mut String and appends text to it, and another that takes &str and prints it. Call both from main, then try holding a shared reference across the mutable call and read the error.
cargo new borrow cd borrow cargo run
Common questions
- What is borrowing in Rust?
- Borrowing means using a value through a reference instead of taking ownership of it. You write &value to lend it for reading, or &mut value to lend it for changing. The owner keeps the value, and it is not dropped when the reference ends, so you can keep using the original afterwards.
- Why can't I have two mutable references in Rust?
- The borrowing rule allows either one mutable reference or any number of immutable ones at a time. Two mutable references to the same value could change it at once, so the compiler refuses them. A reference lasts until its last use, so you can take a new one after the first is no longer used.
- What is the difference between &str and &String?
- Both are references to text. A &String borrows a whole String, while &str is a string slice that borrows a part or all of some text. A function that takes &str is more flexible, because you can pass it a reference to a String as well as a string literal.
Sources
- https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html (read 2026-12-03)
- https://doc.rust-lang.org/book/ch04-03-slices.html (read 2026-12-03)
Measured with rustc 1.100.1 (play.rust-lang.org, stable, edition 2024) on 2026-12-03
Published Dec 3, 2026 · Updated Dec 3, 2026