Standards & Conventions 📏
Code is read far more often than it is written. The industry's conventions exist so that any programmer can open any project and feel at home. Learn them early and your code looks professional from day one.
Why conventions matter
Imagine a library where every shelf used a different sorting system. That is a codebase without conventions. Conventions are not bureaucracy; they are kindness to the next reader, and the next reader is usually you, three months from now, at midnight, hunting a bug.
Naming things 🏷️
Different languages chose different casing styles, and each community treats its choice as law. Rust's rules:
| Thing | Style | Example |
|---|---|---|
| variables & functions | snake_case | player_score, calculate_tip |
| types (structs, enums, traits) | UpperCamelCase | PlayerScore, TrafficLight |
| constants | SCREAMING_SNAKE_CASE | MAX_PLAYERS |
| crates & files | snake_case or kebab-case | my_game, rusty-school |
Beyond casing: names should say what a thing is or does.
days_until_launch beats d. The compiler does not
care. Humans care enormously.
Formatting: let the robot do it 🤖
Where do braces go? Tabs or spaces? Programmers wasted decades arguing. The modern answer: an auto-formatter rewrites your code into the community's one agreed style, and everyone moves on with their lives. In Rust:
cargo fmt # reformat the whole project, perfectly, in a second
cargo clippy # the linter: flags working-but-unidiomatic code and explains better ways
A linter like clippy goes beyond layout: it spots patterns
that work but smell ("you wrote a loop where .sum() would do").
Treat clippy as a free senior colleague. Run both before every commit and your
code reviews will be about ideas, never about commas.
Semantic versioning: what 1.4.2 means 🔢
Software versions look like MAJOR.MINOR.PATCH, and the numbers
are a promise called semver:
- PATCH (1.4.2 → 1.4.3): bug fixes only. Safe to update, always.
- MINOR (1.4.2 → 1.5.0): new features, nothing existing breaks.
- MAJOR (1.4.2 → 2.0.0): breaking changes. Read the notes before updating.
A 0.x version means "still finding its shape, anything may
change." When Cargo lists rand = "0.9" in
Cargo.toml, this grammar is what the numbers speak. Rust itself
takes the promise unusually seriously: code that compiled on Rust 1.0 in 2015
still compiles today.
Documentation: the README and friends 📖
Every project's front door is a README file: what this is,
how to run it, how to help. Beyond that, Rust has documentation comments:
write /// above a function and cargo doc builds a
documentation website from your comments automatically. The beautiful std
library docs you will use daily are generated exactly this way. Rule of thumb:
document why, let the code say what.
Licenses: the legal layer of open source ⚖️
Public code is not automatically free to use; the license file grants permission. You mostly need to recognize three families:
- MIT / Apache-2.0: "use this for almost anything, keep the credit notice." Business-friendly. The Rust ecosystem's default is dual-licensing under both.
- GPL: "use freely, but derivative software must also share its source." Powers Linux.
- No license file: legally means all rights reserved. Look but do not touch.
The three-letter philosophies 🧭
You will hear these acronyms in every code review of your life:
- DRY (Don't Repeat Yourself): if you paste the same code a third time, extract a function. One place to fix, not five.
- KISS (Keep It Simple): the clever one-liner loses to the boring five-liner everyone understands. Simple survives.
- YAGNI (You Aren't Gonna Need It): build what today needs, not what tomorrow might. Speculative features are where projects go to die.
Fix the names
These compile but break Rust convention. Correct each one:
let PlayerHealth = 100; ·
fn CheckCollision() {} ·
struct game_state {} ·
let max_retries_constant = 3; (it never changes)
Reveal solutions
let player_health = 100; ·
fn check_collision() {} ·
struct GameState {} ·
const MAX_RETRIES: u32 = 3;. Bonus: the Rust compiler actually
warns about most of these. Convention is built into the tools.
Read the version tea leaves
Your project depends on serde 1.0.219. For each new release,
decide: update casually, or read the changelog first?
(a) 1.0.220 · (b) 1.1.0 · (c) 2.0.0 · (d) a different crate at 0.3.1 → 0.4.0
Reveal answers
(a) patch: update casually. (b) minor: safe, skim the new features for fun. (c) major: read the migration notes before touching it. (d) 0.x minor bumps may break by convention: treat like a major, read first.
Audit a real project
Open any popular Rust repo on GitHub (try rust-lang/rustlings).
Find its README, its LICENSE (which family?), its version number, and one
/// doc comment in the source. Five minutes of detective work.
Reveal what you will find
Rustlings: a thorough README with setup steps, dual MIT/Apache-2.0
licensing (the Rust custom), a semver version in Cargo.toml,
and doc comments throughout the source. Every convention from this lesson,
live in the wild.