String versus &str
Quick answer
String owns growable UTF-8 text. &str borrows a UTF-8 text region without owning it. Use String when you need to build or retain text; use &str when a function only needs to read it.
A flexible borrowed parameter
A string literal has type &str with a static lifetime. An owned String can provide a borrowed view using as_str(), or through coercion when an &str parameter is expected. The function can read either representation without allocating a new String. The original owner must remain valid while the borrowed view is used.
fn show(text: &str) { println!("{text}"); }fn main() { let owned = String::from("owned"); show("literal"); show(owned.as_str());}Expected stdout (not executed):
literalownedOne function accepts both borrowed views.
fn main() { let source = "ready"; let mut owned = source.to_string(); owned.push('!'); println!("{source} / {owned}");}Expected stdout (not executed):
ready / ready!Converting to String creates owned text, allowing modification.
Common mistake
Do not require &String for a function that only reads text; it excludes string literals unless a caller allocates.
Try it yourself
Write fn byte_length(text: &str) -> usize and call it with a literal and an owned String borrow.
Static examples for Rust 1.85.0, edition 2024. Run them locally in a disposable project; ByteMind execution support and these outputs have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
String versus &str — practice quiz
Choose one answer per question, then review the explanation.
What does `owned.as_str()` return for a String?
A function accepts `&str`. How can you pass an owned String named s without moving it?
Which value is an owned growable string?