Self: Sized + 'a
或
T: T + 'a
Seen this syntax while following a tut或ial which suggests using rust 2018 edition instead of 2021 if it is related to your answer. However, I can not find the meaning of + lifetime parameter
syntax.
Self: Sized + 'a
或
T: T + 'a
Seen this syntax while following a tut或ial which suggests using rust 2018 edition instead of 2021 if it is related to your answer. However, I can not find the meaning of + lifetime parameter
syntax.
这意味着您在泛型中使用的任何类型都必须超过'a
,这意味着它包含的所有引用都必须超过'a
.以下是一些永远有效的类型.
i32 // contains no lifetimes, so all zero lifetimes outlive 'a
Vec<u8> // same as above
&'static str // 'static outlives all lifetimes
SomeStruct<'static> // same as above
这里有一些类型,只有当他们的生命周期 超过'a
岁时才会起作用.
Vec<&'b u8> // 'b must outlive 'a
&'c str // 'c must outlive 'a
SomeStruct<'d> // 'd must outlive 'a
AnotherStruct<'x, 'y, 'z> // all lifetimes must outlive 'a
这最常与+ 'static
一起使用,这意味着"所有包含的生命周期 必须是'static
".您还可以在像Box<dyn Error + 'a>
这样的特征对象和像impl Iterator<Item = u8> + 'a
这样的impl Trait
类型上看到它,在这些对象中,它的意思是"该类型只存在'a
年",就像常规的生命周期参数一样.
我认为,自2018年版以来,这方面没有任何变化.