Skip to content

Allow *local* mutation and assignments in pattern guards #1006

Closed
@Stebalien

Description

@Stebalien

Due to rust-lang/rust#15989, it isn't possible to assign/mutate in pattern guards. However, assignment/mutation of variables local to the pattern guard should be perfectly safe.

As reported on reddit: https://pay.reddit.com/r/rust/comments/2ztiag/cannot_mutably_borrow_in_a_pattern_guard/ by qthree:

This code is failing: http://is.gd/cAYb5r But that's ok: http://is.gd/7pF28q

For completeness, the failing code is:

fn main() {
    let v = vec!["1".to_string(), "2".to_string(), "3".to_string()];
    let q = match Some(&v) {
        Some(iv) if iv.iter().any(|x| &x[..]=="2") => true,
        _ => false
    };
}

However, the following works:

fn main() {
    let v = vec!["1".to_string(), "2".to_string(), "3".to_string()];
    let q = match Some(&v) {
        Some(iv) if (|| iv.iter().any(|x| &x[..]=="2"))() => true,
        _ => false
    };
}

Metadata

Metadata

Assignees

No one assigned

    Labels

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

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions