#[derive(Debug)]
enum SettlementSize {
Village,
Town,
City,
}
impl Distribution<SettlementSize> for Standard {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> SettlementSize {
let res = rng.gen_range(0, 3);
match res {
0 => SettlementSize::Village,
1 => SettlementSize::Town,
2 => SettlementSize::City,
_ => panic!("Unknown value!"),
}
}
}
If I add a new entry to the SettlementSize, e.g. MegaCity, I wouldn't like to forget to increase the value in rng.gen_range(0, 3); to 4. Is there some way to detect the maximum value in the enum (as cast to a number)? Do I need to maintain a watcher/guard at the end of the enum for that purpose?
@fosskers I hoped for something similar to Java or Python.
I found the strum crate, which allows me to do: UniqueFeature::iter().count(). I am not sure how to remove the call to iter and just do UniqueFeature::count(). This is a on an enum with struct variants.