<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><code>rustc</code> means Rust compiler, and it's what does the actual compiling. A rust file ends with an <code>.rs</code>. But most people don't write something like <code>rustc main.rs</code> to compile. They use something called <code>cargo</code>, which is the main package manager for Rust.</p>
<p>One note about the name: it's called <code>cargo</code> because when you put crates together, you get cargo. A crate is a wooden box that you see on ships or trucks, but you remember that every Rust project is also called a crate. Then when you put them together you get the whole cargo.</p>
<p>You can see this when you use cargo to run a project. Let's try something simple with <code>rand</code>: we'll just randomly choose between eight letters.</p>
<pre><preclass="playground"><codeclass="language-rust">use rand::seq::SliceRandom; // Use this for .choose over slices
<p>This will print something like <code>b c g h e a</code>. But we want to see what <code>cargo</code> does first. To use <code>cargo</code> and run our program, usually we type <code>cargo run</code>. This will build our program and run it for us. But when it starts compiling, it does something like this:</p>
<p>So it looks like it didn't just bring in <code>rand</code>, but some others too. That's because we need <code>rand</code> for our crate, but <code>rand</code> also has some code that needs other crates too. So <code>cargo</code> will find all the crates we need and put them together. In our case we only had seven, but on very big projects you may have 200 or more crates to bring in.</p>
<p>This is where you can see the tradeoff for Rust. Rust is extremely fast, because it compiles ahead of time. It does this by looking through the code and looking to see what the code you write actually does. For example, you might write this generic code:</p>
fn print_and_return_thing<T: Display>(input: T) -> T {
println!("You gave me {} and now I will give it back.", input);
input
}
fn main() {
let my_name = print_and_return_thing("Windy");
let small_number = print_and_return_thing(9.0);
}
</code></pre></pre>
<p>This function can take anything with <code>Display</code>, so we gave it a <code>&str</code> and next gave it a <code>f64</code> and that is no problem for us. But the compiler doesn't look at generics, because it doesn't want to do anything at runtime. It wants to put together a program that can run by itself as fast as possible. So when it looks at the first part with <code>"Windy"</code>, it doesn't see <code>fn print_and_return_thing<T: Display>(input: T) -> T</code>. It sees something like <code>fn print_and_return_thing(input: &str) ->&str</code>. And next it sees <code>fn print_and_return_thing(input: f64) -> f64</code>. All the checking about traits and so on is done during compile time. That's why generics take longer to compile, because it needs to figure them out, and make it concrete.</p>
<p>One more thing: Rust in 2020 is working hard on compile time, because this part takes the longest. Every version of Rust is a little bit faster at compiling, and there are some other plans to speed it up. But in the meantime, here's what you should know:</p>
<ul>
<li><code>cargo build</code> will build your program so you can run it</li>
<li><code>cargo run</code> will build your program and run it</li>
<li><code>cargo build --release</code> and <code>cargo run --release</code> will do the same but in release mode. What's that? Release mode is for when your code is finally done. Then Rust will take even longer to compile, but it does this because it uses everything it knows to make it faster. Release mode is actually a <em>lot</em> faster than the regular mode, which is called debug mode. That's because it compiles quicker and has more debug information. The regular <code>cargo build</code> is called a "debug build" and <code>cargo build --release</code> is called a "release build".</li>
<li><code>cargo check</code> is a way to check your code. It's like compiling except that it won't actually make your program. This is a good way to check your code a lot because it doesn't take as long as <code>build</code> or <code>run</code>.</li>
</ul>
<p>By the way, the <code>--release</code> part of the command is called a <code>flag</code>. That means extra information in a command.</p>
<p>Some other things you need to know are:</p>
<ul>
<li><code>cargo new</code>. You do this to create a new Rust project. After <code>new</code>, write the name of the project and <code>cargo</code> will make the folder and all the files you need.</li>
<li><code>cargo clean</code>. When you add crates to <code>Cargo.toml</code>, the computer will download all the files it needs and they can take a lot of space. If you don't want them on your computer anymore, type <code>cargo clean</code>.</li>
<p>One more thing about the compiler: it only takes the most time when you use <code>cargo build</code> or <code>cargo run</code> the first time. After that it will remember, and it will compile fast again. But if you use <code>cargo clean</code> and then run <code>cargo build</code>, it will have to compile slowly one more time.</p>