1.9Whitespace and basic formatting
Whitespace is the blank stuff: spaces, tabs, and newlines. This is a short lesson about how Rust treats it, what the standard style is, and why this topic, a reliable source of decades-long arguments elsewhere, is nearly a non-topic in Rust.
What the compiler cares about
Mostly, nothing. Rust reads code by its symbols, not its layout, so these two programs are identical to the compiler:
fn main() {
let x = 5;
println!("{x}");
}fn main(){let x=5;println!("{x}");}
The exceptions are the obvious ones. Whitespace separates words that would otherwise merge: let x can't be letx (that's just an identifier). And inside string literals, whitespace is data, kept exactly as typed; "a b" prints with both spaces.
The compiler's indifference is precisely why formatting matters for humans: the second program above compiles, and reading it feels like chewing foil. Layout is how code communicates structure to people, so every language community develops conventions about it. Most then argue about them forever.
The Rust style
Rust's conventions, used by the standard library, every major project, and this course: indent with 4 spaces (spaces, not tabs) for each level of nesting; opening braces on the same line as what they belong to; lines kept under 100 characters. There are finer points (where to break long expressions, how to wrap arguments), but you don't need to learn them, for the best possible reason.
cargo fmt, the argument ender
You met rustfmt in lesson 0.11. Now that you're writing multi-line programs, here's the habit: run cargo fmt in your project, and every .rs file in it is rewritten into the standard style. Feed it the foil-chewing version:
fn main(){let x=5;println!("{x}");}cargo fmtfn main() {
let x = 5;
println!("{x}");
}
Every fine point handled, instantly, the same way everyone else's code is formatted. This is the punchline the C++ world can't tell: there, formatting style is per-project, configurable, and argued about; here, the community shipped one style, one tool, and moved on. The brace-placement debate isn't settled in Rust so much as extinct.
Best practice
Run cargo fmt before you consider any piece of work finished. Better: most editors will run it on every save (in VS Code, enable "Format on Save"), after which formatting stops being a thing you do and becomes a thing that happens.
Tip
Type your code roughly right (it's easier to read while you write it), but don't fuss over alignment mid-thought. The formatter exists exactly so your attention can stay on what the code does.
Quiz time
Question #1
Does this program compile, and how would a Rust programmer feel about it?
fn main()
{
let total=1+2 ;
println!("{total}");
}Show solution
It compiles fine; the compiler doesn't care about the layout. A Rust programmer would wince at the dangling brace, the wandering indentation, and the spaced-out semicolon, then run cargo fmt and never speak of it again.
Question #2
Where does whitespace not get ignored?
Show solution
Between words that would otherwise merge into one (let x, not letx), and inside string literals, where every space is part of the data.
That's the look of the code settled. Now back to its substance: a closer look at the values you type into programs, and the operators that combine them.