liveBook · Manning

Compiler

As explained in Learn Rust in a Month of Lunches

A compiler is a program that translates source code into an executable or another lower-level representation while checking it against the language's rules.

A compiler checks code before it runs. Rust’s compiler is strict: code that breaks its rules will not run until you fix it. Its messages often point to the problem and suggest a change, so an error can be a useful next step rather than a dead end. section 1.1 section 1.1.2

Fix a String one message at a time

Suppose you create a String, add an exclamation mark, and print it. The first attempt has three problems: the print call needs a comma, the starting text needs conversion to a String, and `push` needs a character in single quotes. After those fixes, the compiler finds one more problem: the variable must be declared `mut` before it can be changed. section 1.1.2

error: expected `,`, found `my_name`
  |
4 |     println!("{}" my_name);
  |               ^^^^^^^ expected `,`
 
error[E0308]: mismatched types
 --> src/main.rs:2:27
  |
2 |     let my_name: String = "Dave";
  |                  ------   ^^^^^^- help: try using a conversion method:
                     `.to_string()`
  |                  |        |
  |                  |        expected struct `String`, found `&str`
  |                  expected due to this
 
error[E0308]: mismatched types
 --> src/main.rs:3:18
  |
3 |     my_name.push("!");
  |             ---- ^^^ expected `char`, found `&str`
  |             |
  |             arguments to this function are incorrect
  |
help: if you meant to write a `char` literal, use single quotes
  |
3 |     my_name.push('!');
  |                  ~~~

The first compiler messages identify three separate errors and suggest how to fix them. section 1.1.2

error[E0596]: cannot borrow `my_name` as mutable, as it is not declared as
mutable
 --> src/main.rs:3:5
  |
2 |     let my_name: String = "Dave".to_string();
  |         ------- help: consider changing this to be mutable: `mut
            my_name`
3 |     my_name.push('!');
  |     ^^^^^^^^^^^^^^^^^ cannot borrow as mutable

The next message asks for a mutable variable: adding to the String changes it. section 1.1.2

fn main() {
    let mut my_name: String = "Dave".to_string();
    my_name.push('!');
    println!("{}", my_name);
}

With `mut` added, the corrected program runs. section 1.1.2

Not every message means the program is broken. An unused-variable warning can appear even when code compiles and runs. The compiler also works out many variable types for you: `let my_number = 8` gets the default integer type `i32`, while `let my_number: u8 = 8` states a different choice. When it cannot determine the type—or you want another one—you can specify it. section 1.1.2 section 1.4

How Learn Rust in a Month of Lunches covers it

How deep it goes
Walks through successive errors and a warning, then explains basic type inference; does not examine compiler internals.
Examples
Uses a broken String update, successive corrections, an unused-variable warning, and integer type choices.
What it assumes
Introduces compiler feedback through short Rust examples and defines type inference in plain language.
What you can do after
Diagnose/debug with it by following compiler suggestions and distinguishing blocking errors from warnings.
How it connects
Links compiler feedback to type inference, mutability, compile-time errors, and runtime debugging.

Also explained in

Compare how 9 books explain compiler →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Rust in a Month of Lunches. The section links point to the parts of the book it draws on.