Closed
Description
This lets you get a mutable reference to an immutable value under a specific case.
Code:
enum Foo {
Bar(i32),
}
fn main() {
let arr = vec!(Foo::Bar(0));
if let (Some(Foo::Bar(ref mut val)), _) = (&arr.get(0), 0) {
*val = 9001;
}
match arr[0] {
Foo::Bar(ref s) => println!("{}", s)
}
}
From what I can tell, the tuple and at least two depths of Enums are necessary, along with an &
on the right side. There's probably other variations, but that seems to work well enough.
Expected output would be a compile error, instead output is 9001
This works on both stable rustc 1.27.1 (5f2b325f6 2018-07-07)
and nightly rustc 1.29.0-nightly (e5f6498d3 2018-07-10)