Issues in the Rust reference book

Viewed 58

https://doc.rust-lang.org/reference/types/closure.html#capture-modes

struct SetVec {
    set: HashSet<u32>,
    vec: Vec<u32>
}

impl SetVec {
    fn populate(&mut self) {
        let vec = &mut self.vec;
        self.set.iter().for_each(|&n| {
            vec.push(n);
        })
    }
}

If, instead, the closure were to use self.vec directly, then it would attempt to capture self by mutable reference. But since self.set is already borrowed to iterate over, the code would not compile.

The reference book says the code would not compile. , but the code can be compiled. So why?

1 Answers
Related