Structs
Annotation of lifetimes in structures are also similar to functions:
1. // A type `Borrowed` which houses a reference to an
2. // `i32`. The reference to `i32` must outlive `Borrowed`.
3. #[derive(Debug)]
4. struct Borrowed<'a>(&'a i32);
6. // Similarly, both references here must outlive this structure.
7. #[derive(Debug)]
8. struct NamedBorrowed<'a> {
9. x: &'a i32,
10. y: &'a i32,
11. }
13. // An enum which is either an `i32` or a reference to one.
14. #[derive(Debug)]
15. enum Either<'a> {
16. Num(i32),
17. Ref(&'a i32),
18. }
20. fn main() {
21. let x = 18;
22. let y = 15;
24. let single = Borrowed(&x);
25. let double = NamedBorrowed { x: &x, y: &y };
26. let reference = Either::Ref(&x);
27. let number = Either::Num(y);
29. println!("x is borrowed in {:?}", single);
30. println!("x and y are borrowed in {:?}", double);
31. println!("x is borrowed in {:?}", reference);
32. println!("y is *not* borrowed in {:?}", number);
33. }
