Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can use if let:

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

Playground

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading