Are nested enums optimized into a single match statement?

⚓ Rust    📅 2025-10-29    👤 surdeus    👁️ 3      

surdeus

I'm curious about how nested enums are optimized. I'm well aware that matching in the following cases will be either identical, or negligable, but I am just curious on behaviour. Given the following code:

pub enum Value {
    SomeFloat(f32),
    SpecialType(SpecialValue),
}

pub enum SpecialValue {
    A,
    B,
}

Does the following match statement require matching two enums:

    match a {
        Value::SomeFloat(value) => todo!(),
        Value::SpecialType(SpecialValue::A) => todo!(),
        Value::SpecialType(SpecialValue::B) => todo!(),
    }

Or is it optimized where matching would be equal to the following:

pub enum Value {
    SomeFloat(f32),
    SpecialTypeA,
    SpecialTypeB,
}

2 posts - 2 participants

Read full topic

🏷️ Rust_feed