Hi,
I have this enum:
#[derive(Debug, PartialEq, Ord, PartialOrd, Eq)]
pub enum WeekdayRepeat {
Monday(u32),
Tuesday(u32),
Wednesday(u32),
Thursday(u32),
Friday(u32),
Saturday(u32),
Sunday(u32),
}
And a sorted vector of WeekdayRepeat.
I'd like to check if I have the same variant more than once in the vec, regardless of the value:
my_vec.windows(2).try_for_each(|win|
if win[0] == win[1] {
bail!("Can't have the same weekday multiple times")
} else {
Ok(())
});
this fails because same variant with different value are different.
Is there a way to achieve this without implementing PartialEq and Eq myself?
1 Like
kaj
August 11, 2021, 8:18pm
2
A verbose but working solution:
use WeekdayRepeat::*;
let same_day = std::matches(
(a, b),
(Monday(_), Monday(_)) | (Tuesday(_), Tuesday(_)) // | ...
);
Taking a step back, what is the u32
in each variant used for? If they are the same thing, maybe WeekdayRepeat
could be a struct of the day (as a simple enum) and the u32 in a separate field?
2 Likes
It is indeed the same thing, a frequency for events. So yes, I can transform that in a struct! Thanks! How could I miss this x)
blonk
August 11, 2021, 9:09pm
4
You can use std::mem::discriminant to compare the "outer value" only.
2 Likes
Thanks! Didn't know this function!
system
Closed
November 9, 2021, 9:17pm
6
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.