Start with the idea
The & symbol creates a borrow. A shared reference such as &str permits reading but not mutation, and it does not own the value it points to. Passing a reference avoids a move, so the original binding remains usable after the call. Later ownership lessons add the precise aliasing rules and mutable references.
Read the code
fn length(text: &str) -> usize {
text.len()
}
let name = String::from("Crabrix");
println!("{} {name}", length(&name));length borrows the text, so the caller can still use name.
Practice starter
fn show(text: String) {
println!("{text}");
}
let name = String::from("Ferris");
show(name);
println!("{name}");
Work through it
In the example, name owns a String. Passing &name to length creates a shared borrow, and the parameter text: &str lets the function read the bytes through that borrow. length returns a usize, not the text itself. After the call ends, the caller still owns name and can print it. No second owned String had to be made for this read-only question.
A reference is valid only while the value it refers to is available. Rust checks that relationship so code cannot keep a pointer into a value that has already gone away. Shared references permit reading; mutation through a shared reference is not allowed. That is why a function that only needs to inspect text should often accept &str instead of taking String ownership. It can then work with both owned strings and borrowed string slices supplied by callers.
Borrowing is not a promise that the data will never change anywhere. It is a controlled access rule for a particular period and reference. Later lessons introduce mutable borrows and the rule that prevents conflicting simultaneous access. For now, change length to take String by value and see why the owner is unavailable after the call. Restore &str and explain what the ampersand changed about the function's contract.
References also have lifetimes, although the compiler often infers them in simple cases. A reference cannot outlive the value it points to. Return owned data or borrow from an input that lives long enough; do not try to return a reference to a local temporary.
A reference lets code temporarily use a value without becoming its owner. If a function accepts &String or &str to read text, the caller retains the original value after the call. The borrow must remain valid while the reference is used, and a shared borrow does not permit mutation of that value through the same reference. This is easier to understand if you draw two names: the owning binding and the temporary reference pointing to it.
Trace the example at the call site: who owns the string before the call, what the function can read, and who owns it afterward. Replace the borrowed parameter with an owned String and observe whether the caller can still use its binding. Then restore the reference. Prefer &str for a read-only text parameter when both string literals and String values should work. The signature says what the function needs; the borrow checker enforces that promise.
Try it
Pass text to a function by reference and use the owner afterward.
What success looks like: The caller retains ownership and no clone is needed.
Check your understanding
What signature keeps name usable?
Reveal the answer
- fn show(text: &str)
- fn show(mut text: String)
- fn show(text: Box<String>)
Borrowing &str lets show read the text without taking its String owner.
Learn this lesson with AI
Paste this into Claude, Codex, Cursor or another agent connected to Crabrix Academy. It contains only this lesson’s public details.
I'm learning Rust with Crabrix Academy. Teach me this lesson with the Crabrix Academy tools. Lesson: First References (courseId "basics", lessonId "references-intro") Link: https://crabrix.com/learn/basics/references-intro/ Goal: &T borrows T for read-only use without transferring ownership. Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=basics/references-intro) and I will write the code there. Explain it step by step for my level, let me write the code myself, give hints before any solution, and do not mark anything as passed: only Crabrix checks do that.