Making a Set of some variants of an enum

I've got an enum with ~30 variants, each of which I will be associating with a field of a struct to retrieve the field's value. But I will only ever need a certain number (~15-20) of the variants/fields for each of many iterations to be run, so I'm looking to use a Set to only store the fields I want to retrieve per iteration. I only need to configure the Set once, and I only intend to access each variant exactly once per iteration (using a memoized intermediary).

It seems like overkill to use a HashSet, when enums are already distinct without hashing. Though I suppose HashMap does allow for custom hashing, so could I switch to an unhashed version as long as all keys are guaranteed to be unique? BTreeSet seems to have drawbacks, but would also technically work. The other option (and the one I'm leaning towards) would be to custom implement around a Vec. The work to check appends would be minimal (if !vec.contains() then append), iterating its held variants would be a breeze, and I don't care about order of insertion/retrieval...

Any suggestions would be greatly appreciated. Thanks!

I guess another option would be a simple struct of bool flags for each field. Default to false, set to true for each variant held that will need to be retrieved.

You should take a look at the bitflags crate.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.