-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanother_enum.rs
More file actions
42 lines (37 loc) · 1.25 KB
/
Copy pathanother_enum.rs
File metadata and controls
42 lines (37 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
pub enum VehicleStatus {
// Define the VehicleStatus variants here
Parked,
Driving { speed: u32 },
BrokenDown(String),
}
impl VehicleStatus {
pub fn is_operational(&self) -> bool {
match self{
VehicleStatus::Parked => true,
VehicleStatus::Driving{..} => true,
_=> false,
}
}
pub fn description(&self) -> String {
match self{
VehicleStatus::Parked => "The vehicle is parked.".to_string(),
VehicleStatus::Driving{speed} => format!("The vehicle is driving at {speed} km/h."),
VehicleStatus::BrokenDown(rea) => format!("The vehicle is broken down: {rea}."),
}
}
}
// Example use case
pub fn main() {
let parked = VehicleStatus::Parked;
assert!(parked.is_operational());
assert_eq!(parked.description(), "The vehicle is parked.");
let driving = VehicleStatus::Driving { speed: 80 };
assert!(driving.is_operational());
assert_eq!(driving.description(), "The vehicle is driving at 80 km/h.");
let broken_down = VehicleStatus::BrokenDown("Flat tire".to_string());
assert!(!broken_down.is_operational());
assert_eq!(
broken_down.description(),
"The vehicle is broken down: Flat tire."
);
}