Why does iter borrow mutably when used in a pattern guard?

Viewed 301

This code:

fn t(r: &[u8]) {
    match r {
        _ if r.iter().any(|&x| x == b';') => {}
        _ => {}
    }
}

gives me the error:

error[E0301]: cannot mutably borrow in a pattern guard
   |
10 |         _ if r.iter().any(|&x| x == b';') => {}
   |              ^^^^^^^^ borrowed mutably in pattern guard

I understand that I can not borrow mutably in match patterns, but why does the compiler think that r.iter() borrows mutably? There is a separate method iter_mut for borrowing mutably.

And how can I check that &[u8] contains b';' without introducing separate functions?

1 Answers
Related