Hello, compiler
Worked examples
Read the claim, then run it and check the machine agrees. One at a time — nothing here is taken on trust.
fn main() {
println!("Hello, compiler!");
let name = "Ferris";
let year = 2015;
println!("{name} shipped in {year}"); // inline capture
println!("{} + {} = {}", 2, 40, 2 + 40); // positional slots
println!("pi is roughly {:.2}", 3.14159); // spec: 2 decimals
}
Four println! calls, three slot styles. The macro validates every
one at compile time: delete the year argument's binding or add a
fourth {} and this stops being a program. {:.2} rounds for
display only — the f64 underneath keeps its full value.
fn main() {
let cold = -3;
let label = if cold < 0 { "freezing" } else { "fine" };
let y = {
let a = 2;
a * 21
};
println!("{label}, y = {y}");
}
No ternary operator, no temporary mutable variable waiting to be
assigned in two branches: if hands its branch's value straight
to let. The block computing y shows the same rule one level
down — its last expression, a * 21 with no semicolon, is the
block's value. Prints freezing, y = 42.
fn main() {
let x: i32 = 5; // annotation and value agree
println!("x = {x}");
// Uncomment to receive the letter:
// let y: i32 = "five";
//
// error[E0308]: mismatched types
// expected `i32`, found `&str`
// (and `--> file:line:col` points at the exact span)
}
Put the bad line back and the program never becomes a binary —
E0308 names both sides of the disagreement and flags the i32
annotation as the source of the expectation ("expected due to
this"). When a challenge in this course won't compile, that
letter appears as your feedback, word for word: read it before
touching the code.
Check the concept
One question at a time. Unsure? Revisit the lecture, then answer.
The challenge
Pass the quiz to unlock the challenge — your code will still be waiting here.