Skip to content

if let && bool #2411

@Ekleog

Description

@Ekleog

First, there are currently 474 issues|PRs here that contain "if let", so I hope I'm not duplicating something already discussed.

This idea is quite close to #929, but instead of allowing to && lets, it'd allow to && with a bool.

Basically, the idea looks like this:

if let Some(x) = y && x > 42 {
    println!("foo");
} else {
    println!("bar");
}

// Would be equivalent to:
if let Some(x) = y {
    if x > 42 {
        println!("foo");
    } else {
        println!("bar");
    }
} else {
    println!("bar");
}

This pattern is something that frequently occurred to me, and I'm finding it painful to always look for workarounds.

The constraints to this pattern would be:

  • Only && could be used after a let, especially if a || is wanted it must be as a child node of the && (required so that the RHS could be evaluated with the let binding in scope)
  • For the time being, it is restricted to one let and one or more && with bools, leaving ideas like if let Some((a, b)) = x && a > 42 && let Some(c) = b.unwrap() for later (modulo Support && in if let expressions #929)

What do you think about this idea?

Metadata

Metadata

Assignees

No one assigned

    Labels

    T-langRelevant to the language team, which will review and decide on the RFC.

    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