如何正确地从表达式返回一个装箱的可克隆迭代器?例如:
fn example() -> Box<Iterator<Item = String> + Clone> {
unimplemented!()
}
这让我出现了一个错误,只有自动特性可以用这种方式指定:
error[E0225]: only auto traits can be used as additional traits in a trait object
--> src/main.rs:1:47
|
1 | fn example() -> Box<Iterator<Item = String> + Clone> {
| ^^^^^ non-auto additional trait
这是我的真实代码:
let my_iterator = {
if a {
Box::new(/* ... */) as Box<Iterator<Item = String> + Clone>
} else {
Box::new(/* ... */) as Box<Iterator<Item = String> + Clone>
}
};
let pb = ProgressBar::new(my_iterator.clone().count() as u64);
如果考虑其他建议:两个分支代表从文件加载的一条路径,另一条路径代表自动生成,如果不需要保留在内存中,我宁愿不保留。
static
生命周期是什么意思吗?如果我把它移除掉,就会出现T may not live long enough
的错误。我无法理解在什么情况下,如果编译器忽略了这个错误,代码会崩溃。 - undefined