Familiar syntax, different defaults · 8 min · free lesson

const by Default

In Rust a binding is immutable unless you write mut, and a new let can reuse a name for a new value or even a new type.

Core idea

let bindings are immutable by default; mut opts in to mutation, and shadowing with a new let creates a new binding instead of changing the old one.

C and Rust, side by side

C C mutates by default, needs a new name per parsing stage, and its const means read-only rather than constant.
#include <stdio.h>
#include <stdlib.h>

int main(void) {
    const int limit = 3;            /* read-only, but not a constant expression */
    int total = 0;                  /* writable unless you say otherwise */
    const char *raw_width = " 42 "; /* each parsing stage needs its own name */
    long width = strtol(raw_width, NULL, 10);
    for (int i = 0; i < limit; i++) {
        total += (int)width;
    }
    printf("limit=%d total=%d\n", limit, total);
    return 0;
}
Rust width is shadowed twice, from &str to a trimmed &str to a u32; only total needs mut.
const LIMIT: u32 = 3; // a typed compile-time constant

fn main() {
    let width = " 42 ";
    let width = width.trim(); // shadowing: a new binding, same name
    let width: u32 = width.parse().expect("a number"); // and now a u32
    let mut total = 0; // mutation is opt-in
    for _ in 0..LIMIT {
        total += width;
    }
    println!("limit={LIMIT} total={total}");
}
What changes
CRust
Objects are writable unless declared const.Bindings are immutable unless declared mut; reassignment is error E0384.
const int means read-only, not a compile-time constant.const is a typed compile-time constant; static has one address.
Each processing stage needs a new variable name.A new let can shadow the name, even with a different type.

Understand it

In C every object is writable unless you add const, and const means read-only through this name, not constant: const int limit = 3 is not an integer constant expression, and a const T * parameter says nothing about whether someone else changes the object. Rust flips the default. let total = 0; can never be reassigned; trying is error E0384. Write let mut total = 0; when the value really changes.

Immutability belongs to the binding, which makes it cheap to check and useful to read: a binding without mut names the same value for its whole scope. Mutable state stands out because every mutable binding is marked, and rustc warns when a mut turns out to be unnecessary.

Shadowing has no C equivalent. let width = width.trim(); declares a new binding called width that hides the previous one, and it may have a different type. C code usually invents a name per stage (raw_width, trimmed, width_value); Rust code often reuses one, because the earlier stage can then no longer be used by mistake. Shadowing is not mutation: the old value is unchanged, and a reference that borrowed it still sees it.

Rust's const LIMIT: u32 = 3; is what C programmers usually want from #define or enum { LIMIT = 3 }: a typed compile-time constant, copied into each use. static is the item with one fixed address, the closer match to a C global. A mutable static needs unsafe to access, so shared counters use an atomic or a Mutex instead.

The C habit, written in Rust

This is how the C version usually looks on a first attempt in Rust. Predict what rustc says before you read the explanation.

fn main() {
    let count = 0;
    for word in ["a", "", "c"] {
        if !word.is_empty() {
            count += 1;
        }
    }
    println!("{count}");
}

Practise this code in Playground ↗

Try it

Predict which statement in the quiz rustc rejects, then fix the practice program with the smallest possible change.

What success looks like: You can explain why a shadowing let compiles while += on a binding without mut is error E0384.

Check your understanding

In let total = 0; let total = total + 5; total += 1;, which statement does rustc reject?

Reveal the answer
  1. let total = total + 5;: a name cannot be declared twice
  2. total += 1;: the second total is a new binding, and it is not mut
  3. None of them: the second let made total mutable
  4. let total = 0;: an integer literal needs a type suffix

Shadowing declared a new immutable total with the value 5. += would change it, so rustc reports E0384 and suggests let mut total.

Go deeper

Common misconception: “Shadowing is a mutation with extra syntax.”

A shadowing let creates a second binding and may change the type; the first value is not modified. Anything that already borrowed the first binding still sees the old value.

  1. Find every mut: List the bindings marked mut; they are the only names whose value may change in this scope.
  2. Follow each shadow: For each repeated let, note the new type: &str, then &str, then u32 in the example.
  3. Read the error: Compile the practice program and find E0384 and the help line that suggests let mut count.

Transfer challenge: Parse " 7 " into a u8 with three shadowing let lines, then add 250 to it in a function and see what a debug build does.

Connections

  • From printf to println!: Format strings checked at compile time
  • Integer Overflow and Casts: Defined overflow policy and explicit conversions
Side-by-side comparison: the C version on the left, the Rust version on the right, then 3 rows naming what changes.
const by Default: the same task in C and in Rust, with the differences that matter.
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: const by Default (courseId "rust-for-c-developers", lessonId "c-const-by-default")
Link: https://crabrix.com/learn/rust-for-c-developers/c-const-by-default/
Goal: let bindings are immutable by default; mut opts in to mutation, and shadowing with a new let creates a new binding instead of changing the old one.
Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=rust-for-c-developers/c-const-by-default) 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.

In Claude Code, Codex or Cursor, paste it into a chat with Crabrix Academy connected. On a phone, the Claude and ChatGPT apps open the same links.