Why does a for loop not require a mutable iterator?

后端 未结 1 909
隐瞒了意图╮
隐瞒了意图╮ 2020-12-11 15:51

If I want to consume an iterator by hand, it has to be mutable:

let test = vec![1, 2, 3];
let mut test_mut = test.iter();
while let Some(val) = test_mut.next         


        
相关标签:
1条回答
  • 2020-12-11 16:04

    That's exactly right. Since it's moved to the for loop, the for loop now owns it and can do whatever it wants with it, including "making it" mutable. Consider this analogous example, where we appear to be mutating xs despite it being immutable, but really it's because we're moving it, so the new owner is free to do with it whatever it wants, including re-binding it as mutable:

    let xs: Vec<i32> = vec![1, 2, 3];
    
    fn append(v: Vec<i32>, x: i32) -> Vec<i32> {
        let mut my_v = v;
        my_v.push(x);
        my_v
    }
    
    let appended = append(xs, 4);
    

    playground

    Note that the function can be made shorter using the mut parameter convenience syntax:

    fn append(mut v: Vec<i32>, x: i32) -> Vec<i32> {
        v.push(x);
        v
    }
    

    This is more or less explained in the iter module's documentation.

    0 讨论(0)
提交回复
热议问题