2

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);
}

(Playground)

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`
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
ptato
  • 21
  • 4
  • Hi there! You have seen the error message, right? Can you explain what confused you about it or why it didn't help you with your problem? Thanks! – Lukas Kalbertodt May 17 '19 at 09:15
  • The error message just tells me to modify the reference so that it points to a different value. It doesn't help me with modifying the original value. @LukasKalbertodt – ptato May 17 '19 at 09:17
  • Oh oops, this time I was the one to not fully read the error message :D It's a bad suggestion by the compiler IMO, because that's almost never what you want. The solution to your problem is to say `*test_ref = TestType::Second(49);` (dereference the reference with `*`). I'm pretty sure this has been asked and answered before. I will try to find a relevant Q&A shortly! – Lukas Kalbertodt May 17 '19 at 09:19
  • Strange, the best related questions I was able to find are [this](https://stackoverflow.com/q/45985827/2408867) and [this](https://stackoverflow.com/q/36557412/2408867), both of which don't perfectly answer your question IMO (but they might help you regardless). Please let us know if the `*` solved your problem. Thanks! – Lukas Kalbertodt May 17 '19 at 09:24

1 Answers1

0

You have a vector of references, so when getting an element from it, the variable must be mutable then you dereference it to assign a value:

#[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 mut test_ref = test_vec.pop().unwrap();
        *test_ref = TestType::Second(49);
    }
    println!("Original {:?}", test_var);
}

Result:

Original First
--------------
Original Second(49)
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Kaveh
  • 3
  • 1
  • 1