Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Iteration moves values

  • If we remove the & from the first iteration the code won't compile any more as we have moved the values.
fn main() {
    let animals = vec![
        String::from("cat"),
        String::from("dog"),
        String::from("crab"),
    ];

    for animal in &animals {
        println!("{animal}");
    }
    println!();

    for animal in animals {
        println!("{animal}");
    }
}
cat
dog
crab

cat
dog
crab