// *** 以前 ***
// 关联类型过去不能有自己的参数,
// 所以根本无法写出 lending trait
trait Container {
type Item;
fn get(&self) -> Self::Item; // 只能按值
}
// *** 在 1.65 版本中 ***
trait Container {
type Item<'a> where Self: 'a;
fn get<'a>(&'a self) -> Self::Item<'a>; // 内部带引用也没问题
}
impl Container for Vec<String> {
type Item<'a> = &'a str;
fn get<'a>(&'a self) -> &'a str {
&self[0]
}
}