Rust Foundations · 10 min · free lesson

Reading rustc

A compiler diagnostic is a map back to the source: find its primary span, compare expected with found, and make one intentional fix before reading follow-up errors.

Core idea

Repair the first causal error before chasing its downstream messages.

Coming from C? Reading rustc Diagnostics covers this side by side. · Coming from Node.js? TypeScript types vs Rust types covers this side by side.

Start with the idea

When rustc rejects a program, begin at the highlighted source span. The message usually names what type was expected and what expression was actually found. In the example, count is annotated u32, while "four" is text; the compiler cannot turn that word into an unsigned integer by guessing. The primary span points to the conflicting value. Nearby labels and notes explain how the expectation was established, and a help message may suggest an operation. Treat suggestions as evidence, not commands: first decide what the program should mean.

Read the code

fn main() {
    let count: u32 = "four";
    println!("{count}");
}

The annotation sets the expected u32; the primary span marks the found &str.

Practice starter

let enabled: bool = 1;
Diagram for Reading rustc: a Rust code example flows to the lesson rule and the key observation.
The annotation sets the expected u32; the primary span marks the found &str.

Work through it

If count should be four, replace the text with the numeric literal 4. If the user really supplies text, parsing is a separate, fallible step that returns a Result and needs error handling. Those are different programs. Fix the earliest causal mismatch and compile again before chasing a long list of later messages; one root error can make downstream expressions appear broken. Keep the same reading order for any type mismatch: locate the span, compare types, choose a conversion only if it preserves intent, then recheck the build.

Compiler messages often include a code such as E0308 and a help suggestion. The code is useful for looking up the rule, while the span tells you where this particular program violates it. A suggestion to parse text may compile only after you handle the possibility of invalid input; adding unwrap just to silence a diagnostic changes the failure mode to a runtime panic. Prefer a fix you can explain in terms of the data the program actually has.

A compiler error is evidence about a particular source version. First confirm that the file you edited is the one being built. Then read the primary error message and its highlighted span before looking at follow-up notes. 'Expected' and 'found' describe a disagreement the compiler can point to, but the best fix may be earlier than the underlined token. One missing delimiter can produce many later diagnostics; repair the first meaningful error and rebuild before chasing the rest.

Do not treat a compiler suggestion as an instruction to apply mechanically. Ask what your program is meant to do. If Rust suggests borrowing instead of moving a value, decide whether the function truly needs ownership or only temporary access. If it suggests a conversion, decide what should happen when the input is out of range. Compiler feedback is strongest when combined with a prediction about the intended data flow.

Create three controlled errors: a misspelled variable, a wrong argument type, and a missing brace. For each, record the first error category, the line and column, and the smallest intentional edit that repairs the program. Rebuild after each fix. Compare the failed build with a successful run: diagnostics describe why a new program was not produced, while stdout comes from a program that did run. That distinction keeps the learning loop precise even when the interface shows compiler messages and output near one another.

Try it

Read the highlighted span, expected bool, and found integer before editing. Decide whether enabled should be a fixed true or false, or the result of a comparison; implement the smallest fix for that intent and recompile.

What success looks like: You can explain the mismatch in one sentence and show that the chosen change matches the program’s meaning, not merely that the compiler message disappeared.

Check your understanding

Which part of the diagnostic is the best starting point?

Reveal the answer
  1. The primary source span and expected/found pair
  2. The final warning in the file
  3. Any suggested cast, applied automatically

The primary span anchors the message to the expression that established the conflict.

One more experiment

Change "four" to 4.0 and write the expected and found types you predict before asking rustc. Then change it to 4 and predict what println! prints.

Learn this lesson with AI

Paste this into Claude, Codex, Cursor or another agent connected to Crabrix Academy. It contains only this lesson’s public details.

I'm learning Rust with Crabrix Academy. Teach me this lesson with the Crabrix Academy tools.
Lesson: Reading rustc (courseId "basics", lessonId "compiler-feedback")
Link: https://crabrix.com/learn/basics/compiler-feedback/
Goal: Repair the first causal error before chasing its downstream messages.
Practice: give me the lesson's exercise (crabrix_academy_get_exercise); I will write and run the code in Crabrix.
Explain it step by step for my level, let me write the code myself, give hints before any solution, and do not mark anything as passed: only Crabrix checks do that.

In Claude Code, Codex or Cursor, paste it into a chat with Crabrix Academy connected. On a phone, the Claude and ChatGPT apps open the same links.