Skip to content

Untrue (presumably outdated) information about if let #4612

@creeper6530

Description

@creeper6530

URL to the section(s) of the book with this problem:

https://doc.rust-lang.org/stable/book/ch19-01-all-the-places-for-patterns.html#conditional-if-let-expressions

Description of the problem:

In chapter 19.1, near the end of section "Conditional if let Expressions", the following is written:

We can’t combine these two conditions into if let Ok(age) = age && age > 30. The new age we want to compare to 30 isn’t valid until the new scope starts with the curly bracket.

This is not true at the latest stable version of Rust. The following code, tested in the playground with Rust 1.92.0, edition 2024, works as expected, comparing to the "unwrapped" age of type u8:

fn main() {
    let age: Result<u8, _> = "34".parse();

    if let Ok(age) = age && age > 30 {
        println!("Above 30: {}", age);
    } else if let Ok(age) = age {
        println!("Not above 30: {}", age);
    } else {
        println!("Err");
    }
}

Suggested fix:

Update the note to reflect the actual state of affairs. Hopefully a simple, quick fix.

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