`struct` from an `enum`?

⚓ rust    📅 2025-06-12    👤 surdeus    👁️ 2      

surdeus

Info

This post is auto-generated from RSS feed The Rust Programming Language Forum - Latest topics. Source: `struct` from an `enum`?

I have an enum that I'm deserializing from a configuration file. I also have a bunch of structs implementing MyTrait. I want to have one variant of the enum for each struct that implements MyTrait, and I want to be able to create an instance of the correct struct from the enum variant. Example:

enum Types {
    Type1,
    Type2,
    Type3,
}

struct Type1Option {
    ...
} impl MyTrait for Type1Option {
    ...
}

struct Type2Option {
    ...
} impl MyTrait for Type2Option {
    ...
}

struct Type3Option {
    ...
} impl MyTrait for Type3Option {
    ...
}

#[test]
fn test_this_mess() {
    let type2 = Types::Type2;
    let should_be_type_2_option = type2.generate();
}

Is there a good way to do this?

2 posts - 2 participants

Read full topic

🏷️ rust_feed