I have an enum type. I create a variable and assign it a value of that enum type, and I want that variable to be the owner of the value "forever". There is a Vec
of mutable references to that type. I then want to retrieve those references and maybe modify the value that they point towards, so that the original variable is changed. Is there a way to do this?
#[derive(Debug)]
enum TestType {
First,
Second(i32),
}
fn main() {
let mut test_var = TestType::First;
println!("Original {:?}", test_var);
let mut test_vec: Vec<&mut TestType> = vec![&mut test_var];
println!("--------------");
{
let test_ref = test_vec.pop().unwrap();
test_ref = TestType::Second(49);
}
println!("Original {:?}", test_var);
}
Compiling results in:
error[E0308]: mismatched types
--> src/main.rs:16:20
|
16 | test_ref = TestType::Second(49);
| ^^^^^^^^^^^^^^^^^^^^
| |
| expected mutable reference, found enum `TestType`
| help: consider mutably borrowing here: `&mut TestType::Second(49)`
|
= note: expected type `&mut TestType`
found type `TestType`