Programs, source code and the compiler

Rust · Essentials · 6 min

Every later lesson writes code and builds it, so it helps to know what happens between typing text and running a program.

A program is a list of instructions for a computer, the way a recipe is a list of steps for a cook. The computer follows them exactly, one after another, and never guesses what you meant.

Programmers write those instructions as text in a programming language, such as Rust. That text is called source code. It is saved in ordinary text files; Rust files end in .rs.

The chip that actually carries out instructions, the processor (or CPU), can't read Rust. It only understands machine code: long lists of numbers that nobody wants to write by hand.

So Rust comes with a compiler: a program that translates your source code into machine code. That translation step is called compiling. The result is an executable, a file you can run as a program, just like any app on your computer.

While it translates, Rust's compiler also checks your code very strictly. If something is wrong, such as a typo or using a value in a way that could crash, it refuses to build the program and prints a compile error that explains the problem. You fix the code and try again.

That strictness is Rust's big idea. The moment the compiler checks and builds your code is called compile time; the moment the program actually runs is called runtime. Many mistakes that crash programs at runtime in other languages are caught at compile time in Rust, before any user sees them.

You give commands to Rust's tools in a terminal: a text window where you type a command, press Enter and read the reply. On macOS it is the Terminal app; on Windows, PowerShell. The next lesson uses it to create and run your first program.

Rust's error messages are unusually helpful. They point at the exact line, say what went wrong and often suggest the fix. Read them to the end: learning Rust is partly learning to have a conversation with the compiler.

Example

// src/main.rs: a complete Rust program (this text is source code)
fn main() {
    println!("Hello, world!");
}

// Compiling turns this text into an executable.
// Running the executable prints:
// Hello, world!

// A typo such as  printn!("Hi");  is caught at compile time:
// error: cannot find macro `printn` in this scope
//        help: a macro with a similar name exists: `println`

Remember

  • Source code is the text you write; the compiler translates it into an executable the computer can run
  • Rust's compiler checks your code strictly and stops with a compile error when something is wrong
  • Compile time is when code is checked and built; runtime is when the program runs

Watch out for

  • Skimming past a compile error: the full message usually tells you exactly what to change.

Check yourself

  1. What does a compiler do?
  2. Rust reports a mistake in your code before the program ever runs. When was it caught?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: doc.rust-lang.org/book/ch01-02-hello-world.html

Builds on

Nothing: this is a foundation.

Unlocks