Simpler lifetimes in static and const

Minimum Rust version: 1.17

In older Rust, you had to explicitly write the 'static lifetime in anystatic or const that needed a lifetime:

  1. #![allow(unused_variables)]
  2. fn main() {
  3. mod foo {
  4. const NAME: &'static str = "Ferris";
  5. }
  6. mod bar {
  7. static NAME: &'static str = "Ferris";
  8. }
  9. }

But 'static is the only possible lifetime there. So Rust now assumes the 'static lifetime,and you don't have to write it out:

  1. #![allow(unused_variables)]
  2. fn main() {
  3. mod foo {
  4. const NAME: &str = "Ferris";
  5. }
  6. mod bar {
  7. static NAME: &str = "Ferris";
  8. }
  9. }

In some situations, this can remove a lot of boilerplate:

  1. #![allow(unused_variables)]
  2. fn main() {
  3. mod foo {
  4. // old
  5. const NAMES: &'static [&'static str; 2] = &["Ferris", "Bors"];
  6. }
  7. mod bar {
  8. // new
  9. const NAMES: &[&str; 2] = &["Ferris", "Bors"];
  10. }
  11. }