Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does creating a mutable reference to a dereferenced mutable reference work?

Tags:

rust

I understand you're not allowed to create two mutable references to an object at once in Rust. I don't entirely understand why the following code works:

fn main() {
    let mut string = String::from("test");
    let mutable_reference: &mut String = &mut string;
    mutable_reference.push_str(" test");
    // as I understand it, this creates a new mutable reference (2nd?)
    test(&mut *mutable_reference);

    println!("{}", mutable_reference);
}

fn test(s: &mut String) {
    s.push_str(" test");
}
like image 916
Programmer9000 Avatar asked Jul 14 '17 05:07

Programmer9000


People also ask

What is a mutable reference?

A mutable type is a type whose instance data can be modified. The System. Text. StringBuilder class is an example of a mutable reference type. It contains members that can change the value of an instance of the class.

How do references work in Rust?

In Rust, by contrast, the compiler guarantees that references will never be dangling references: if you have a reference to some data, the compiler will ensure that the data will not go out of scope before the reference to the data does.


1 Answers

The rule

There shall only be one usable mutable reference to a particular value at any point in time.

This is NOT a spatial exclusion (there CAN be multiple references to the same piece) but a temporal exclusion.

The mechanism

In order to enforce this, &mut T is NOT Copy; therefore calling:

test(mutable_reference);

should move the reference into test.

Actually doing this would make it unusable later on and not be very ergonomic, so the Rust compiler inserts an automatic reborrowing, much like you did yourself:

test(&mut *mutable_reference);

You can force the move if you wanted to:

test({ let x = mutable_reference; x });

The effect

Re-borrowing is, in essence, just borrowing:

  • mutable_reference is borrowed for as long as the unnamed temporary mutable reference exists (or anything that borrows from it),
  • the unnamed temporary mutable reference is moved into test,
  • at the of expression, the unnamed temporary mutable reference is destroyed, and therefore the borrow of mutable_reference ends.
like image 118
Matthieu M. Avatar answered Oct 21 '22 23:10

Matthieu M.