Get data out of enum

I have a function that received an enum with some data that I don’t know inside. How can I retrieve its single values? For ex:

pub enum Type1{
    Original {
      A: u32,
      B: u32,
    },
  
    Plus {
      A: u32,
      C: u32,
    },
  }

let bp = Type1::Original { A: 3, B: 4 };
// I have received bp but I want to work with bp.A
// How can I print it for example?

println!("{}", bp.A);
// Gives me the error: error[E0609]: no field `A` on type `Type1`

>Solution :

You can use if let:

if let Type1::Original { A, B } = bp {
    println!("{}", A);
}

Playground

Leave a Reply