Elision

Some lifetime patterns are overwelmingly common and so the borrow checker
will implicitly add them to save typing and to improve readability.
This process of implicit addition is called elision. Elision exists in Rust
solely because these patterns are common.

The following code shows a few examples of elision. For a more comprehensive
description of elision, see lifetime elision in the book.

  1. // `elided_input` and `annotated_input` essentially have identical signatures
  2. // because the lifetime of `elided_input` is elided by the compiler:
  3. fn elided_input(x: &i32) {
  4. println!("`elided_input`: {}", x);
  5. }
  6. fn annotated_input<'a>(x: &'a i32) {
  7. println!("`annotated_input`: {}", x);
  8. }
  9. // Similarly, `elided_pass` and `annotated_pass` have identical signatures
  10. // because the lifetime is added implicitly to `elided_pass`:
  11. fn elided_pass(x: &i32) -> &i32 { x }
  12. fn annotated_pass<'a>(x: &'a i32) -> &'a i32 { x }
  13. fn main() {
  14. let x = 3;
  15. elided_input(&x);
  16. annotated_input(&x);
  17. println!("`elided_pass`: {}", elided_pass(&x));
  18. println!("`annotated_pass`: {}", annotated_pass(&x));
  19. }

See also:

elision