Skip to content

Fix inaccurate explanation of type mismatch error in Chapter 3.1 #4772

Description

@150410awe

Section: Chapter 3.1 - Variables and Mutability
URL: https://rust-book.cs.brown.edu/ch03-01-variables-and-mutability.html

Current wording:
In the section explaining the compile-time error for the following code:

let mut spaces = " ";
spaces = spaces.len();

The book states: "The error says we’re not allowed to mutate a variable’s type:"

Problem:
This explanation is imprecise and potentially misleading. The actual error (E0308) is a type mismatch — the code attempts to assign a usize value to a variable of type &str. Rust's type system does not prevent changing a variable's type in general; in fact, shadowing (let spaces = spaces.len();) allows exactly that.

The error is about incompatible types in an assignment, not about a blanket prohibition on type changes.

Suggested fix:
Change the explanation to something like:

"This error indicates that we cannot assign a value of type usize to a variable of type &str because their types are incompatible."

Which translates to:

"This error shows that we cannot assign a value of type usize to a variable of type &str, because their types do not match."

This accurately describes the mismatch while also being consistent with Rust's shadowing feature.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions