<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><strong>See this chapter on YouTube: <ahref="https://youtu.be/mKWXt9YTavc">immutable references</a> and <ahref="https://youtu.be/kJV1wIvAbyk">mutable references</a></strong></p>
print_country(country); // We print "Austria"
print_country(country); // ⚠️ That was fun, let's do it again!
}
</code></pre></pre>
<p>It does not work because <code>country</code> is destroyed. Here's how:</p>
<ul>
<li>Step 1: We create the <code>String</code> called <code>country</code>. <code>country</code> is the owner.</li>
<li>Step 2: We give <code>country</code> to <code>print_country</code>. <code>print_country</code> doesn't have an <code>-></code>, so it doesn't return anything. After <code>print_country</code> finishes, our <code>String</code> is now dead.</li>
<li>Step 3: We try to give <code>country</code> to <code>print_country</code>, but we already did that. We don't have <code>country</code> to give anymore.</li>
</ul>
<p>We can make <code>print_country</code> give the <code>String</code> back, but it is a bit awkward.</p>
print_country(&country); // We print "Austria"
print_country(&country); // That was fun, let's do it again!
}
</code></pre></pre>
<p>Now <code>print_country()</code> is a function that takes a reference to a <code>String</code>: a <code>&String</code>. Also, we give it a reference to country by writing <code>&country</code>. This says "you can look at it, but I will keep it".</p>
<p>Now let's do something similar with a mutable reference. Here is an example of a function that uses a mutable variable.</p>
<pre><preclass="playground"><codeclass="language-rust">fn add_hungary(country_name: &mut String) { // first we say that the function takes a mutable reference
country_name.push_str("-Hungary"); // push_str() adds a &str to a String
println!("Now it says: {}", country_name);
}
fn main() {
let mut country = String::from("Austria");
add_hungary(&mut country); // we also need to give it a mutable reference.
}
</code></pre></pre>
<p>This prints <code>Now it says: Austria-Hungary</code>.</p>
<p>So to conclude:</p>
<ul>
<li><code>fn function_name(variable: String)</code> takes a <code>String</code> and owns it. If it doesn't return anything, then the variable dies inside the function.</li>
<li><code>fn function_name(variable: &String)</code> borrows a <code>String</code> and can look at it</li>
<li><code>fn function_name(variable: &mut String)</code> borrows a <code>String</code> and can change it</li>
</ul>
<p>Here is an example that looks like a mutable reference, but it is different.</p>
let country = String::from("Austria"); // country is not mutable, but we are going to print Austria-Hungary. How?
adds_hungary(country);
}
fn adds_hungary(mut country: String) { // Here's how: adds_hungary takes the String and declares it mutable!
country.push_str("-Hungary");
println!("{}", country);
}
</code></pre></pre>
<p>How is this possible? It is because <code>mut country</code> is not a reference: <code>adds_hungary</code> owns <code>country</code> now. (Remember, it takes <code>String</code> and not <code>&String</code>). The moment you call <code>adds_hungary</code>, it becomes the full owner. <code>country</code> has nothing to do with <code>String::from("Austria")</code> anymore. So <code>adds_hungary</code> can take <code>country</code> as mutable, and it is perfectly safe to do so.</p>
<p>Remember our employee Powerpoint and manager situation above? In this situation it is like the employee just giving his whole computer to the manager. The employee won't ever touch it again, so the manager can do anything he wants to it.</p>