Start with the idea
println! takes a format string and turns values into a line on stdout. In the example, {name} and {builds} capture bindings with those names. The macro checks the placeholders while compiling, so a misspelled capture or missing positional argument becomes a diagnostic before the program runs. This is safer than assembling a message with a chain of ad hoc string additions, especially when the output becomes a log line or a table someone else must read.
Read the code
let name = "Ferris";
let builds = 4;
println!("{name} ran {builds} builds");
println!("{builds:>3}");Names can be captured directly inside a checked format string.
Practice starter
let language = "Rust";
println!("Learning {language}");
Work through it
Formatting controls presentation without changing the stored value. In {builds:>3}, the colon introduces a format specification: > right-aligns the value in a field three characters wide. With builds = 4, that line begins with two spaces and then 4. Predict the whitespace rather than relying on how a proportional font looks. Try replacing a named capture with {} and pass the value explicitly; then remove the argument and observe how rustc identifies the mismatch. Use formatting to communicate, but keep business decisions outside the format string so the data and its display remain easy to reason about.
A placeholder also asks the value to support the requested formatting trait. Ordinary {} output uses Display, while {:?} is a Debug view often useful during inspection. If a custom type has no suitable implementation, rustc reports that requirement rather than inventing a representation. The width in :>3 is a minimum, not a maximum: a longer number is not truncated. Use that rule to predict both a one-digit and a four-digit count before running.
A format string is a template. Literal characters appear in output exactly as written, while each placeholder is replaced with a formatted value. {} consumes a positional argument; {name} can capture a named local binding. The compiler checks that placeholders and supplied arguments agree. A second {} without a second value is a compile-time mistake, not a blank that Rust fills automatically.
Formatting also changes how a value is represented. {value} uses Display, which aims at a reader-friendly form; {value:?} uses Debug, which is often useful while investigating program state. Some types implement one but not the other. println! appends a newline after formatting; print! does not. An escaped brace is written {{ or }} when you want a literal brace in the output. Spaces and punctuation inside the quotes are ordinary data, so write down the expected line character by character before running.
Build a tiny receipt line from a label and a number. First use positional placeholders, then use captured names. Add a literal pair of braces and compare the result. Next deliberately remove one argument and identify the compiler message before repairing it. For an unexpected output, distinguish three questions: did it compile, did the program run, and did the format string contain exactly the characters you intended? This habit prevents a surprising amount of debugging by guesswork.
Try it
Print a name and build count on one line, then right-align the count to width three on the next. Record the exact spaces you expect. Switch one capture to {} and deliberately omit its argument once.
What success looks like: stdout contains the expected words and alignment; the malformed positional call fails at compile time. You can explain which placeholder consumed each value.
Check your understanding
When is the format string checked?
Reveal the answer
- Only after the user sees stdout
- While println! expands during compilation
- Only when a placeholder is missing
println! is a macro, so malformed placeholders become compiler errors.
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: Printing Values (courseId "basics", lessonId "print-formatting") Link: https://crabrix.com/learn/basics/print-formatting/ Goal: Every formatting placeholder must have a value known to println!. Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=basics/print-formatting) 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.