Rust Lifetimes

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Syntax

  • fn function<'a>(x: &'a Type)
  • struct Struct<'a> { x: &'a Type }
  • enum Enum<'a> { Variant(&'a Type) }
  • impl<'a> Struct<'a> { fn x<'a>(&self) -> &'a Type { self.x } }
  • impl<'a> Trait<'a> for Type
  • impl<'a> Trait for Type<'a>
  • fn function<F>(f: F) where for<'a> F: FnOnce(&'a Type)
  • struct Struct<F> where for<'a> F: FnOnce(&'a Type) { x: F }
  • enum Enum<F> where for<'a> F: FnOnce(&'a Type) { Variant(F) }
  • impl<F> Struct<F> where for<'a> F: FnOnce(&'a Type) { fn x(&self) -> &F { &self.x } }

Remarks

  • All references in Rust have a lifetime, even if they are not explicitly annotated. The compiler is capable of implicitly assigning lifetimes.
  • The 'static lifetime is assigned to references that are stored in the program binary and will be valid throughout its entire execution. This lifetime is most notably assigned to string literals, which have the type &'static str.


Got any Rust Question?