Enums are a fundamental feature in Rust that let you define a type that can be one of several distinct variants. Enums make it easier to represent a set of related states or options cleanly and effectively.
In this challenge, you will implement a simple TrafficLight enum to represent the states of a traffic light: Red, Yellow, and Green. Each variant will be a unit struct with no associated data.
Your Task
Create an enum TrafficLight with three variants:
Redrepresenting the red light.Yellowrepresenting the yellow light.Greenrepresenting the green light.
Write a function light_action that takes a TrafficLight and returns a string describing the action associated with the light.
- For
Red, return"Stop". - For
Yellow, return"Caution". - For
Green, return"Go".
Hints
<details> <summary>Click here to reveal hints</summary>- Use the
matchstatement to handle each enum variant.
← PreviousNext →
pub enum TrafficLight {// Define enum variants here}pub fn light_action(light: &TrafficLight) -> &'static str {// Your code here...}