---
title: "1.9 Whitespace and basic formatting"
description: "How Rust treats whitespace, the standard 4-space style, and how cargo fmt ended the formatting wars."
url: "https://learnrust.net/chapter-1/whitespace-and-formatting/"
last_updated: "2026-06-11"
---

# 1.9 Whitespace 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:

```rust
fn main() {
    let x = 5;
    println!("{x}");
}
```

```rust
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](https://learnrust.net/chapter-0/warnings-lints-and-clippy/). 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:

```rust
fn main(){let x=5;println!("{x}");}
```

```bash
cargo fmt
```

```rust
fn 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?

```rust
fn main()
{
        let total=1+2   ;
    println!("{total}");
}
```

<details class="solution">
<summary>Show solution</summary>

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.

</details>

**Question #2**

Where does whitespace *not* get ignored?

<details class="solution">
<summary>Show solution</summary>

Between words that would otherwise merge into one (`let x`, not `letx`), and inside string literals, where every space is part of the data.

</details>

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.

## Sitemap

See the full [sitemap](https://learnrust.net/sitemap.md) for all pages.
