Enum style question

I have a large enum that has some struct variants, some tuple variants, and some tuple variants that wrap a struct. I'm wondering if this is considered bad style. Possibly it's better to be consistent and have every variant be struct-like?

Another option is to have all enums wrap a struct. This allows the enum data to be passed around easily.

What style do you use?

// Mixed
pub enum E1 {
  Tag1(A, B),
  Tag2 { c: C, d: D },
}

// Alternative 1: Always struct-like
pub enum E2 {
  Tag1 { a: A, b: B },
  Tag2 { c: C, d: D },
}

// Alternative 2: Always wrap a struct
pub enum E3 {
  Tag1(Tag1),
  Tag2(Tag2)
}

pub struct Tag1 {
  pub a: A,
  pub b: B,
}

pub struct Tag2 {
  pub a: A,
  pub b: B,
}
1 Like

Separate struct is useful in case you'd want to have functions that operate on that type. Enum variants are not types, so for example you can't return a specific variant from a function.

2 Likes