|
| 1 | +# `infer_outlives_requirements` |
| 2 | + |
| 3 | +The tracking issue for this feature is: [#44493] |
| 4 | + |
| 5 | +[#44493]: https://github.com/rust-lang/rust/issues/44493 |
| 6 | + |
| 7 | +------------------------ |
| 8 | +The `infer_outlives_requirements` feature indicates that certain |
| 9 | +outlives requirements can be inferred by the compiler rather than |
| 10 | +stating them explicitly. |
| 11 | + |
| 12 | +For example, currently generic struct definitions that contain |
| 13 | +references, require where-clauses of the form T: 'a. By using |
| 14 | +this feature the outlives predicates will be inferred, although |
| 15 | +they may still be written explicitly. |
| 16 | + |
| 17 | +```rust,ignore (pseudo-Rust) |
| 18 | +struct Foo<'a, T> |
| 19 | + where T: 'a // <-- currently required |
| 20 | + { |
| 21 | + bar: &'a T, |
| 22 | + } |
| 23 | +``` |
| 24 | + |
| 25 | + |
| 26 | +## Examples: |
| 27 | + |
| 28 | + |
| 29 | +```rust,ignore (pseudo-Rust) |
| 30 | +#![feature(infer_outlives_requirements)] |
| 31 | +
|
| 32 | +// Implicitly infer T: 'a |
| 33 | +struct Foo<'a, T> { |
| 34 | + bar: &'a T, |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +```rust,ignore (pseudo-Rust) |
| 39 | +#![feature(infer_outlives_requirements)] |
| 40 | +
|
| 41 | +// Implicitly infer `U: 'b` |
| 42 | +struct Foo<'b, U> { |
| 43 | + bar: Bar<'b, U> |
| 44 | +} |
| 45 | +
|
| 46 | +struct Bar<'a, T> where T: 'a { |
| 47 | + x: &'a (), |
| 48 | + y: T, |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +```rust,ignore (pseudo-Rust) |
| 53 | +#![feature(infer_outlives_requirements)] |
| 54 | +
|
| 55 | +// Implicitly infer `b': 'a` |
| 56 | +struct Foo<'a, 'b, T> { |
| 57 | + x: &'a &'b T |
| 58 | +} |
| 59 | +``` |
| 60 | + |
| 61 | +```rust,ignore (pseudo-Rust) |
| 62 | +#![feature(infer_outlives_requirements)] |
| 63 | +
|
| 64 | +// Implicitly infer `<T as std::iter::Iterator>::Item : 'a` |
| 65 | +struct Foo<'a, T: Iterator> { |
| 66 | + bar: &'a T::Item |
| 67 | +``` |
0 commit comments