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 { // 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 { // 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::Parked => true, 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(v) => format!("The vehicle is broken down: {}.", v) } }}// 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 Self::Parked | Self::Driving{ .. } = self { return true; } false /*if let Self::BrokenDown(_string) = self { return false; } else { return true; }*/ /*match self { Self::Parked => return true, Self::Driving{speed} => return true, _ => return false, }*/ } 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::Parked | VehicleStatus::Driving { .. } => true, _ => 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." );}
pub enum VehicleStatus { Parked, Driving {speed: u32}, BrokenDown(String),}impl VehicleStatus { pub fn is_operational(&self) -> bool { !matches!(self, VehicleStatus::BrokenDown(_)) } pub fn description(&self) -> String { return format!("The vehicle is {}.", self.unwrap_desc()) } pub fn unwrap_desc(&self) -> String { match &self { VehicleStatus::Parked => "parked".to_string(), VehicleStatus::Driving {speed} => format!("driving at {speed} km/h"), VehicleStatus::BrokenDown(cause) => format!("broken down: {cause}"), } }}// 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::BrokenDown(_)) } 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 => true, VehicleStatus::Driving{speed : _} => true, VehicleStatus::BrokenDown( _reason ) => false, } } pub fn description(&self) -> String { match self { VehicleStatus::Parked => format!("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::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 { Parked, Driving { speed: u32 }, BrokenDown(String),}impl VehicleStatus { pub fn is_operational(&self) -> bool { 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 { // Define the VehicleStatus variants here Parked, Driving{speed : u32 }, BrokenDown(String),}impl VehicleStatus { pub fn is_operational(&self) -> bool { // Your code here... let op = match self{ VehicleStatus::Parked => true, VehicleStatus::Driving{speed} => true, VehicleStatus::BrokenDown(reason) =>false, }; op } pub fn description(&self) -> String { // Your code here... let des = 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), }; des }}// 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." );}
#[derive(Debug, Clone)]pub enum VehicleStatus { // Define the VehicleStatus variants here Parked, Driving { speed: u32 }, BrokenDown (String),}impl VehicleStatus { #[allow(unused)] fn get_speed (&self) -> Option<u32> { if let VehicleStatus::Driving {speed} = self { Some(*speed) } else { None } } pub fn is_operational(&self) -> bool { 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 {} km/h.", *speed), VehicleStatus::BrokenDown (desc) => format!("The vehicle is broken down: {}.", desc), } }}// 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." );}
#[derive(Debug, Clone)]pub enum VehicleStatus { // Define the VehicleStatus variants here Parked, Driving { speed: u32 }, BrokenDown (String),}impl VehicleStatus { fn get_speed (&self) -> Option<u32> { if let VehicleStatus::Driving {speed} = self { Some(*speed) } else { None } } pub fn is_operational(&self) -> bool { 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:_} => { match self.get_speed() { Some(speed) => format!("The vehicle is driving at {} km/h.", speed), None => format!("The vehicle is not driving."), } }, VehicleStatus::BrokenDown (desc) => format!("The vehicle is broken down: {}.", desc), } }}// 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 { // 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 => 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{speed: _} => true, VehicleStatus::BrokenDown(_reason) => 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." );}
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 => format!("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 { 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 => 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 { // 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{ speed: _ } => 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." );}
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 => 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 { 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 => 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." );}
#[derive(Eq, PartialEq)]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 => 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... if let VehicleStatus::BrokenDown(_) = self { false } else { 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 {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 { // Your code here... !matches!(self, VehicleStatus::BrokenDown(_)) } 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 { 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." );}
#[derive(PartialEq)]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::BrokenDown(_) => false, _ =>true, } } pub fn description(&self) -> String { // Your code here... match self{ Self::Parked => format!("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 { 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 => "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 { 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 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 { return false } return true } 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 (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::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}."), _ => "".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 { match self { VehicleStatus::BrokenDown(_) => false, _ => true, } } pub fn description(&self) -> String { match self { VehicleStatus::BrokenDown(reason) => format!("The vehicle is broken down: {reason}."), VehicleStatus::Parked => "The vehicle is parked.".to_string(), 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 { 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 { let tail: String = match self { VehicleStatus::BrokenDown(reason) => "broken down: ".to_string() + reason, VehicleStatus::Parked => "parked".to_string(), VehicleStatus::Driving { speed } => format!("driving at {speed} km/h"), }; "The vehicle is ".to_string() + &tail + "." }}// 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 { 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 { match self { VehicleStatus::BrokenDown(_) => 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 {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::BrokenDown(_) => false, _ => true } } pub fn description(&self) -> String { match self { Self::Parked => format!("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::Parked => true, VehicleStatus::Driving{speed} => true, VehicleStatus::BrokenDown(reason) => false } } pub fn description(&self) -> String { // Your code here... match self { VehicleStatus::Parked => return format!("The vehicle is parked."), VehicleStatus::Driving {speed} => return format!("The vehicle is driving at {speed} km/h."), VehicleStatus::BrokenDown(reason) => return 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 => 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 => 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." );}
pub enum VehicleStatus { Parked, Driving { speed: u32 }, BrokenDown(String),}impl VehicleStatus { pub fn is_operational(&self) -> bool { !matches!(self, VehicleStatus::BrokenDown(_)) } 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 { 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 {} 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 { Self::BrokenDown(_) => false, _ => true, } } 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 => true, Self::Driving { speed } => true, Self::BrokenDown(d) => 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(c) => format!("The vehicle is broken down: {}.", c), } }}// 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 => ("The vehicle is parked.").to_string(), VehicleStatus::Driving{speed} => format!("The vehicle is driving at {} km/h.", speed).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 { // 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 { speed: _ } => true, VehicleStatus::BrokenDown(_reason) => 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... 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 {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 { 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 { match self { VehicleStatus::Parked => true, 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 => true, VehicleStatus::Driving {speed:_} => true, _ => 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 {} 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 { match self { VehicleStatus::Parked => true, VehicleStatus::Driving {speed:_} => true, _ => false } } 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 { // Define the VehicleStatus variants hereç Parked, Driving{speed:u32}, BrokenDown(String)}impl VehicleStatus { pub fn is_operational(&self) -> bool { // Your code here... !matches!(self, VehicleStatus::BrokenDown(_)) } pub fn description(&self) -> String { match self{ VehicleStatus::Parked=>format!("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::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 {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." );}