trait

trait 方法中生命期的标注基本上与函数类似。注意,impl 也可能有生命周期的标注。

1. // 带有生命周期标注的结构体。
2. #[derive(Debug)]
3. struct Borrowed<'a> {
4. x: &'a i32,
5. }

7. // 给 impl 标注生命周期。
8. impl<'a> Default for Borrowed<'a> {
9. fn default() -> Self {
10. Self {
11. x: &10,
12. }
13. }
14. }

16. fn main() {
17. let b: Borrowed = Default::default();
18. println!("b is {:?}", b);
19. }

参见:

trait