3 In order to make common patterns more ergonomic, Rust allows lifetimes to be
4 *elided* in function signatures.
6 A *lifetime position* is anywhere you can write a lifetime in a type:
14 Lifetime positions can appear as either "input" or "output":
16 * For `fn` definitions, input refers to the types of the formal arguments
17 in the `fn` definition, while output refers to
18 result types. So `fn foo(s: &str) -> (&str, &str)` has elided one lifetime in
19 input position and two lifetimes in output position.
20 Note that the input positions of a `fn` method definition do not
21 include the lifetimes that occur in the method's `impl` header
22 (nor lifetimes that occur in the trait header, for a default method).
24 * In the future, it should be possible to elide `impl` headers in the same manner.
26 Elision rules are as follows:
28 * Each elided lifetime in input position becomes a distinct lifetime
31 * If there is exactly one input lifetime position (elided or not), that lifetime
32 is assigned to *all* elided output lifetimes.
34 * If there are multiple input lifetime positions, but one of them is `&self` or
35 `&mut self`, the lifetime of `self` is assigned to *all* elided output lifetimes.
37 * Otherwise, it is an error to elide an output lifetime.
42 fn print(s: &str); // elided
43 fn print<'a>(s: &'a str); // expanded
45 fn debug(lvl: usize, s: &str); // elided
46 fn debug<'a>(lvl: usize, s: &'a str); // expanded
48 fn substr(s: &str, until: usize) -> &str; // elided
49 fn substr<'a>(s: &'a str, until: usize) -> &'a str; // expanded
51 fn get_str() -> &str; // ILLEGAL
53 fn frob(s: &str, t: &str) -> &str; // ILLEGAL
55 fn get_mut(&mut self) -> &mut T; // elided
56 fn get_mut<'a>(&'a mut self) -> &'a mut T; // expanded
58 fn args<T: ToCStr>(&mut self, args: &[T]) -> &mut Command // elided
59 fn args<'a, 'b, T: ToCStr>(&'a mut self, args: &'b [T]) -> &'a mut Command // expanded
61 fn new(buf: &mut [u8]) -> BufWriter; // elided
62 fn new<'a>(buf: &'a mut [u8]) -> BufWriter<'a> // expanded