Skip to content

Chapter 4.1 Discussion about ownership and use before define #371

@hrishim

Description

@hrishim
  • I have searched open and closed issues and pull requests for duplicates, using these search terms:
    • ownership
    • 4.1
  • I have checked the latest main branch to see if this has already been fixed, in this file:
    • No

URL to the section(s) of the book with this problem:
https://rust-book.cs.brown.edu/ch04-01-what-is-ownership.html

Description of the problem:
The code (and text around it) that I am referring to

fn read(y: bool) {
    if y {
        println!("y is true!");
    }
}

fn main() {
    read(x); // oh no! x isn't defined!
    let x = true;
}

The explanation in this section implies that not defining a variable before use is an ownership issue. In fact, this is strictly a use-before-define/initialization error, not an ownership problem per se. Most programmers coming from compiled languages (like C, C++, Rust’s other influences) will recognize this immediately. For newcomers with experience only in dynamically typed languages (e.g., Python), the point might be useful—but it should be framed correctly.
Additionally, the section transitions rather quickly into disassembling generated machine code. While low-level details can be valuable in their place, this assembly dive feels like overkill at this stage of the book.
Readers experienced with C or C++ may be puzzled why such a discussion is labeled an ownership topic.
Readers coming from high-level languages (e.g., Python) are unlikely to benefit from the assembly explanation here and may find it confusing.
My suggestion:
Clarify that the compile-time error shown is fundamentally a use-before-define/initialization issue.
Defer the assembly-level explanation to a later chapter where readers are already comfortable with ownership basics.

Suggested fix:
My suggestion:
Clarify that the compile-time error shown is fundamentally a use-before-define/initialization issue.
Defer the assembly-level explanation to a later chapter where readers are already comfortable with basics.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions