Closed
Description
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
};
}