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
.