References

There are two kinds of reference:

  • Shared reference: &
  • Mutable reference: &mut

Which obey the following rules:

  • A reference cannot outlive its referent
  • A mutable reference cannot be aliased

That’s it. That’s the whole model references follow.

Of course, we should probably define what aliased means.

  1. error[E0425]: cannot find value `aliased` in this scope
  2. --> <rust.rs>:2:20
  3. |
  4. 2 | println!("{}", aliased);
  5. | ^^^^^^^ not found in this scope
  6. error: aborting due to previous error

Unfortunately, Rust hasn’t actually defined its aliasing model. ?

While we wait for the Rust devs to specify the semantics of their language,let’s use the next section to discuss what aliasing is in general, and why itmatters.