Enums in Rust can have associated functions and methods, just like structs. These methods make it easier to encapsulate behavior directly within the enum.
In this challenge, you'll model the statuses of different vehicles and implement methods to describe their behavior.
Create an enum VehicleStatus
with the following variants:
Parked
— a unit variant representing a parked vehicle.Driving { speed: u32 }
— a named field variant representing a vehicle driving at a certain speed.BrokenDown(String)
— a tuple variant with a String
describing the reason for the breakdown.Implement the following methods for VehicleStatus
:
is_operational(&self) -> bool
:
true
if the vehicle is either Parked
or Driving
.false
if the vehicle is BrokenDown
.description(&self) -> String
:
"The vehicle is parked."
for Parked
."The vehicle is driving at {speed} km/h."
for Driving { speed }
."The vehicle is broken down: {reason}."
for BrokenDown(reason)
.match
expression inside the methods to handle each variant.&self
for the methods since they should not consume the enum.format!
to construct strings with dynamic values, such as speed
and reason
.pub enum VehicleStatus { Parked, Driving {speed: u32 }, BrokenDown(String)}impl VehicleStatus { pub fn is_operational(&self) -> bool { return match self { VehicleStatus::Parked => true, VehicleStatus::Driving {..} => true, VehicleStatus::BrokenDown(_) => false } } pub fn description(&self) -> String { match self { VehicleStatus::Parked => String::from("The vehicle is parked."), VehicleStatus::Driving {speed} => format!("The vehicle is driving at {speed} km/h."), VehicleStatus::BrokenDown (reason) => format!("The vehicle is broken down: {reason}.") } }}// Example use casepub 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." );}
pub enum VehicleStatus { // Define the VehicleStatus variants here Parked, Driving{speed:u32}, BrokenDown(String), }impl VehicleStatus { pub fn is_operational(&self) -> bool { // Your code here... match self{ VehicleStatus::Parked => true, VehicleStatus::Driving{..} => true, VehicleStatus::BrokenDown(_) => false, } } pub fn description(&self) -> String { // Your code here... match self{ VehicleStatus::Parked => format!("The vehicle is parked."), VehicleStatus::Driving{speed} => format!("The vehicle is driving at {speed} km/h."), VehicleStatus::BrokenDown(a) => format!("The vehicle is broken down: {a}." ), } }}// Example use casepub 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." );}
pub enum VehicleStatus { // Define the VehicleStatus variants here Parked, Driving {speed: u32}, BrokenDown (String)}impl VehicleStatus { pub fn is_operational(&self) -> bool { // Your code here... match self { VehicleStatus::Parked => true, VehicleStatus::Driving{..} => true, VehicleStatus::BrokenDown(_) => false, } } pub fn description(&self) -> String { // Your code here... match self { VehicleStatus::Parked => "The vehicle is parked.".to_string(), VehicleStatus::Driving {speed} => format!("The vehicle is driving at {} km/h.", *speed), VehicleStatus::BrokenDown (reason) => format!("The vehicle is broken down: {}.", reason), } }}// Example use casepub 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." );}
pub enum VehicleStatus { // Define the VehicleStatus variants here Parked, Driving {speed: u32}, BrokenDown (String),}impl VehicleStatus { pub fn is_operational(&self) -> bool { // Your code here... match self { VehicleStatus::Parked => true, VehicleStatus::Driving { .. } => true, VehicleStatus::BrokenDown (_) => 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 {} km/h.", *speed), VehicleStatus::BrokenDown (reason) => format!("The vehicle is broken down: {}.", reason), } }}// Example use casepub 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." );}
pub enum VehicleStatus { // Define the VehicleStatus variants here Parked, Driving{speed: u32}, BrokenDown(String),}impl VehicleStatus { pub fn is_operational(&self) -> bool { // Your code here... if let VehicleStatus::BrokenDown(_) = self { false } else { true } } pub fn description(&self) -> String { // Your code here... match self { VehicleStatus::Parked => "The vehicle is parked.".to_string(), VehicleStatus::Driving{speed} => format!("The vehicle is driving at {speed} km/h."), VehicleStatus::BrokenDown(reason) => format!("The vehicle is broken down: {reason}."), } }}// Example use casepub 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." );}
pub enum VehicleStatus { // Define the VehicleStatus variants here Parked, Driving { speed: u32 }, BrokenDown(String),}impl VehicleStatus { pub fn is_operational(&self) -> bool { !matches!(self, VehicleStatus::BrokenDown(_)) } pub fn description(&self) -> String { // Your code here... match self { VehicleStatus::Parked => "The vehicle is parked.".to_string(), VehicleStatus::Driving { speed } => format!("The vehicle is driving at {speed} km/h."), VehicleStatus::BrokenDown(reason) => format!("The vehicle is broken down: {reason}.") } }}// Example use casepub 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." );}
// Define the VehicleStatus enum with the specified variantspub enum VehicleStatus { Parked, // Unit variant representing a parked vehicle Driving { speed: u32 }, // Named field variant representing a vehicle driving at a certain speed BrokenDown(String), // Tuple variant with a String describing the reason for the breakdown}// Implement methods for VehicleStatusimpl VehicleStatus { // Method to check if the vehicle is operational pub fn is_operational(&self) -> bool { match self { VehicleStatus::Parked | VehicleStatus::Driving { .. } => true, // Vehicle is operational if Parked or Driving VehicleStatus::BrokenDown(_) => false, // Vehicle is not operational if BrokenDown } } // Method to return a description of the vehicle's status pub fn description(&self) -> String { match self { VehicleStatus::Parked => "The vehicle is parked.".to_string(), VehicleStatus::Driving { speed } => format!("The vehicle is driving at {} km/h.", speed), VehicleStatus::BrokenDown(reason) => format!("The vehicle is broken down: {}.", reason), } }}// Example use casepub 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." );}
pub enum VehicleStatus { // Define the VehicleStatus variants here Parked, Driving { speed: u32 }, BrokenDown(String),}impl VehicleStatus { pub fn is_operational(&self) -> bool { // Your code here... match self { VehicleStatus::BrokenDown(_) => { false } _ => { true } } } pub fn description(&self) -> String { // Your code here... match self { VehicleStatus::Parked => { "The vehicle is parked.".to_string() } VehicleStatus::Driving {speed} => { format!("The vehicle is driving at {speed} km/h.") } VehicleStatus::BrokenDown(reason) => { format!("The vehicle is broken down: {reason}.") } } }}// Example use casepub 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." );}
pub enum VehicleStatus { Parked, Driving { speed: u32 }, BrokenDown( String ),}impl VehicleStatus { pub fn is_operational(&self) -> bool { matches!(self, VehicleStatus::Parked | VehicleStatus::Driving{..}) } 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.").to_string(), VehicleStatus::BrokenDown(reason) => format!("The vehicle is broken down: {reason}.").to_string(), } }}// Example use casepub 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." );}
pub enum VehicleStatus { Parked, Driving{speed: u32}, BrokenDown(String),}impl VehicleStatus { pub fn is_operational(&self) -> bool { matches!(self, VehicleStatus::Parked) || matches!(self, VehicleStatus::Driving{ speed: _ }) } 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(reason) => format!("The vehicle is broken down: {reason}."), } }}// Example use casepub 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." );}
pub enum VehicleStatus { Parked, Driving { speed: u32 }, BrokenDown(String),}impl VehicleStatus { pub fn is_operational(&self) -> bool { match self { VehicleStatus::Parked | VehicleStatus::Driving { .. } => true, VehicleStatus::BrokenDown(_) => 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(reason) => format!("The vehicle is broken down: {reason}."), } }}
pub enum VehicleStatus { Parked, Driving { speed: u32 }, BrokenDown(String),}impl VehicleStatus { pub fn is_operational(&self) -> bool { match self { VehicleStatus::BrokenDown(_) => false, _ => true, } } 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(reason) => format!("The vehicle is broken down: {reason}."), } }}// Example use casepub 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." );}
pub enum VehicleStatus { // Define the VehicleStatus variants here Parked, Driving{speed: u32}, BrokenDown(String)}impl VehicleStatus { pub fn is_operational(&self) -> bool { // Your code here... match self { VehicleStatus::Parked | VehicleStatus::Driving{..} => true, VehicleStatus::BrokenDown(_) => false } } pub fn description(&self) -> String { // Your code here... match self { VehicleStatus::Parked => "The vehicle is parked.".to_string(), VehicleStatus::Driving{speed} => format!("The vehicle is driving at {speed} km/h."), VehicleStatus::BrokenDown(reason) => format!("The vehicle is broken down: {reason}.") } }}// Example use casepub 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." );}
use std::format;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, VehicleStatus::BrokenDown(_) => false } } pub fn description(&self) -> String { match self { VehicleStatus::Parked => String::from("The vehicle is parked."), VehicleStatus::Driving { speed } => format!("The vehicle is driving at {} km/h.", speed), VehicleStatus::BrokenDown(reason) => format!("The vehicle is broken down: {}.", reason) } }}// Example use casepub 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." );}
pub enum VehicleStatus { Parked, Driving { speed: u32 }, BrokenDown(String)}impl VehicleStatus { pub fn is_operational(&self) -> bool { match self { VehicleStatus::Parked | VehicleStatus::Driving { speed: _ } => true, VehicleStatus::BrokenDown(_) => false } } pub fn description(&self) -> String { match self { VehicleStatus::Parked => "The vehicle is parked.".to_string(), VehicleStatus::BrokenDown(reason) => format!("The vehicle is broken down: {reason}."), VehicleStatus::Driving { speed } => format!("The vehicle is driving at {speed} km/h.") } }}// Example use casepub 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." );}
pub enum VehicleStatus { // Define the VehicleStatus variants here Parked, Driving { speed: u32 }, BrokenDown(String),}impl VehicleStatus { pub fn is_operational(&self) -> bool { // Your code here... match self { VehicleStatus::Parked => true, VehicleStatus::Driving { .. } => true, VehicleStatus::BrokenDown(_) => false } } pub fn description(&self) -> String { // Your code here... match self { VehicleStatus::Parked => "The vehicle is parked.".to_string(), VehicleStatus::Driving { speed } => format!("The vehicle is driving at {} km/h.", speed), VehicleStatus::BrokenDown(reason) => format!("The vehicle is broken down: {}.", reason), } }}// Example use casepub 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." );}
pub enum VehicleStatus { // Define the VehicleStatus variants here Parked, Driving {speed: u32}, BrokenDown(String),}impl VehicleStatus { pub fn is_operational(&self) -> bool { // Your code here... match self { VehicleStatus::BrokenDown(_) => false, _ => true } } pub fn description(&self) -> String { // Your code here... match self { VehicleStatus::Parked => String::from("The vehicle is parked."), VehicleStatus::Driving{speed} => format!("The vehicle is driving at {} km/h.", speed), VehicleStatus::BrokenDown(reason) => format!("The vehicle is broken down: {}.", reason) } }}// Example use casepub 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." );}
pub enum VehicleStatus { Parked, Driving { speed: u32 }, BrokenDown(String)}impl VehicleStatus { pub fn is_operational(&self) -> bool { match self { VehicleStatus::BrokenDown(reason) => false, _ => true } } pub fn description(&self) -> String { match self { VehicleStatus::Parked => format!("The vehicle is parked."), VehicleStatus::Driving{speed} => format!("The vehicle is driving at {} km/h.",speed), VehicleStatus::BrokenDown(reason) => format!("The vehicle is broken down: {}.", reason) } }}// Example use casepub 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." );}
pub enum VehicleStatus { Parked, Driving { speed: u32 }, BrokenDown(String),}impl VehicleStatus { pub fn is_operational(&self) -> bool { match self { Self::Parked | Self::Driving{..} => true, Self::BrokenDown(_) => false, } } pub fn description(&self) -> String { match self { Self::Parked => "The vehicle is parked.".to_string(), Self::Driving {speed} => format!("The vehicle is driving at {} km/h.", speed), Self::BrokenDown(reason) => format!("The vehicle is broken down: {}.", reason), } }}// Example use casepub 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." );}
pub enum VehicleStatus { Parked, Driving { speed: u32 }, BrokenDown(String)}impl VehicleStatus { pub fn is_operational(&self) -> bool { match self { Self::Parked => true, Self::Driving { speed: _ } => true, _ => false } } pub fn description(&self) -> String { match self { Self::Parked => "The vehicle is parked.".into(), Self::Driving { speed } => format!("The vehicle is driving at {speed} km/h."), Self::BrokenDown(reason) => format!("The vehicle is broken down: {reason}.") } }}// Example use casepub 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." );}
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::BrokenDown(_) => false, _ => true } } 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(reason) => format!("The vehicle is broken down: {reason}.") } }}// Example use casepub 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." );}
pub enum VehicleStatus { // Define the VehicleStatus variants here Parked, Driving { speed: u32 }, BrokenDown(String),}impl VehicleStatus { pub fn is_operational(&self) -> bool { // Your code here... match self { VehicleStatus::BrokenDown(_) => false, _ => true, } } pub fn description(&self) -> String { // Your code here... match self { VehicleStatus::Parked => "The vehicle is parked.".to_string(), VehicleStatus::Driving { speed } => { format!("The vehicle is driving at {} km/h.", speed) } VehicleStatus::BrokenDown(reason) => format!("The vehicle is broken down: {}.", reason), } }}// Example use casepub 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." );}
pub enum VehicleStatus { // Define the VehicleStatus variants here Parked, Driving { speed: u32 }, BrokenDown(String),}impl VehicleStatus { pub fn is_operational(&self) -> bool { // Your code here... match self { VehicleStatus::BrokenDown(_) => false, _ => true, } } pub fn description(&self) -> String { // Your code here... match self { VehicleStatus::Parked => "The vehicle is parked.".to_string(), VehicleStatus::Driving { speed } => { format!("The vehicle is driving at {} km/h.", speed) } VehicleStatus::BrokenDown(reason) => format!("The vehicle is broken down: {}.", reason), } }}// Example use casepub 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." );}
pub enum VehicleStatus { // Define the VehicleStatus variants here Parked, Driving{speed: u32}, BrokenDown(String),}impl VehicleStatus { pub fn is_operational(&self) -> bool { // Your code here... match self { VehicleStatus::Parked | VehicleStatus::Driving{..} => true, VehicleStatus::BrokenDown(_) => false, } } pub fn description(&self) -> String { // Your code here... match self { VehicleStatus::Parked => "The vehicle is parked.".to_owned(), VehicleStatus::Driving{speed} => format!("The vehicle is driving at {} km/h.", speed).to_owned(), VehicleStatus::BrokenDown(reason) => format!("The vehicle is broken down: {}.", reason).to_owned(), } }}// Example use casepub 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." );}
pub enum VehicleStatus { // Define the VehicleStatus variants here Parked, Driving {speed: u32}, BrokenDown(String), }impl VehicleStatus { pub fn is_operational(&self) -> bool { // Your code here... match self { Self::Parked | Self::Driving {speed : _} => true, Self::BrokenDown(_) => false } } pub fn description(&self) -> String { // Your code here... match self { Self::Parked => "The vehicle is parked.".to_string(), Self::Driving {speed} => format!("The vehicle is driving at {speed} km/h."), Self::BrokenDown(reason) => format!("The vehicle is broken down: {reason}.") } }}// Example use casepub 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." );}
pub enum VehicleStatus { Parked, Driving { speed: u32 }, BrokenDown(String)}impl VehicleStatus { pub fn is_operational(&self) -> bool { match self { VehicleStatus::BrokenDown(_) => false, _ => true } } pub fn description(&self) -> String { match self { VehicleStatus::Parked => "The vehicle is parked.".to_string(), VehicleStatus::Driving { speed } => format!("The vehicle is driving at {} km/h.", speed), VehicleStatus::BrokenDown(reason) => format!("The vehicle is broken down: {}.", reason), } }}// Example use casepub 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." );}
pub enum VehicleStatus { // Define the VehicleStatus variants here Parked, Driving { speed: u32}, BrokenDown(String),}impl VehicleStatus { pub fn is_operational(&self) -> bool { // Your code here... match self { VehicleStatus::BrokenDown(_) => false, _ => true } } pub fn description(&self) -> String { // Your code here... match self { VehicleStatus::Parked => String::from("The vehicle is parked."), VehicleStatus::Driving { speed } => format!("The vehicle is driving at {} km/h.", speed), VehicleStatus::BrokenDown(reason) => format!("The vehicle is broken down: {}.", reason) } }}// Example use casepub 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." );}
pub enum VehicleStatus { Parked, Driving { speed: u32 }, BrokenDown(String),}impl VehicleStatus { pub fn is_operational(&self) -> bool { match self { VehicleStatus::BrokenDown(_) => false, _ => true } } 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(reason) => format!("The vehicle is broken down: {reason}."), } }}// Example use casepub 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." );}
pub enum VehicleStatus { // Define the VehicleStatus variants here Parked, Driving {speed: u32}, BrokenDown(String)}impl VehicleStatus { pub fn is_operational(&self) -> bool { // Your code here... match self { Self::Parked => true, Self::Driving{speed} => true, _ => false } } pub fn description(&self) -> String { match self { Self::Parked => "The vehicle is parked.".to_string(), Self::Driving{speed} => format!("The vehicle is driving at {speed} km/h."), Self::BrokenDown(reason) => format!("The vehicle is broken down: {reason}.") } }}// Example use casepub 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." );}
pub enum VehicleStatus { Parked, Driving { speed: u32 }, BrokenDown(String),}impl VehicleStatus { pub fn is_operational(&self) -> bool { match self { Self::Parked => true, Self::Driving{ speed: _ } => true, _ => false, } } pub fn description(&self) -> String { let status = match self { Self::Parked => "parked".to_string(), Self::Driving{ speed } => format!("driving at {speed} km/h"), Self::BrokenDown(reason) => format!("broken down: {reason}") }; format!("The vehicle is {status}.") }}// Example use casepub 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." );}
pub enum VehicleStatus { Parked, Driving { speed: u32 }, BrokenDown(String),}impl VehicleStatus { pub fn is_operational(&self) -> bool { match self { Self::Parked | Self::Driving {..} => true, Self::BrokenDown(_) => false } } pub fn description(&self) -> String { match self { Self::Parked => "The vehicle is parked.".to_string(), Self::Driving { speed } => format!("The vehicle is driving at {} km/h.", speed), Self::BrokenDown(reason) => format!("The vehicle is broken down: {}.", reason) } }}// Example use casepub 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." );}
pub enum VehicleStatus { Parked, Driving { speed: u32 }, BrokenDown(String),}impl VehicleStatus { pub fn is_operational(&self) -> bool { match self { Self::Parked | Self::Driving {..} => true, Self::BrokenDown(_) => false } } pub fn description(&self) -> String { match self { Self::Parked => "The vehicle is parked.".to_string(), Self::Driving { speed } => format!("The vehicle is driving at {} km/h.", speed), Self::BrokenDown(reason) => format!("The vehicle is broken down: {}.", reason) } }}// Example use casepub 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." );}
pub enum VehicleStatus { Parked, Driving { speed: u32 }, BrokenDown(String),}impl VehicleStatus { pub fn is_operational(&self) -> bool { match self { VehicleStatus::Parked => true, VehicleStatus::Driving {..} => true, VehicleStatus::BrokenDown(_) => false, } } pub fn description(&self) -> String { match self { VehicleStatus::Parked => "The vehicle is parked.".into(), VehicleStatus::Driving{speed} => format!("The vehicle is driving at {speed} km/h.").into(), VehicleStatus::BrokenDown(reason) => format!("The vehicle is broken down: {reason}.").into(), } } }
pub enum VehicleStatus { // Define the VehicleStatus variants here Parked, Driving { speed: u32}, BrokenDown(String)}impl VehicleStatus { pub fn is_operational(&self) -> bool { // Your code here... match self { Self::Parked | Self::Driving{speed: _} => true, Self::BrokenDown(_) => false } } pub fn description(&self) -> String { // Your code here... match self { Self::Parked => "The vehicle is parked.".to_string(), Self::Driving { speed } => format!("The vehicle is driving at {} km/h.", speed), Self::BrokenDown(reason) => format!("The vehicle is broken down: {}.", reason) } }}// Example use casepub 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." );}
pub enum VehicleStatus { // Define the VehicleStatus variants here Parked, Driving { speed: u32}, BrokenDown(String)}impl VehicleStatus { pub fn is_operational(&self) -> bool { // Your code here... match self { Self::Parked | Self::Driving{speed: _} => true, Self::BrokenDown(_) => false } } pub fn description(&self) -> String { // Your code here... match self { Self::Parked => "The vehicle is parked.".to_string(), Self::Driving { speed } => format!("The vehicle is driving at {} km/h.", speed), Self::BrokenDown(reason) => format!("The vehicle is broken down: {}.", reason) } }}// Example use casepub 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." );}
pub enum VehicleStatus { Parked, Driving { speed: u32 }, BrokenDown(String),}impl VehicleStatus { pub fn is_operational(&self) -> bool { match self { VehicleStatus::Parked | VehicleStatus::Driving { speed: _ } => true, VehicleStatus::BrokenDown(_) => 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(reason) => format!("The vehicle is broken down: {reason}."), } }}// Example use casepub 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." );}
pub enum VehicleStatus { // Define the VehicleStatus variants here Parked, Driving{speed: u32}, BrokenDown(String)}impl VehicleStatus { pub fn is_operational(&self) -> bool { // Your code here... match self{ VehicleStatus::Parked | VehicleStatus::Driving{..} => true, VehicleStatus::BrokenDown(_) => false } } pub fn description(&self) -> String { // Your code here... match self { VehicleStatus::Parked => String::from("The vehicle is parked."), VehicleStatus::Driving{speed} => format!("The vehicle is driving at {speed} km/h."), VehicleStatus::BrokenDown(reason)=> format!("The vehicle is broken down: {reason}.") } }}// Example use casepub 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." );}
pub enum VehicleStatus { // Define the VehicleStatus variants here Parked, Driving { speed: u32 }, BrokenDown(String),}impl VehicleStatus { pub fn is_operational(&self) -> bool { // Your code here... match self { VehicleStatus::Parked | 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(reason) => format!("The vehicle is broken down: {reason}.") } }}// Example use casepub 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." );}
pub enum VehicleStatus { Parked, Driving { speed: u32 }, BrokenDown(String),}impl VehicleStatus { pub fn is_operational(&self) -> bool { matches!(self, VehicleStatus::Parked | VehicleStatus::Driving { .. } ) } 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(reason) => format!("The vehicle is broken down: {reason}."), } }}// Example use casepub 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." );}
pub enum VehicleStatus { Parked, Driving { speed: u32 }, BrokenDown(String),}impl VehicleStatus { pub fn is_operational(&self) -> bool { match self { VehicleStatus::Driving { .. } | VehicleStatus::Parked => true, VehicleStatus::BrokenDown(_) => 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(reason) => format!("The vehicle is broken down: {reason}."), } }}
pub enum VehicleStatus { // Define the VehicleStatus variants here Parked, Driving {speed: u32}, BrokenDown(String),}impl VehicleStatus { pub fn is_operational(&self) -> bool { // Your code here... if let Self::BrokenDown(_) = self { false } else { true } } pub fn description(&self) -> String { // Your code here... match self { Self::Parked => String::from("The vehicle is parked."), Self::Driving{speed} => format!("The vehicle is driving at {speed} km/h."), Self::BrokenDown(reason) => format!("The vehicle is broken down: {reason}."), } }}// Example use casepub 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." );}
pub enum VehicleStatus { // Define the VehicleStatus variants here Parked, Driving { speed: u32 }, BrokenDown(String)}impl VehicleStatus { pub fn is_operational(&self) -> bool { // Your code here... match self{ VehicleStatus::BrokenDown(_)=>false, _=> true } } pub fn description(&self) -> String { // Your code here... match self{ VehicleStatus::Parked=>"The vehicle is parked.".to_string(), VehicleStatus::Driving{speed}=>format!("The vehicle is driving at {speed} km/h."), VehicleStatus::BrokenDown(reason)=>format!("The vehicle is broken down: {reason}.") } }}// Example use casepub 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." );}
pub enum VehicleStatus { Parked, Driving { speed: u32 }, BrokenDown(String),}impl VehicleStatus { pub fn is_operational(&self) -> bool { match self { Self::Parked | Self::Driving { .. } => true, Self::BrokenDown(_) => false, } } pub fn description(&self) -> String { match self { Self::Parked => "The vehicle is parked.".into(), Self::Driving { speed } => format!("The vehicle is driving at {speed} km/h."), Self::BrokenDown(reason) => format!("The vehicle is broken down: {reason}."), } }}// Example use casepub 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." );}
pub enum VehicleStatus { // Define the VehicleStatus variants here Parked, Driving { speed: i32 }, BrokenDown(String),}impl VehicleStatus { pub fn is_operational(&self) -> bool { match self { VehicleStatus::Parked | VehicleStatus::Driving { speed: _ } => true, VehicleStatus::BrokenDown(_) => false, } } pub fn description(&self) -> String { match self { VehicleStatus::Parked => String::from("The vehicle is parked."), VehicleStatus::Driving { speed } => { format!("The vehicle is driving at {} km/h.", speed) } VehicleStatus::BrokenDown(reason) => format!("The vehicle is broken down: {}.", reason), } }}// Example use casepub 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." );}
pub enum VehicleStatus { Parked, Driving { speed: u32 }, BrokenDown(String),}use VehicleStatus::*;impl VehicleStatus { pub fn is_operational(&self) -> bool { match self { Parked | Driving { .. } => true, BrokenDown(_) => false, } } pub fn description(&self) -> String { match self { Parked => "The vehicle is parked.".to_string(), Driving { speed } => format!("The vehicle is driving at {speed} km/h."), BrokenDown(reason) => format!("The vehicle is broken down: {reason}."), } }}// Example use casepub 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." );}
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::BrokenDown(_) => false, _ => true, } } 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(reason) => format!("The vehicle is broken down: {reason}.") } }}// Example use casepub 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." );}
pub enum VehicleStatus { // Define the VehicleStatus variants here Parked, Driving { speed: u32 }, BrokenDown(String),}impl VehicleStatus { pub fn is_operational(&self) -> bool { // Your code here... match self { Self::Parked | Self::Driving { .. } => true, Self::BrokenDown(_) => false, } } pub fn description(&self) -> String { // Your code here... format!("The vehicle is {}.", match self { Self::Parked => "parked".into(), Self::Driving { speed } => format!("driving at {speed} km/h"), Self::BrokenDown(reason) => format!("broken down: {reason}"), }) }}// Example use casepub 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." );}
pub enum VehicleStatus { // Define the VehicleStatus variants here Parked, Driving { speed: u32 }, BrokenDown(String),}impl VehicleStatus { pub fn is_operational(&self) -> bool { // Your code here... match &self { VehicleStatus::BrokenDown(_) => false, _ => true, } } pub fn description(&self) -> String { // Your code here... match &self { VehicleStatus::Parked => "The vehicle is parked.".to_string(), VehicleStatus::Driving { speed } => format!("The vehicle is driving at {speed} km/h."), VehicleStatus::BrokenDown(reason) => format!("The vehicle is broken down: {reason}."), } }}// Example use casepub 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." );}
pub enum VehicleStatus { // Define the VehicleStatus variants here Parked, Driving { speed: u32 }, BrokenDown(String),}impl VehicleStatus { pub fn is_operational(&self) -> bool { // Your code here... match &self { VehicleStatus::BrokenDown(_) => false, _ => true, } } pub fn description(&self) -> String { // Your code here... match &self { VehicleStatus::Parked => "The vehicle is parked.".to_string(), VehicleStatus::Driving { speed: speed } => format!("The vehicle is driving at {speed} km/h."), VehicleStatus::BrokenDown(reason) => format!("The vehicle is broken down: {reason}."), } }}// Example use casepub 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." );}
pub enum VehicleStatus { Parked, Driving { speed: u32 }, BrokenDown(String)}impl VehicleStatus { pub fn is_operational(&self) -> bool { match self { VehicleStatus::Parked => true, VehicleStatus::Driving { speed: _ } => true, VehicleStatus::BrokenDown(_) => false, } } pub fn description(&self) -> String { match self { VehicleStatus::Parked => String::from("The vehicle is parked."), VehicleStatus::Driving { speed } => format!("The vehicle is driving at {} km/h.", speed), VehicleStatus::BrokenDown(reason) => format!("The vehicle is broken down: {}.", reason) } }}// Example use casepub 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." );}