3

I have recently started learning Rust from The Book. While learning about ownership, I came up with following piece of code:

let mut s = String::from("hello");

let r1 = &mut s;

Here it confused me. Why are we not using mut keyword before r1 too? What if we use mut keyword? What will it signify?

Because I am still able to modify content with:

r1.push_str(" World!");
3
  • 3
    Try assigning to r1 again. And then try it after making it mut. You can think of those as kind of external and internal mutability. The way you have it now you can modify the string internally, but you cannot modify the variable. Commented May 15 at 5:48
  • I see. So if I don't add mut then I can't assign a different address to r1. But can alter the internal content of that address because it's a mutable refference. Thanks @freakish Commented May 15 at 6:04
  • 1
    &mut s makes r1 refer to something mutable, allowing you to do *r1 = some_other_string at a later point (as well as modifying it in-place with r1.push_str() and others.). mut r1 makes t1 itself mutable, allowing you to do r1 = reference_to_some_other_string at a later point. Commented May 15 at 8:07

1 Answer 1

1

A newcomer to Rust might stumble upon what could be considered a slightly unfortunate choice of words. Rust uses mut is several situations, and they are not the same:

  • We say "this is a immutable reference" (&T), or "this is a mutable reference" (&mut T). The choice of words is not exactly precise, and it does not help that the word mut even appears in the syntax. What we actually mean is "this is a shared reference" (&T), or "this is an exclusive reference" (&mut T). The confusion only becomes worse when the objects behind what we call "immutable references" are actually mutable (but let's not get into interior mutability here)...
    One of the core principles of Rust is that mutating something while aliasing it is a primary source of evil; so we don't allow it. If we can prove that no aliasing can possibly occur, mutation has to be harmless, because "mutating while aliasing" can't occur in that situation. This is the situation of an exclusive reference to something. If that reference is the only reference to a value, we are free to mutate that value. Unfortunately, we call that reference a "mutable reference" - because that's what it does, not what it is. Therefor it might be helpful to think of &mut T as "exclusive reference", not "mutable reference".
  • A place can be mutable or not. That is, let mut foo: Foo = ... declares that foo itself can change, while a let foo: Foo = ... says that foo itself can't change (but the value foo refers to can). This is basically bookkeeping within the syntax, in order to signal intend by the programmer; it does not reflect what either Foo or foo can actually do.
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for explanation @user2722968. It's easier to undestand when you look them as shared and exclusive references.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.