I’m struggling with the borrow checker and hope anyone can give me a clue.
The relevant code is below:
// Doc: https://docs.rs/strum/latest/strum/derive.EnumIter.html
use strum_macros::EnumIter;
#[derive(EnumIter, Debug)]
pub enum Frequency {
Freq1m,
Freq1h,
Freq1d,
}
impl Frequency {
pub fn value(&self) -> i32 {
match self {
Frequency::Freq1m => 60,
Frequency::Freq1h => 3600,
Frequency::Freq1d => 86400,
}
}
pub fn display_name(&self) -> &'static str {
match self {
Frequency::Freq1m => "1m",
Frequency::Freq1h => "1h",
Frequency::Freq1d => "1d",
}
}
}
impl PartialEq for Frequency {
fn eq(&self, other: &Self) -> bool {
self.value() == other.value()
}
}
for freq in Frequency::iter() { # move occurs
ui.selectable_value(&mut self.frequency, freq, freq.display_name()); # borrow of moved value
}
After trial and error, I know that if I add the Clone
and Copy
trait to my Frequency
, it will work. But why? My understanding is that the move in for freq in Frenquency::iter()
should be fine, right? The later borrow seems valid to me. What am I missing?