Skip to content

Simplify solution to 06_ticket_management/01_arrays #304

Description

@iurii-kyrylenko

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);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions