Are nested enums optimized into a single match statement?

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

surdeus

Warning

This post was published 51 days ago. The information described in this article may have changed.

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