如何在 Rust 结构中保存迭代器

gec*_*kos 2 rust

我正在尝试这样做

struct RwindIter {
    iter: Box<dyn Iterator<Item = String>>,
}

fn build(i: impl Iterator<Item = String>) -> RwindIter {
    RwindIter { iter: Box::new(i) }
}
Run Code Online (Sandbox Code Playgroud)

但我收到了这个错误

   Compiling myml v0.1.0 (/Users/gecko/code/myml)
error[E0310]: the parameter type `impl Iterator<Item = String>` may not live long enough
  --> src/main.rs:47:23
   |
47 |     RwindIter { iter: Box::new(i) }
   |                       ^^^^^^^^^^^
   |
note: ...so that the type `impl Iterator<Item = String>` will meet its required lifetime bounds
  --> src/main.rs:47:23
   |
47 |     RwindIter { iter: Box::new(i) }
   |                       ^^^^^^^^^^^
help: consider adding an explicit lifetime bound  `'static` to `impl Iterator<Item = String>`...
   |
46 | fn build(i: impl Iterator<Item = String> + 'static) -> RwindIter {
   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0310`.
error: could not compile `myml`.

To learn more, run the command again with --verbose.
Run Code Online (Sandbox Code Playgroud)

我原以为这Box::new(x)会占据所有权,x所以我无法弄清楚错误消息的含义。有任何想法吗?


更新

好吧,我知道这是impl语法上的一些限制。这有效

struct I {}

impl Iterator for I {
    type Item = String;
    fn next(&mut self) -> Option<String> {
        None
    }
}

fn build(i: I) -> RwindIter {
    RwindIter { iter: Box::new(i) }
}
Run Code Online (Sandbox Code Playgroud)

Loc*_*cke 6

我建议仅使用常规通用来解决该问题。

pub struct FooIter<I> {
    iter: I,
}

impl<I> FooIter<I> {
    pub fn new(iter: I) -> Self {
        FooIter { iter }
    }
}

impl<I: Iterator<Item=String>> Iterator for FooIter<I> {
    type Item = String;
    
    fn next(&mut self) -> Option<Self::Item> {
        self.iter.next()
    }
}
Run Code Online (Sandbox Code Playgroud)

dyn Iterator<Item=String>但是,只要提供生命周期限制,您仍然可以使用 a 。然而,它可能会在以后的实现过程中导致生命周期的混乱,具体取决于您与该结构的交互方式。

pub struct FooIter<'a> {
    iter: Box<dyn Iterator<Item=String> + 'a>,
}

impl<'a> FooIter<'a> {
    pub fn new(iter: impl Iterator<Item=String> + 'a) -> Self {
        FooIter {
            iter: Box::new(iter),
        }
    }
}

impl<'a> Iterator for FooIter<'a> {
    type Item = String;
    
    fn next(&mut self) -> Option<Self::Item> {
        self.iter.next()
    }
}
Run Code Online (Sandbox Code Playgroud)