How to cast an enum that is a field inside a struct?

Viewed 272

Why does casting move self out of borrowed content?

#[derive(Debug)]
enum Foo {
    One = 1,
    Two = 2,
}

struct Bar {
    f: Foo,
}

impl Bar {
    fn bar(&mut self) {
        println!("{:?}", self.f); // "Two"
        println!("{:?}", Foo::Two as u8); // "2"
        println!("{:?}", self.f as u8); // error
    }
}

fn main() {
    Bar{f: Foo::Two}.bar();
}

Throws this error:

error[E0507]: cannot move out of borrowed content
  --> src/main.rs:15:26
   |
15 |         println!("{:?}", self.f as u8); // error
   |                          ^^^^ cannot move out of borrowed content
1 Answers
Related