Dangling References

Rust will statically forbid dangling references:

  1. fn main() {
  2. let ref_x: &i32;
  3. {
  4. let x: i32 = 10;
  5. ref_x = &x;
  6. }
  7. println!("ref_x: {ref_x}");
  8. }
  • A reference is said to “borrow” the value it refers to.
  • Rust is tracking the lifetimes of all references to ensure they live long enough.
  • We will talk more about borrowing when we get to ownership.