mirror of
https://github.com/Dhghomon/easy_rust
synced 2024-11-15 18:13:23 +00:00
deploy: cd320254fa
This commit is contained in:
parent
173feb9493
commit
5624bd1a5e
@ -164,6 +164,7 @@
|
||||
<div id="content" class="content">
|
||||
<main>
|
||||
<h2><a class="header" href="#mutable-references" id="mutable-references">Mutable references</a></h2>
|
||||
<p><strong><a href="https://youtu.be/G48z6Rv76vc">See this chapter on YouTube</a></strong></p>
|
||||
<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>
|
||||
<pre><pre class="playground"><code class="language-rust">fn main() {
|
||||
let mut my_number = 8; // don't forget to write mut here!
|
||||
|
@ -164,6 +164,7 @@
|
||||
<div id="content" class="content">
|
||||
<main>
|
||||
<h2><a class="header" href="#giving-references-to-functions" id="giving-references-to-functions">Giving references to functions</a></h2>
|
||||
<p><strong>See this chapter on YouTube: <a href="https://youtu.be/mKWXt9YTavc">immutable references</a> and <a href="https://youtu.be/kJV1wIvAbyk">mutable references</a></strong></p>
|
||||
<p>References are very useful for functions. The rule in Rust on values is: a value can only have one owner.</p>
|
||||
<p>This code will not work:</p>
|
||||
<pre><pre class="playground"><code class="language-rust">fn print_country(country_name: String) {
|
||||
|
@ -1221,6 +1221,7 @@ fn main() {
|
||||
<p>The function <code>return_str()</code> creates a String, then it creates a reference to the String. Then it tries to return the reference. But the String <code>country</code> only lives inside the function, and then it dies. Once a variable is gone, the computer will clean up the memory and use it for something else. So after the function is over, <code>country_ref</code> is referring to memory that is already gone, and that's not okay. Rust prevents us from making a mistake with memory here.</p>
|
||||
<p>This is the important part about the "owned" type that we talked about above. Because you own a <code>String</code>, you can pass it around. But a <code>&String</code> will die if its <code>String</code> dies, so you don't pass around "ownership" with it.</p>
|
||||
<h2><a class="header" href="#mutable-references" id="mutable-references">Mutable references</a></h2>
|
||||
<p><strong><a href="https://youtu.be/G48z6Rv76vc">See this chapter on YouTube</a></strong></p>
|
||||
<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>
|
||||
<pre><pre class="playground"><code class="language-rust">fn main() {
|
||||
let mut my_number = 8; // don't forget to write mut here!
|
||||
@ -1308,6 +1309,7 @@ Second_number = triple_reference? true
|
||||
}
|
||||
</code></pre></pre>
|
||||
<h2><a class="header" href="#giving-references-to-functions" id="giving-references-to-functions">Giving references to functions</a></h2>
|
||||
<p><strong>See this chapter on YouTube: <a href="https://youtu.be/mKWXt9YTavc">immutable references</a> and <a href="https://youtu.be/kJV1wIvAbyk">mutable references</a></strong></p>
|
||||
<p>References are very useful for functions. The rule in Rust on values is: a value can only have one owner.</p>
|
||||
<p>This code will not work:</p>
|
||||
<pre><pre class="playground"><code class="language-rust">fn print_country(country_name: String) {
|
||||
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user