Skip to content

Cannot mutably borrow inside a match guard #24535

Closed
@Binero

Description

@Binero

I understand this shouldn't be possible in some occasions, because you might be able to modify what you're matching, which can cause the match to break. Why would it be disallowed to mutably borrow something that only exists within the match though?

fn main() {
    let a = 3u8;

    match a {
        0 => (),
        3 if compare(&a, &mut 3) => (),
        _ => (),
    }
}

fn compare(a: &u8, b: &mut u8) -> bool {
    a == b
}

http://is.gd/yuIFGc

I have constructed this minimal case. In my actual code I am running .iter().any() on a variable form outside the match, which isn't allowed because .any() requires a mutable borrow. As the thing I am mutably borrowing only exists within the match, this makes even less sense to me.

Compiler bug?

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