C and Rust, side by side
-Wall warns only about count; the use after free compiles silently and is found at run time by AddressSanitizer.#include <stdio.h>
#include <stdlib.h>
/* Sums the readings and frees them: the caller must not touch them again. */
static int total(int *values, int n) {
int sum = 0;
for (int i = 0; i < n; i++) sum += values[i];
free(values);
return sum;
}
int main(int argc, char **argv) {
(void)argv;
int *readings = malloc(3 * sizeof *readings);
if (!readings) return 1;
readings[0] = 3; readings[1] = 4; readings[2] = 5;
int count;
if (argc > 0) count = 3; /* -Wall: may be used uninitialized */
int sum = total(readings, count);
printf("%d from %d, first %d\n", sum, count, readings[0]); /* no warning */
return 0;
}
clone help: total borrows a slice, so readings is still usable afterwards.// The fix the diagnostic's note points to: borrow instead of taking ownership.
fn total(values: &[i32]) -> i32 {
values.iter().sum()
}
fn main() {
let readings = vec![3, 4, 5];
let sum = total(&readings);
println!("{sum} from {} readings, first {}", readings.len(), readings[0]);
}
| C | Rust |
|---|---|
A use after free compiles without a diagnostic, even with -Wall -Wextra. | A use after move is error E0382, with spans for where the value moved and where it was used. |
-Wall warns about a possibly uninitialised variable; -Werror makes it fatal. | Reading a possibly uninitialised binding is always error E0381. |
| Warning flags are looked up in the compiler manual. | rustc --explain E0382 prints the rule with examples, offline. |
-Werror promotes warnings to errors. | #![deny(warnings)] or -D warnings does the same for rustc lints. |
Understand it
In C the compiler's main job is translation, and whole classes of bugs produce no diagnostic at all. The example's use after free compiles silently even with -Wall -Wextra; the only warning is about count, which may be used uninitialised. -Werror turns that warning into a build failure, and the use after free surfaces only at run time, as heap-use-after-free under AddressSanitizer.
rustc refuses the equivalent program instead, and its diagnostic has a fixed anatomy. The first line gives the severity, the error code and the rule: error[E0382]: borrow of moved value. The --> line gives the file, line and column. Under the source, ^^^ marks the primary span, the place where the rule is broken (value borrowed here after move), and --- marks secondary spans that explain why: where the value moved, and that Vec<i32> does not implement Copy. note: lines add context, here pointing at the parameter that takes ownership, and help: lines propose a change.
Read in that order and fix the first error first. One root mistake can produce several later errors, so recompile after each fix before reading the rest. Treat help: as evidence, not a command: consider cloning the value would compile, but the note's suggestion to borrow, values: &[i32], matches what the program means and copies nothing. For any code, rustc --explain E0382 prints the rule with examples, offline.
Errors and warnings are separate in Rust. An error, such as a move, a type mismatch or a possibly uninitialised read (E0381), means no program is built; there is no flag that lets it through. Lints such as unused variables are warnings by default, and #![deny(warnings)] in the crate, or RUSTFLAGS="-D warnings" in CI, is the equivalent of -Werror. cargo clippy adds further lints, much as extra -W flags do for clang.
The C habit, written in Rust
This is how the C version usually looks on a first attempt in Rust. Predict what rustc says before you read the explanation.
fn total(values: Vec<i32>) -> i32 {
values.iter().sum()
}
fn main() {
let readings = vec![3, 4, 5];
let sum = total(readings);
println!("{sum} from {} readings", readings.len());
}
Try it
Compile the practice program, label each part of the diagnostic (code, primary span, secondary spans, note, help), then choose between the clone help and the borrowing note.
What success looks like: You can point to the ^^^ span that breaks the rule, the --- span that explains it, and explain why borrowing is a better fix than cloning here.
Check your understanding
After one edit, rustc reports three errors: E0308 at a call to total, then two more in later lines that use its result. What do you do first?
Reveal the answer
- Fix the first error at its primary span, recompile, and then see which later errors remain
- Fix the last error first, because it is closest to the end of
main - Apply every
help:suggestion in the order printed - Silence the errors with
#[allow(...)]attributes
Later errors are often consequences of the first. Its primary span shows where the rule breaks; fix that cause, rebuild, and only then read what is left.
Go deeper
Common misconception: “The help: line is the fix; apply it and move on.”
A help: line is one change that would compile. It may cost a copy (clone) or turn a compile error into a run-time panic (unwrap). Decide what the program should mean first; the notes often point to a better fix.
- Read the header: Note the severity, the error code and the one-line rule, such as
error[E0382]: borrow of moved value. - Follow the spans: Start at
^^^, then read each---label to see where the value came from and why the rule applies. - Weigh the help: Compare each
help:andnote:with what the program should mean; choose the fix you can explain, then recompile.
Transfer challenge: Run rustc --explain E0502 and rustc --explain E0308, then write a two-line program that triggers each and match its spans to the explanation.
Connections
- What Safe Rust Rules Out: Undefined behaviour excluded, bugs still possible
- From printf to println!: Format strings checked at compile time

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 Diagnostics (courseId "rust-for-c-developers", lessonId "c-rustc-diagnostics") Link: https://crabrix.com/learn/rust-for-c-developers/c-rustc-diagnostics/ Goal: Repair the first causal error before chasing its downstream messages: read its code and primary span, then the labels, notes and help, fix the cause and recompile. Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=rust-for-c-developers/c-rustc-diagnostics) and I will write the code there. 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.