|
pub enum Weekday { |
|
Monday, |
|
Tuesday, |
|
Wednesday, |
|
Thursday, |
|
Friday, |
|
Saturday, |
|
Sunday, |
|
} |
All variants in the enum are unit-only. According to enum casting, its discriminant can be directly accessed with a numeric cast.
So, instead of
|
pub fn get_temperature(&self, day: Weekday) -> Option<i32> { |
|
let index = weekday2index(&day); |
|
self.temperatures[index] |
|
} |
|
|
|
pub fn set_temperature(&mut self, day: Weekday, temperature: i32) { |
|
let index = weekday2index(&day); |
|
self.temperatures[index] = Some(temperature); |
|
} |
|
} |
a simplified version can be used:
pub fn get_temperature(&self, day: Weekday) -> Option<i32> {
self.temperatures[day as usize]
}
pub fn set_temperature(&mut self, day: Weekday, temperature: i32) {
self.temperatures[day as usize] = Some(temperature);
}
100-exercises-to-learn-rust/exercises/06_ticket_management/01_arrays/src/lib.rs
Lines 7 to 15 in d347d1f
All variants in the enum are unit-only. According to enum casting, its discriminant can be directly accessed with a numeric cast.
So, instead of
100-exercises-to-learn-rust/exercises/06_ticket_management/01_arrays/src/lib.rs
Lines 24 to 33 in d347d1f
a simplified version can be used: