References
What you'll learn:
- pass a value to a function by reference with
&without giving up ownership; - change someone else's string or number through
&mut; - explain the "one writer or many readers" rule and find where a borrow ends;
- take slices of a string and explain why you cannot return a reference to a local variable.

In the previous theme, a function with a String parameter took the string for good. To get the string back, the function had to hand it back as its result. But often a function only needs to read the string: print it or count its length. Handing over the key to the unit for that is needless fuss.
Picture a friend asking to read your book. You do not give it away, you lend it: you stay the owner of the book. Your friend reads it, and the book comes back to your shelf. Rust can lend a value too, and references are how it does that.
A reference with &
A reference is a value that gives access to another value but does not own it. The & operator creates it: &book means "a reference to book". The type of a reference to a String is written &String.
fn shout(s: &String) {
println!("{s}!");
}
fn main() {
let book = String::from("Dune");
shout(&book); // Dune!
shout(&book); // Dune!
println!("{book} {}", book.len()); // Dune 4
}Let's go through the example line by line:
- The parameter
shas the type&String: the function expects not the string itself but a reference to it. let book = String::from("Dune");makesbookthe owner of the string.shout(&book)passes a reference to the function.bookstays the owner, andsonly reads the string.- The function prints
Dune!. At its}the parameters, the reference, goes away, and the string stays where it is. - The second call works the same way: the string has not gone anywhere.
- The last line prints
Dune 4:bookstill owns the string, andlen()returns its length.
With a String parameter, the second call would no longer compile: the string would have gone into the function on the first call.
Borrowing
Creating a reference is called borrowing: the value is taken for a while, and the owner stays the same. Inside, a reference holds the address where book lives, and the bytes Dune themselves are not copied anywhere.
Only the owner frees the string, and it does so at its own }. A reference owns nothing, so when it goes away there is nothing to free.
You write & in two places: in the parameter type and in the call. So the line shout(&book) shows at once that the function only borrows the string and book stays usable after the call.
A reference in a variable
You can also put a reference into an ordinary variable:
fn main() {
let book = String::from("Dune");
let r = &book;
println!("{r} {}", r.len()); // Dune 4
println!("{book}"); // Dune
}Let's go step by step:
let r = &book;puts a reference tobookintor. The type ofris&String.r.len()calls a method through the reference. The dot finds the string the reference points to on its own, and you add nothing.println!prints the stringDuneitself through the reference, not an address.bookis still the owner, and you can read it as before.
Here the book analogy falls short. In real life a friend may forget to return the book. A reference does not need returning: it stops working by itself once you stop using it. And the compiler makes sure that a reference never outlives the book.
Interviewers like to ask this: "How does a &String parameter differ from a String parameter?" The answer:
Stringmoves ownership into the function, and the calling code loses the string;&Stringpasses a reference: the owner stays the same, and the string is usable after the call;- when the function returns, only the reference goes away, and the owner frees the string later;
- the string's bytes are not copied in either case.
Common mistake
The function expects a reference, but the & was left out of the call:
fn shout(s: &String) {
println!("{s}!");
}
fn main() {
let book = String::from("Dune");
shout(book); // ✗
}error[E0308]: mismatched typesThe program does not compile: the function expects a &String but got the String itself. Rust does not turn a value into a reference behind your back, because giving away a string and lending it are different things. Below the error, the compiler suggests the fix: shout(&book).
Key points
- The reference
&bookgives access to a value but does not own it; a reference to aStringhas the type&String. - Borrowing does not take ownership: after
shout(&book)you can keep usingbook. - When a reference goes away, the value is not freed: only the owner does that.
- Methods and
println!work through a reference the same way as with the value itself.