6
我想做一個迭代器,生成素數的流。我一般的思維過程是如此,例如,你有如何編寫可變迭代器?
let mut n = (2..N)
開始,然後每個素數你變異迭代器,並添加過濾器
let p1 = n.next()
n = n.filter(|&x| x%p1 !=0)
let p2 = n.next()
n = n.filter(|&x| x%p2 !=0)
我想包一個迭代器連續濾膜使用下面的代碼,但我似乎無法得到它的工作
struct Primes {
base: Iterator<Item = u64>,
}
impl<'a> Iterator for Primes<'a> {
type Item = u64;
fn next(&mut self) -> Option<u64> {
let p = self.base.next();
match p {
Some(n) => {
let prime = n.clone();
let step = self.base.filter(move |&: &x| {x%prime!=0});
self.base = &step as &Iterator<Item = u64>;
Some(n)
},
_ => None
}
}
}
我與這個變化玩弄,但我似乎無法得到壽命和類型匹配。眼下編譯器告訴我
- 我不能突變self.base
- 變量主要不活夠
這裏長我得到
solution.rs:16:17: 16:26 error: cannot borrow immutable borrowed content `*self.base` as mutable
solution.rs:16 let p = self.base.next();
^~~~~~~~~
solution.rs:20:28: 20:37 error: cannot borrow immutable borrowed content `*self.base` as mutable
solution.rs:20 let step = self.base.filter(move |&: &x| {x%prime!=0});
^~~~~~~~~
solution.rs:21:30: 21:34 error: `step` does not live long enough
solution.rs:21 self.base = &step as &Iterator<Item = u64>;
^~~~
solution.rs:15:39: 26:6 note: reference must be valid for the lifetime 'a as defined on the block at 15:38...
solution.rs:15 fn next(&mut self) -> Option<u64> {
solution.rs:16 let p = self.base.next();
solution.rs:17 match p {
solution.rs:18 Some(n) => {
solution.rs:19 let prime = n.clone();
solution.rs:20 let step = self.base.filter(move |&: &x| {x%prime!=0});
...
solution.rs:20:71: 23:14 note: ...but borrowed value is only valid for the block suffix following statement 1 at 20:70
solution.rs:20 let step = self.base.filter(move |&: &x| {x%prime!=0});
solution.rs:21 self.base = &step as &Iterator<Item = u64>;
solution.rs:22 Some(n)
solution.rs:23 },
error: aborting due to 3 previous errors
錯誤
Rust爲什麼不讓我這樣做?
哦,我完全忘了它。謝謝! –
*並且引用特徵對象無處可存儲* - 此外,迭代器'base'的實際**大小**隨着它越來越多地嵌套而發生變化。因此,我們需要使用堆分配,以使「Primes」的大小始終保持不變。 – Shepmaster
@Shepmaster,引用特質對象的大小不會改變,afaik。它不能使用的唯一原因是所有權。 –