【发布时间】:2016-02-17 12:11:12
【问题描述】:
我有这个特点和简单的结构:
use std::path::{Path, PathBuf};
trait Foo {
type Item: AsRef<Path>;
type Iter: Iterator<Item = Self::Item>;
fn get(&self) -> Self::Iter;
}
struct Bar {
v: Vec<PathBuf>,
}
我想为Bar 实现Foo 特征:
impl Foo for Bar {
type Item = PathBuf;
type Iter = std::slice::Iter<PathBuf>;
fn get(&self) -> Self::Iter {
self.v.iter()
}
}
但是我收到了这个错误:
error[E0106]: missing lifetime specifier
--> src/main.rs:16:17
|
16 | type Iter = std::slice::Iter<PathBuf>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^ expected lifetime parameter
我发现无法在该关联类型中指定生命周期。特别是我想表达迭代器不能超过self 的生命周期。
我必须如何修改 Foo 特征或 Bar 特征实现才能使其工作?
【问题讨论】: