<buttonid="sidebar-toggle"class="icon-button"type="button"title="Toggle Table of Contents"aria-label="Toggle Table of Contents"aria-controls="sidebar">
<inputtype="search"id="searchbar"name="searchbar"placeholder="Search this book ..."aria-controls="searchresults-outer"aria-describedby="searchresults-header">
<p>Some types in Rust are very simple. They are called <strong>copy types</strong>. These simple types are all on the stack, and the compiler knows their size. That means that they are very easy to copy, so the compiler always copies when you send it to a function. It always copies because they are so small and easy that there is no reason not to copy. So you don't need to worry about ownership for these types.</p>
<p>These simple types include: integers, floats, booleans (<code>true</code> and <code>false</code>), and <code>char</code>.</p>
<p>How do you know if a type <strong>implements</strong> copy? (implements = can use) You can check the documentation. For example, here is the documentation for char:</p>
<p>On the left you can see <strong>Trait Implementations</strong>. You can see for example <strong>Copy</strong>, <strong>Debug</strong>, and <strong>Display</strong>. So you know that a <code>char</code>:</p>
<ul>
<li>is copied when you send it to a function (<strong>Copy</strong>)</li>
<li>can use <code>{}</code> to print (<strong>Display</strong>)</li>
<li>can use <code>{:?}</code> to print (<strong>Debug</strong>)</li>
</ul>
<pre><preclass="playground"><codeclass="language-rust">fn prints_number(number: i32) { // There is no -> so it's not returning anything
// If number was not copy type, it would take it
// and we couldn't use it again
println!("{}", number);
}
fn main() {
let my_number = 8;
prints_number(my_number); // Prints 8. prints_number gets a copy of my_number
prints_number(my_number); // Prints 8 again.
// No problem, because my_number is copy type!
}
</code></pre></pre>
<p>But if you look at the documentation for String, it is not copy type.</p>
<p>On the left in <strong>Trait Implementations</strong> you can look in alphabetical order. A, B, C... there is no <strong>Copy</strong> in C. But there is <strong>Clone</strong>. <strong>Clone</strong> is similar to <strong>Copy</strong>, but usually needs more memory. Also, you have to call it with <code>.clone()</code> - it won't clone just by itself.</p>
<p>In this example, <code>prints_country()</code> prints the country name, a <code>String</code>. We want to print it two times, but we can't:</p>
<pre><codeclass="language-text">error[E0382]: use of moved value: `country`
--> src\main.rs:4:20
|
2 | let country = String::from("Kiribati");
| ------- move occurs because `country` has type `std::string::String`, which does not implement the `Copy` trait
3 | prints_country(country);
| ------- value moved here
4 | prints_country(country);
| ^^^^^^^ value used here after move
</code></pre>
<p>The important part is <code>which does not implement the Copy trait</code>. But in the documentation we saw that String implements the <code>Clone</code> trait. So we can add <code>.clone()</code> to our code. This creates a clone, and we send the clone to the function. Now <code>country</code> is still alive, so we can use it.</p>
prints_country(country.clone()); // make a clone and give it to the function. Only the clone goes in, and country is still alive
prints_country(country);
}
</code></pre></pre>
<p>Of course, if the <code>String</code> is very large, <code>.clone()</code> can use a lot of memory. One <code>String</code> can be a whole book in length, and every time we call <code>.clone()</code> it will copy the book. So using <code>&</code> for a reference is faster, if you can. For example, this code pushes a <code>&str</code> onto a <code>String</code> and then makes a clone every time it gets used in a function:</p>
<pre><preclass="playground"><codeclass="language-rust">fn get_length(input: String) { // Takes ownership of a String
println!("It's {} words long.", input.split_whitespace().count()); // splits to count the number of words
}
fn main() {
let mut my_string = String::new();
for _ in 0..50 {
my_string.push_str("Here are some more words "); // push the words on
get_length(my_string.clone()); // gives it a clone every time
}
}
</code></pre></pre>
<p>It prints:</p>
<pre><codeclass="language-text">It's 5 words long.
It's 10 words long.
...
It's 250 words long.
</code></pre>
<p>That's 50 clones. Here it is using a reference instead, which is better:</p>
<p>A variable without a value is called an "uninitialized" variable. Uninitialized means "hasn't started yet". They are simple: just write <code>let</code> and the variable name:</p>
<p>You can see that <code>my_number</code> was declared in the <code>main()</code> function, so it lives until the end. But it gets its value from inside a loop. However, that value lives as long as <code>my_number</code>, because <code>my_number</code> has the value. And if you wrote <code>let my_number = loop_then_return(number)</code> inside the block, it would just die right away.</p>
<p>It helps to imagine if you simplify the code. <code>loop_then_return(number)</code> gives the result 100, so let's delete it and write <code>100</code> instead. Also, now we don't need <code>number</code> so we will delete it too. Now it looks like this:</p>
<p>Also note that <code>my_number</code> is not <code>mut</code>. We didn't give it a value until we gave it 50, so it never changed its value. In the end, the real code for <code>my_number</code> is just <code>let my_number = 100;</code>.</p>