Rust Ownership and Borrowing

fn main(){
    let mut example = vec![1,2,3,4,5];
}
fn func(example: &mut Vec<i32>){
    for num in example{
        println!("{}", num);
    }
    println!("{:?}", example);
}

An implicit iterator "Into_iter" is used, which occurs after the loop has completed. Does an iterator return data back to a vector? Because if you specify it explicitly, the code works, but if not, then the error “example moved due to this implicit call to .into_iter()” will appear. If you can, tell us point by point the rules of ownership and borrowing. So that I understand why this happens

fn main(){
    let mut example = vec![1,2,3,4,5];
}
fn func(example: &mut Vec<i32>){
    for num in example.into_iter(){
        println!("{}", num);
    }
    println!("{:?}", example);
}

Upvotes: 1

Views: 53

Answers (0)

Related Questions