T: 'a inference in structs

An annotation in the form of T: 'a, where T is either a type or anotherlifetime, is called an "outlives" requirement. Note that "outlives" alsoimplies 'a: 'a.
One way in which edition 2018 helps you out in maintaining flow when writingprograms is by removing the need to explicitly annotate these T: 'a outlivesrequirements in struct definitions. Instead, the requirements will beinferred from the fields present in the definitions.
Consider the following struct definitions in Rust 2015:
2. #![allow(unused_variables)]
3. fn main() {
4. // Rust 2015
6. struct Ref<'a, T: 'a> {
7. field: &'a T
8. }
10. // or written with a `where` clause:
12. struct WhereRef<'a, T> where T: 'a {
13. data: &'a T
14. }
16. // with nested references:
18. struct RefRef<'a, 'b: 'a, T: 'b> {
19. field: &'a &'b T,
20. }
22. // using an associated type:
24. struct ItemRef<'a, T: Iterator>
25. where
26. T::Item: 'a
27. {
28. field: &'a T::Item
29. }
30. }
In Rust 2018, since the requirements are inferred, you can instead write:
1. // Rust 2018
3. struct Ref<'a, T> {
4. field: &'a T
5. }
7. struct WhereRef<'a, T> {
8. data: &'a T
9. }
11. struct RefRef<'a, 'b, T> {
12. field: &'a &'b T,
13. }
15. struct ItemRef<'a, T: Iterator> {
16. field: &'a T::Item
17. }
If you prefer to be more explicit in some cases, that is still possible.
More details
For more details, see the tracking issueand the RFC.
