Owned strings and scope
Quick answer
A String owns its text storage and releases that storage when the value is normally dropped. This makes its resource lifetime follow its owner rather than a manually chosen free operation.
Where an owner is usable
String::from creates an owned string from text. A binding is visible inside its lexical scope. At a closing brace, initialized local owners that have not been moved or otherwise consumed are dropped normally. This does not imply destructors run in every termination scenario; abrupt abort is an exception.
fn main() { { let note = String::from("draft"); println!("{note}"); } println!("scope ended");}Expected stdout (not executed):
draftscope endedThe String's normal cleanup happens before the final message.
fn main() { { let note = String::from("draft"); } println!("{note}");}Expected: compilation fails; exact diagnostic not verified.
The name is unavailable outside the braces; this is not runnable code.
Common mistake
Do not manually free the storage owned by a String. The owner already manages it.
Try it yourself
Create an outer String and an inner String in a nested block. Predict which names can be used after the inner block.
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
Owned strings and scope — practice quiz
Choose one answer per question, then review the explanation.
A String is created inside a block and not moved out. When does normal cleanup occur?
Can a binding declared only inside a block be named outside it?
Which operation constructs an owned string from the literal `"hi"`?