Tôi muốn tạo một trình lặp tạo ra một luồng các số nguyên tố. quá trình suy nghĩ chung của tôi là quấn một iterator với các bộ lọc liên tiếp ví dụ như vậy bạn bắt đầu vớiLàm cách nào để soạn Iterator có thể thay đổi?
let mut n = (2..N)
Sau đó, đối với mỗi số nguyên tố bạn biến những iterator và thêm vào một bộ lọc
let p1 = n.next()
n = n.filter(|&x| x%p1 !=0)
let p2 = n.next()
n = n.filter(|&x| x%p2 !=0)
Tôi cố gắng để sử dụng đoạn mã sau, nhưng tôi dường như không thể làm cho nó hoạt
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
}
}
}
tôi đã đùa giỡn với các biến thể của việc này, nhưng tôi dường như không thể có được tuổi thọ và các loại để phù hợp lên. Ngay bây giờ các trình biên dịch là nói cho tôi
- Tôi không thể đột biến self.base
- thủ biến không sống đủ lâu
Đây là lỗi Tôi nhận
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
Tại sao Rust không cho phép tôi thực hiện việc này?
Oh well, tôi hoàn toàn quên nó đi. Cảm ơn! –
* và với đối tượng tra cứu các đối tượng không có nơi nào bạn có thể lưu trữ nó * - ngoài ra, kích thước ** thực tế ** của biến lặp 'base' thay đổi khi nó ngày càng được lồng vào nhau. Vì vậy, chúng ta cần phải sử dụng phân bổ đống để kích thước của 'Primes' luôn luôn là hằng số. – Shepmaster
@Shepmaster, Kích thước đối tượng tra cứu không thay đổi, afaik. Lý do duy nhất nó không thể được sử dụng là quyền sở hữu. –