为什么我不能push
在这个向量上inspect
做contains
这个过程skip_while
?
我已经为自己的结构实现了自己的迭代器,Chain
如下所示:
struct Chain {
n: u32,
}
impl Chain {
fn new(start: u32) -> Chain {
Chain { n: start }
}
}
impl Iterator for Chain {
type Item = u32;
fn next(&mut self) -> Option<u32> {
self.n = digit_factorial_sum(self.n);
Some(self.n)
}
}
take
现在,当迭代器产生唯一值时,我想做什么。所以我正在inspect
处理链并推送到一个向量,然后在一个take_while
范围内检查它:
let mut v = Vec::with_capacity(terms);
Chain::new(i)
.inspect(|&x| {
v.push(x)
})
.skip_while(|&x| {
return v.contains(&x);
})
然而,Rust 编译吐出这个错误:
error: cannot borrow `v` as immutable because it is also borrowed as mutable [E0502]
...
borrow occurs due to use of `v` in closure
return v.contains(&x);
^
previous borrow of `v` occurs here due to use in closure; the mutable borrow prevents subsequent moves, borrows, or modification of `v` until the borrow ends
.inspect(|&x| {
v.push(x)
})
显然我不明白“借”的概念。我究竟做错了什么?