Skip to chapter content
navan✳THE BUILD LABRUST / GUIDE
0/26 read
APPENDIX A / E · REFERENCE NOTES

Appendix A · Keywords and names.

Understand why some words shape Rust syntax and how names communicate intent.

BY NAVAN SAMILONG READRUST 2024THEORY FIRST
01 / THE MENTAL MODEL

The idea behind the syntax.

Keywords are words reserved by the language grammar. let starts a binding, fn introduces a function, match selects a pattern, and impl associates behavior with a type or trait. Their meaning comes from the position they occupy in a sentence of code. Learning a keyword list is less useful than learning the grammar around each one: what must follow it, what type of value it introduces, and which scope it affects.

Names are chosen by programmers. Rust commonly uses snake_case for variables, functions, and modules, and UpperCamelCase for types and traits. A name can be brought into scope through a let binding, function parameter, use declaration, or pattern. Two names may refer to related values at different times, but ownership and scope decide which one is actually usable at a given line.

02 / THE RULES

What Rust is checking.

Some words are strict keywords and cannot be ordinary identifiers. A raw identifier such as r#type can name something that would otherwise conflict with a keyword, often when interacting with a foreign API. This is an escape hatch for naming, not a way to change the language rule. Edition changes can reserve additional words while preserving compatibility through explicit edition settings and migration tooling.

Read a declaration by separating the role of each token. In let mut count: u32 = 0, let starts the binding, mut permits reassignment, count is the chosen name, u32 is the type, and 0 is the initial value. The compiler checks the grammar and type; the name should tell a human why the value exists. The examples show keywords in ordinary statements, function boundaries, and conditionals.

03 / CODE, EXPLAINED

See the rule in context.

Each example isolates one part of the model. Follow the bindings, types, ownership transfers, and possible outcomes line by line.

EXAMPLE 01

Variables, mutability & shadowing

A let binding is immutable unless you opt into mut. Mutation changes the value in an existing binding, so its type stays fixed. Shadowing creates another binding with the same name and can change the type. This is useful when turning raw input into a validated representation. A const is a named compile-time value that needs an explicit type. Prefer the least changeable representation that fits the work: immutable values are easier to reason about, while a small local mutable accumulator is often exactly right.

variables.rs
const MAX_RETRIES: u32 = 3;
fn main() {
    let mut attempts = 1;
    attempts += 1;
    let input = " 42 ";
    let input: u32 = input.trim().parse().expect("valid number");
    println!("attempts={attempts}/{MAX_RETRIES}, input={input}");
}
OUTPUT
attempts=2/3, input=42
  1. MAX_RETRIES has a type and a constant initializer.
  2. mut allows attempts to be updated in place.
  3. The second input shadows a string slice with a parsed u32.

The boundary: Do not add mut just to silence an unrelated error. Decide which value is supposed to change.

EXAMPLE 02

Functions & expressions

Function signatures describe the contract between caller and callee. Parameter types and non-unit return types are written explicitly. Most Rust blocks are expressions: their final expression becomes the value if it has no trailing semicolon. Statements perform actions; a semicolon can turn a value-producing expression into a statement. Keep a function focused enough to test with ordinary values. Prefer returning a result to printing inside low-level logic, so callers can decide how to display it.

functions.rs
fn total(price: u32, quantity: u32) -> u32 {
    price * quantity
}
fn main() {
    let invoice = {
        let shipping = 5;
        total(12, 3) + shipping
    };
    println!("total={invoice}");
}
OUTPUT
total=41
  1. total returns its final multiplication expression.
  2. The inner block computes a value using a local shipping binding.
  3. The example uses small values; production arithmetic still needs bounds or checked operations.

The boundary: A semicolon after the intended final return expression changes the block to return ().

EXAMPLE 03

if, else & boolean logic

An if condition must be a bool; numbers do not convert to truthiness automatically. An if/else can produce a value, provided both branches have compatible types. Logical && and || short-circuit, so the second operand is evaluated only when needed. This lets a guard protect an operation such as division. State conditions in terms of your domain rather than clever punctuation, and test the boundary where the decision changes.

conditions.rs
fn main() {
    let age = 19;
    let status = if age >= 18 { "adult" } else { "minor" };
    let denominator = 0;
    let safe = denominator != 0 && 10 / denominator > 1;
    println!("{status}, safe={safe}");
}
OUTPUT
adult, safe=false
  1. The two status branches both return string slices.
  2. The false first operand prevents division by zero.
  3. Test age 17 and 18 to exercise the boundary.

The boundary: Use parentheses to clarify mixed && and || expressions when the business rule could be misread.

04 / THE CONNECTION

Keep this model.

Keywords give Rust its grammar. Good identifiers give a reader the meaning that grammar alone cannot supply.

Finished reading?
Built with curiosity. By Navan Sami. ↗Rust · The rules behind the code.