<buttonid="sidebar-toggle"class="icon-button"type="button"title="Toggle Table of Contents"aria-label="Toggle Table of Contents"aria-controls="sidebar">
<ahref="print.html"title="Print this book"aria-label="Print this book">
<iid="print-button"class="fa fa-print"></i>
</a>
</div>
</div>
<divid="search-wrapper"class="hidden">
<formid="searchbar-outer"class="searchbar-outer">
<inputtype="search"name="search"id="searchbar"name="searchbar"placeholder="Search this book ..."aria-controls="searchresults-outer"aria-describedby="searchresults-header">
<p>If you want to use a reference to change data, you can use a mutable reference. For a mutable reference, you write <code>&mut</code> instead of <code>&</code>.</p>
let mut my_number = 8; // don't forget to write mut here!
let num_ref = &mut my_number;
}
</code></pre></pre>
<p>So what are the two types? <code>my_number</code> is an <code>i32</code>, and <code>num_ref</code> is <code>&mut i32</code> (we say a "mutable reference to an <code>i32</code>").</p>
<p>So let's use it to add 10 to my_number. But you can't write <code>num_ref += 10</code>, because <code>num_ref</code> is not the <code>i32</code> value, it is a <code>&i32</code>. The value is actually inside the <code>i32</code>. To reach the place where the value is, we use <code>*</code>. <code>*</code> means "I don't want the reference, I want the value behind the reference". In other words, one <code>*</code> is the opposite of <code>&</code>. Also, one <code>*</code> erases one <code>&</code>.</p>
<p>Because using <code>&</code> is called "referencing", using <code>*</code> is called "<strong>de</strong>referencing".</p>
<p>Rust has two rules for mutable and immutable references. They are very important, but also easy to remember because they make sense.</p>
<ul>
<li><strong>Rule 1</strong>: If you have only immutable references, you can have as many as you want. 1 is fine, 3 is fine, 1000 is fine. No problem.</li>
<li><strong>Rule 2</strong>: If you have a mutable reference, you can only have one. Also, you can't have an immutable reference <strong>and</strong> a mutable reference together.</li>
</ul>
<p>This is because mutable references can change the data. You could get problems if you change the data when other references are reading it.</p>
<p>A good way to understand is to think of a Powerpoint presentation.</p>
<p>Situation one is about <strong>only one mutable reference</strong>.</p>
<p>Situation one: An employee is writing a Powerpoint presentation. He wants his manager to help him. The employee gives his login information to his manager, and asks him to help by making edits. Now the manager has a "mutable reference" to the employee's presentation. The manager can make any changes he wants, and give the computer back later. This is fine, because nobody else is looking at the presentation.</p>
<p>Situation two is about <strong>only immutable references</strong>.</p>
<p>Situation two: The employee is giving the presentation to 100 people. All 100 people can now see the employee's data. They all have an "immutable reference" to the employee's presentation. This is fine, because they can see it but nobody can change the data.</p>
<p>Situation three is <strong>the problem situation</strong>.</p>
<p>Situation three: The Employee gives his manager his login information. His manager now has a "mutable reference". Then the employee went to give the presentation to 100 people, but the manager can still login. This is not fine, because the manager can log in and do anything. Maybe his manager will log into the computer and start typing an email to his mother! Now the 100 people have to watch the manager write an email to his mother instead of the presentation. That's not what they expected to see.</p>
<p>Here is an example of a mutable borrow with an immutable borrow:</p>
let number_change = &mut number; // create a mutable reference
*number_change += 10; // use mutable reference to add 10
let number_ref = &number; // create an immutable reference
println!("{}", number_ref); // print the immutable reference
}
</code></pre></pre>
<p>It prints <code>20</code> with no problem. It works because the compiler is smart enough to understand our code. It knows that we used <code>number_change</code> to change <code>number</code>, but didn't use it again. So here there is no problem. We are not using immutable and mutable references together.</p>
<p>Earlier in Rust this kind of code actually generated an error, but the compiler is smarter now. It can understand not just what we type, but how we use everything.</p>
<p>Remember when we said that shadowing doesn't <strong>destroy</strong> a value but <strong>blocks</strong> it? Now we can use references to see this.</p>
<p>Does this print <code>Austria, 8</code> or <code>8, 8</code>? It prints <code>Austria, 8</code>. First we declare a <code>String</code> called <code>country</code>. Then we create a reference <code>country_ref</code> to this string. Then we shadow country with 8, which is an <code>i32</code>. But the first <code>country</code> was not destroyed, so <code>country_ref</code> still says "Austria", not "8". Here is the same code with some comments to show how it works:</p>