trait

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

  1. // 带有生命周期标注的结构体。
  2. #[derive(Debug)]
  3. struct Borrowed<'a> {
  4. x: &'a i32,
  5. }
  6. // 给 impl 标注生命周期。
  7. impl<'a> Default for Borrowed<'a> {
  8. fn default() -> Self {
  9. Self {
  10. x: &10,
  11. }
  12. }
  13. }
  14. fn main() {
  15. let b: Borrowed = Default::default();
  16. println!("b is {:?}", b);
  17. }

参见:

trait