What is rustc?

Welcome to "The rustc book"! rustc is the compiler for the Rust programminglanguage, provided by the project itself. Compilers take your source code andproduce binary code, either as a library or executable.

Most Rust programmers don't invoke rustc directly, but instead do it throughCargo. It's all in service of rustc though! If youwant to see how Cargo calls rustc, you can

  1. $ cargo build --verbose

And it will print out each rustc invocation. This book can help youunderstand what each of these options does. Additionally, while mostRustaceans use Cargo, not all do: sometimes they integrate rustc into otherbuild systems. This book should provide a guide to all of the options you'dneed to do so.

Basic usage

Let's say you've got a little hello world program in a file hello.rs:

  1. fn main() {
  2.     println!("Hello, world!");
  3. }

To turn this source code into an executable, you can use rustc:

  1. $ rustc hello.rs
  2. $ ./hello # on a *NIX
  3. $ .\hello.exe # on Windows

Note that we only ever pass rustc the crate root, not every file we wishto compile. For example, if we had a main.rs that looked like this:

  1. mod foo;
  2. fn main() {
  3. foo::hello();
  4. }

And a foo.rs that had this:

  1. pub fn hello() {
  2. println!("Hello, world!");
  3. }

To compile this, we'd run this command:

  1. $ rustc main.rs

No need to tell rustc about foo.rs; the mod statements give iteverything that it needs. This is different than how you would use a Ccompiler, where you invoke the compiler on each file, and then linkeverything together. In other words, the crate is a translation unit, not aparticular module.