ownership
Rust Ownership inside collections
A Vec<String> owns its stored strings. Pushing an owned String transfers it into the collection; removing it with a suitable API can return ownership. Use an ownership-aware API rather than trying to move non-Copy items out by indexing. Expected output (not executed): Review Remaining: 0 Why: push consumes the String; pop returns an Option containing the owned element. Common mistake: Writing let task = tasks[0] to move a String out of a vector.
Formal Syntax & Structure
rust syntaxrust
collection.push(value);Working Example
Example Snippetrust
fn main() { let mut tasks = Vec::new(); let task = String::from("Review"); tasks.push(task); if let Some(done) = tasks.pop() { println!("{done}"); } println!("Remaining: {}", tasks.len());}