
Understand it
A parameter of type String takes ownership, while &str only borrows text. Read the signature before deciding whether the caller should keep using its value.
A function signature is an ownership map. fn inspect(text: &str) asks for temporary read access and lets the caller keep its String. fn store(text: String) takes ownership, so the caller cannot use that binding after the call unless the function returns it or the caller cloned it beforehand. A parameter like i32 is passed by value too, but i32 is Copy, which leaves the original usable. The syntax is the same; the type determines the effect.
Walk through the example: name owns its bytes, &name creates a borrowed view for inspect, and the borrow ends after its final use in the call. The last println! can therefore read name. Change inspect to accept String and pass name without &; the final print now triggers a move error. Returning a String is a legitimate design if the function transforms ownership, but a printing helper has no reason to consume its input.
When designing an API, use &str if any UTF-8 text view will do, &String only when the String container itself matters, and String when ownership or mutation independent of the caller is required. For mutable access without transfer, use &mut String. Try calling one borrowed helper with both a string literal and an owned string. The more flexible signature often makes the surrounding code simpler and avoids clones inserted only to satisfy an unnecessarily consuming function.
Read the code
fn inspect(text: &str) {
println!("{text}");
}
let name = String::from("Ferris");
inspect(&name);
println!("{name}");The example makes the contract visible in code.
Try it
Trace name through inspect(&name) and say who owns it at the last println!.
What success looks like: You can say why name stays usable, and what would change if inspect took String.
Check your understanding
Why can name be printed after inspect?
Reveal the answer
- String is Copy
- inspect only borrowed it
- println! restores it
Correct: inspect only borrowed it.
Go deeper
Common misconception: “name can be printed afterward because String is Copy.”
String is not Copy. inspect takes &str, so &name only lends the text. Change the parameter to String and the last println! fails with E0382.
- Read the parameter:
inspecttakestext: &str, soinspect(&name)lends the text, andnamekeeps ownership in the caller. - Predict before checking: Predict what the snippet prints. Then predict what changes if
inspecttakestext: Stringand the call becomesinspect(name). - Compare with evidence: As written,
Ferrisprints twice. WithString, the finalprintln!gets E0382, becausenamemoved into the call.
Transfer challenge: Add fn shout(text: &mut String) that calls text.push('!'), and pass it &mut name. Decide which of inspect and shout can take a string literal, and check your answer.
Connections
- Operator Traits: Add, Display, From, and Into
- Mutable Borrows: One temporary writer with exclusive access
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: Moves Across Functions (courseId "ownership", lessonId "move-functions") Link: https://crabrix.com/learn/ownership/move-functions/ Goal: A parameter of type String takes ownership, while &str only borrows text. Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=ownership/move-functions) 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.