2 ms·
> I don't know if such shape-shifting is allowed in Rust I think so! You mean this? let mut x: Result <bool, String> = Ok (false); dbg! (&x); //
by ReactiveJelly 3y ago
> I don't know if such shape-shifting is allowed in Rust
I think so! You mean this?
let mut x: Result <bool, String> = Ok (false);
dbg! (&x);
// Get a reference to the bool and mutate it
if let Ok (b) = &mut x {
*b = true;
}
dbg! (&x);
// "shape-shift" to the other enum variant
x = Err ("No boolean :(".to_string ());
dbg! (&x);
It runs in Godbolt https://godbolt.org/z/q6jqYdYh1 https://godbolt.org/z/q6jqYdYh1