Skip to content

Support if let guards in matches!() and assert_matches!() #152313

Open

Description

@theemathas

Once #141295 (stabilizing if_let_guards) is merged, we should probably support if let guards in matches!() and assert_matches!().

Activity

  1. added
    A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)
    T-libs-api[DEPRECATED; DO NOT USE]
    C-feature-requestCategory: A feature request, i.e: not implemented / a PR.
    on Feb 8, 2026
  2. added
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Feb 8, 2026
  3. added
    A-patternsRelating to patterns and pattern matching
    and removed
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Feb 8, 2026
  4. Kivooeo commented on Feb 12, 2026

    @Kivooeo
    Member

    In my opinion, it shouldn't. At least I couldn't come up with any useful examples. I thought about it when I was writing a blog post about 'if let' guards

  5. theemathas commented on Feb 12, 2026

    @theemathas
    ContributorAuthor

    @Kivooeo Here's an artificial example of how it might be useful.

    struct Node {
        parent_id: Option<usize>,
    }
    
    fn has_great_grandparent(curr: &Node, nodes: &[Node]) -> bool {
        matches!(curr.parent_id, Some(p)
            if let Some(gp) = nodes[p].parent_id
            && nodes[gp].parent_id.is_some())
    }
  6. theemathas commented on Feb 12, 2026

    @theemathas
    ContributorAuthor

    A more complicated example:

    enum Expr {
        Assign(String, usize),
        Sum(usize, usize),
        Var(String),
        Num(i32),
    }
    
    // check if an expression is the form "x = x + 1"
    fn is_increment(curr: &Expr, exprs: &[Expr]) -> bool {
        matches!(curr, Expr::Assign(lhs_name, rhs)
            if let Expr::Sum(operand1, operand2) = &exprs[rhs]
            && let Expr::Var(operand1_name) = &exprs[*operand1]
            && lhs_name == operand1_name
            && let Expr::Num(1) = exprs[*operand2]
        )
    }
  7. theemathas commented on Feb 12, 2026

    @theemathas
    ContributorAuthor
  8. added 2 commits that reference this issue on Mar 25, 2026
  9. added a commit that references this issue on Apr 4, 2026
  10. added a commit that references this issue on Jul 21, 2026
  11. added
    T-libsRelevant to the library team, which will review and decide on the PR/issue.
    and removed
    T-libs-api[DEPRECATED; DO NOT USE]
    on Aug 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)A-patternsRelating to patterns and pattern matchingC-feature-requestCategory: A feature request, i.e: not implemented / a PR.T-libsRelevant to the library team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions