Borrow of moved value

please see these comment inside code below. Thank you.

fn main() {

    let foo = ["hello", "world"];
    for el in foo.into_iter(){
        println!("{el}");
    }

    println!("{:#?}", foo);// i am able to access this foo here.

}   
fn main() {

    let foo = vec!["hello", "world"];  // changed this data type of foo from array to vec just.
    for el in foo.into_iter(){
        println!("{el}");
    }

    println!("{:#?}", foo);// i am unable to access this foo here.

} 

Do read basic learning materials before writing any Rust code. Your code snippets are concerned with move vs Copy behavior. What is Ownership? - The Rust Programming Language

3 Likes

[&str; 2] is Copy, and Vec<&str> is not.

6 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.