我正在编写一些代码,我想在其中使用迭代器,或者它的反向版本取决于标志,但简单的代码给出了错误
pub fn eggs<I,T>(iter:I)->Box<dyn Iterator<Item=T>>
where I:Iterator<Item=T>+DoubleEndedIterator
{
Box::new(iter.rev())
}
pub fn bacon<I,T>(iter:I, reverse:bool) -> Box<dyn Iterator<Item=T>>
where I:Iterator<Item=T>+DoubleEndedIterator
{
if reverse {
Box::new(iter.rev())
} else {
Box::new(iter)
}
}
fn main()
{
let pants:String = "pants".into();
eggs(pants.chars());
}
Run Code Online (Sandbox Code Playgroud)
无法编译:
error[E0310]: the parameter type `I` may not live long enough
--> src/main.rs:5:5
|
2 | pub fn eggs<I,T>(iter:I)->Box<dyn Iterator<Item=T>>
| - help: consider adding an explicit lifetime bound...: `I: 'static`
...
5 | Box::new(iter.rev())
| ^^^^^^^^^^^^^^^^^^^^ ...so that the type `Rev<I>` will meet its required lifetime bounds
Run Code Online (Sandbox Code Playgroud)
由于我对 Rust 的了解有限,我不确定这些生命周期的界限从何而来。Iterator特征或Rev结构上没有任何内容,并且正在移动参数。
鉴于这'static不是一个真正的选择,声明这些类型的函数的正确方法是什么。
这完全没有关系.rev(),但与返回有关Box<dyn Iterator>:
// error[E0310]: the parameter type `I` may not live long enough
fn boxed_iter<I, T>(iter: I) -> Box<dyn Iterator<Item = T>>
// - help: consider adding an explicit lifetime bound...: `I: 'static`
where
I: Iterator<Item = T>,
{
Box::new(iter)
// ^^^^^^^^^^^^^^ ...so that the type `I` will meet its required lifetime bounds
}
Run Code Online (Sandbox Code Playgroud)
这样做的原因是 trait 对象如果没有指定则Box<dyn Trait>具有隐式'static生命周期。因此,当编译器尝试强制转换Box<I>为 时Box<dyn Iterator>,如果Iis 也没有'static生命周期,则会失败。(如果 trait 本身包含生命周期,则有一些更具体的规则;您可以在此处阅读更详细的内容。)
如果您想要更短的生命周期,则需要将其明确指定为Box<dyn 'a + Trait>. 例如:
fn boxed_iter<'a, I, T>(iter: I) -> Box<dyn 'a + Iterator<Item = T>>
where
I: 'a + Iterator<Item = T>,
{
Box::new(iter)
}
Run Code Online (Sandbox Code Playgroud)