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
チェーンを -ing し、ベクトルにプッシュしてから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)
})
明らかに、「借りる」という概念を理解していません。私は何を間違っていますか?