Skip to content
Theory · Your First Program · lesson 5 of 7

Reading compiler errors

In the first lesson the compiler was a strict but friendly editor: it sends the text back with notes on what is wrong, where, and how to fix it. Now let's learn to read those notes.

Here is a program where the exclamation mark after println is missing:

Rust
fn main() {
    println("Hello, world!"); // ✗
}
TEXT
error[E0423]: cannot find function `println` in this scope

This is a compile error: the compiler refused to translate the text, so the program never ran. Without the !, the compiler looks for an ordinary function named println, and there is no such function: there is only the macro println!.

What a message consists of

The first line is only the beginning. In full, cargo run prints this message for the program:

TEXT
error[E0423]: cannot find function `println` in this scope
 --> src/main.rs:2:5
  |
2 |     println("Hello, world!");
  |     ^^^^^^^ not found in this scope
  |
  = note: a macro named `println` exists in another namespace
help: use `!` to invoke the macro
  |
2 |     println!("Hello, world!");
  |            +

For more information about this error, try `rustc --explain E0423`.
error: could not compile `hello` (bin "hello") due to 1 previous error

Let's go through it from top to bottom:

  1. error[E0423]: the word error means an error, and E0423 is its code. After the colon the compiler says what is wrong: there is no function println here.
  2. --> src/main.rs:2:5 says where: the file src/main.rs, line 2, character 5.
  3. The program line with the ^ signs under the word println shows the exact spot.
  4. = note: is an explanation: a macro with this name exists.
  5. help: says how to fix it: the fixed code is shown below, and + marks what to add.
  6. The last lines sum up: could not compile means the program could not be built.

All three of the editor's notes are in place: what is wrong in the first line, where after the --> arrow, and how to fix it in the help: hint.

The error code and rustc --explain

Many errors have a code such as E0423. The command rustc --explain E0423 prints a detailed description of the error with examples, and the Rust documentation has a page for every code. Not every error has a code: errors in the format string of println! come without one, with the word error:.

The first error first

Errors are fixed from top to bottom. One typo sometimes drags several messages behind it, and they disappear once the first one is fixed. Check the code again after each fix, fastest with the cargo check command.

Warnings

Besides errors, the compiler gives warnings. A warning does not stop the build, but it marks a suspicious spot. Here a variable is created and never read:

Rust
fn main() {
    let total = 10; // ✗
    println!("done");
}
TEXT
warning: unused variable: `total`

The program builds and prints done, but a warning often reveals a real slip: a value was computed and then forgotten. If that is intended, the help: hint in the full message suggests naming the variable _total. The course examples have no warnings: every variable is used.

Build or run

A compile error stops everything in advance: the program is not built, and not one of its lines runs, even a correct one. Some errors the compiler cannot see, because they depend on data the program gets only while it runs. Then the program builds and starts, and stops in the middle of its work with an error message. Such a stop is called a panic.

Rust tries to catch as many errors as possible at build time: fixing an error on your computer costs less than a crash on a user's computer.

Common mistake

The third argument is forgotten, and the number of {} placeholders does not match the number of arguments:

Rust
fn main() {
    let a = 2;
    let b = 3;
    println!("start");
    println!("{} + {} = {}", a, b); // ✗
}
TEXT
error: 3 positional arguments in format string, but there are 2 arguments

Let's see what happened here:

  1. println! is a macro, so the compiler matches the text in quotes against the arguments while it builds the program.
  2. Three {} need three arguments, but two are passed, and the build stops.
  3. The program does not run, so not even start from the line above is printed.

The fix is to pass the third argument: println!("{} + {} = {}", a, b, a + b);.

Interviewers like this question: why is println! a macro and not a function? The macro checks the format string while the program is built and takes any number of arguments. A function in Rust cannot do that: the number of its arguments is always the same.

Key points

  • A compile error stops the build: the program does not run, and not one of its lines runs.
  • A message answers three questions: what is wrong (error[E…]), where (-->) and how to fix it (help:).
  • Errors are fixed from top to bottom; a warning does not stop the build but often points to a slip.
  • println! matches the placeholders against the arguments at build time: each {} needs its own argument.