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.
Create an enum TrafficLight
with three variants:
Red
representing the red light.Yellow
representing the yellow light.Green
representing the green light.Write a function light_action
that takes a TrafficLight
and returns a string describing the action associated with the light.
Red
, return "Stop"
.Yellow
, return "Caution"
.Green
, return "Go"
.match
statement to handle each enum variant.pub enum TrafficLight { Red, Yellow, Green,}pub fn light_action(light: &TrafficLight) -> &'static str {match light { TrafficLight::Red => "Stop", TrafficLight::Yellow => "Caution", TrafficLight::Green => "Go",}}
pub enum TrafficLight { // Define enum variants here Red, Yellow, Green,}pub fn light_action(light: &TrafficLight) -> &'static str { // Your code here... match light { TrafficLight::Red => "Stop", TrafficLight::Yellow => "Caution", TrafficLight::Green => "Go", }}
pub enum TrafficLight { // Define enum variants here Red, Yellow, Green}pub fn light_action(light: &TrafficLight) -> &'static str { // Your code here... match light { TrafficLight::Red => "Stop", TrafficLight::Yellow => "Caution", TrafficLight::Green => "Go" }}
pub enum TrafficLight { // Define enum variants here Red, Yellow, Green}pub fn light_action(light: &TrafficLight) -> &'static str { // Your code here... match light{ TrafficLight::Red => "Stop", TrafficLight::Yellow => "Caution", TrafficLight::Green => "Go" }}
pub enum TrafficLight { // Define enum variants here Red, Yellow, Green}pub fn light_action(light: &TrafficLight) -> &'static str { // Your code here... match light { TrafficLight::Red => "Stop", TrafficLight::Yellow => "Caution", TrafficLight::Green => "Go" }}
pub enum TrafficLight { // Define enum variants here Red, Yellow, Green,}pub fn light_action(light: &TrafficLight) -> &'static str { // Your code here... match light { TrafficLight::Red => "Stop", TrafficLight::Yellow => "Caution", TrafficLight::Green => "Go", }}
pub enum TrafficLight { Red, Green, Yellow,}pub fn light_action(light: &TrafficLight) -> &'static str { match light { TrafficLight::Red => "Stop", TrafficLight::Yellow => "Caution", TrafficLight::Green => "Go" }}
pub enum TrafficLight { // Define enum variants here Red, Yellow, Green,}pub fn light_action(light: &TrafficLight) -> &'static str { // Your code here... match light { TrafficLight::Red => "Stop", TrafficLight::Yellow => "Caution", TrafficLight::Green => "Go", }}
pub enum TrafficLight { // Define enum variants here Red, Yellow, Green,}pub fn light_action(light: &TrafficLight) -> &'static str { // Your code here... match light { TrafficLight::Red => "Stop", TrafficLight::Yellow => "Caution", TrafficLight::Green => "Go", }}
// Define the TrafficLight enum with three variantspub enum TrafficLight { Red, Yellow, Green,}// Function to return the action associated with a TrafficLight variantpub fn light_action(light: &TrafficLight) -> &'static str { // Match the TrafficLight variant and return the corresponding action match light { TrafficLight::Red => "Stop", TrafficLight::Yellow => "Caution", TrafficLight::Green => "Go", }}// Example usagefn main() { let red_light = TrafficLight::Red; let yellow_light = TrafficLight::Yellow; let green_light = TrafficLight::Green; println!("Red light action: {}", light_action(&red_light)); // Output: Stop println!("Yellow light action: {}", light_action(&yellow_light)); // Output: Caution println!("Green light action: {}", light_action(&green_light)); // Output: Go}
pub enum TrafficLight { // Define enum variants here Red, Yellow, Green,}pub fn light_action(light: &TrafficLight) -> &'static str { // Your code here... match light { TrafficLight::Red => { "Stop" } TrafficLight::Yellow => { "Caution" } TrafficLight::Green => { "Go" } }}
pub enum TrafficLight { Red, Yellow, Green,}pub fn light_action(light: &TrafficLight) -> &'static str { match light { TrafficLight::Red => "Stop", TrafficLight::Yellow => "Caution", TrafficLight::Green => "Go", }}
pub enum TrafficLight { Red, Yellow, Green}pub fn light_action(light: &TrafficLight) -> &'static str { match light { TrafficLight::Red => "Stop", TrafficLight::Yellow => "Caution", TrafficLight::Green => "Go", }}
pub enum TrafficLight { Red, Yellow, Green,}pub fn light_action(light: &TrafficLight) -> &'static str { match light { TrafficLight::Red => { "Stop" } TrafficLight::Yellow => { "Caution" } TrafficLight::Green => { "Go" } }}
pub enum TrafficLight { Red, Yellow, Green,}pub fn light_action(light: &TrafficLight) -> &'static str { match light { TrafficLight::Red => "Stop", TrafficLight::Yellow => "Caution", TrafficLight::Green => "Go", }}
pub enum TrafficLight { Red, Yellow, Green,}pub fn light_action(light: &TrafficLight) -> &'static str { match light { TrafficLight::Red => "Stop", TrafficLight::Yellow => "Caution", TrafficLight::Green => "Go" }}
pub enum TrafficLight { // Define enum variants here Red, Yellow, Green,}pub fn light_action(light: &TrafficLight) -> &'static str { // Your code here... match light { TrafficLight::Red => "Stop", TrafficLight::Yellow => "Caution", TrafficLight::Green => "Go", }}
pub enum TrafficLight { // Define enum variants here Red, Yellow, Green}pub fn light_action(light: &TrafficLight) -> &'static str { // Your code here... match light { TrafficLight::Red => "Stop", TrafficLight::Yellow => "Caution", TrafficLight::Green => "Go" }}
pub enum TrafficLight { Red, Yellow, Green}pub fn light_action(light: &TrafficLight) -> &'static str { match light { TrafficLight::Red => "Stop", TrafficLight::Yellow => "Caution", TrafficLight::Green => "Go" }}
pub enum TrafficLight { // Define enum variants here Red, Yellow, Green}pub fn light_action(light: &TrafficLight) -> &'static str { // Your code here... match light { TrafficLight::Red => "Stop", TrafficLight::Yellow => "Caution", TrafficLight::Green => "Go" }}
pub enum TrafficLight { // Define enum variants here Red, Yellow, Green}pub fn light_action(light: &TrafficLight) -> &'static str { // Your code here... match light { TrafficLight::Red => "Stop", TrafficLight::Yellow => "Caution", TrafficLight::Green => "Go" }}
pub enum TrafficLight { // Define enum variants here Red, Yellow, Green,}pub fn light_action(light: &TrafficLight) -> &'static str { // Your code here... match light { TrafficLight::Red => "Stop", TrafficLight::Yellow => "Caution", TrafficLight::Green => "Go", }}
pub enum TrafficLight { Red, Yellow, Green,}pub fn light_action(light: &TrafficLight) -> &'static str { match light { TrafficLight::Red => "Stop", TrafficLight::Yellow => "Caution", TrafficLight::Green => "Go", }}
pub enum TrafficLight { // Define enum variants here Red, Yellow, Green,}pub fn light_action(light: &TrafficLight) -> &'static str { // Your code here... match &light { TrafficLight::Red => "Stop", TrafficLight::Yellow => "Caution", TrafficLight::Green => "Go", }}
pub enum TrafficLight { Red, Yellow, Green,}pub fn light_action(light: &TrafficLight) -> &'static str { match light { TrafficLight::Red => "Stop", TrafficLight::Yellow => "Caution", TrafficLight::Green => "Go", }}
pub enum TrafficLight { // Define enum variants here Red, Yellow, Green,}pub fn light_action(light: &TrafficLight) -> &'static str { // Your code here... match light { TrafficLight::Red => "Stop", TrafficLight::Yellow => "Caution", TrafficLight::Green => "Go", }}
pub enum TrafficLight { // Define enum variants here Red, Yellow, Green,}pub fn light_action(light: &TrafficLight) -> &'static str { // Your code here... match light { TrafficLight::Red => "Stop", TrafficLight::Yellow => "Caution", TrafficLight::Green => "Go", }}
pub enum TrafficLight { // Define enum variants here Red, Yellow, Green,}pub fn light_action(light: &TrafficLight) -> &'static str { // Your code here... match light { TrafficLight::Red => return "Stop", TrafficLight::Yellow => return "Caution", TrafficLight::Green => return "Go", }}
pub enum TrafficLight { // Define enum variants here Red, Yellow, Green}pub fn light_action(light: &TrafficLight) -> &'static str { // Your code here... match light { TrafficLight::Red => "Stop", TrafficLight::Yellow => "Caution", TrafficLight::Green => "Go", }}
pub enum TrafficLight { // Define enum variants here Red, Yellow, Green,}pub fn light_action(light: &TrafficLight) -> &'static str { // Your code here... match light { TrafficLight::Red => "Stop", TrafficLight::Yellow => "Caution", TrafficLight::Green => "Go", }}
pub enum TrafficLight { // Define enum variants here Red, Yellow, Green,}pub fn light_action(light: &TrafficLight) -> &'static str { // Your code here... match light { TrafficLight::Red => "Stop", TrafficLight::Yellow => "Caution", TrafficLight::Green => "Go", }}
pub enum TrafficLight { Red, Yellow, Green,}pub fn light_action(light: &TrafficLight) -> &'static str { match light { TrafficLight::Red => "Stop", TrafficLight::Yellow => "Caution", TrafficLight::Green => "Go", }}
pub enum TrafficLight { // Define enum variants here Green, Yellow, Red}pub fn light_action(light: &TrafficLight) -> &'static str { // Your code here... match light { TrafficLight::Green => "Go", TrafficLight::Yellow => "Caution", TrafficLight::Red => "Stop" }}
pub enum TrafficLight { // Define enum variants here Red, Yellow, Green}pub fn light_action(light: &TrafficLight) -> &'static str { // Your code here... match light { TrafficLight::Red => "Stop", TrafficLight::Yellow => "Caution", TrafficLight::Green => "Go", }}
pub enum TrafficLight { Red, Yellow, Green,}pub fn light_action(light: &TrafficLight) -> &'static str { match light { TrafficLight::Red => "Stop", TrafficLight::Yellow => "Caution", TrafficLight::Green => "Go", }}
pub enum TrafficLight { // Define enum variants here Red, Yellow, Green}pub fn light_action(light: &TrafficLight) -> &'static str { // Your code here... match light { TrafficLight::Red => "Stop", TrafficLight::Yellow => "Caution", TrafficLight::Green => "Go" }}
pub enum TrafficLight { Red, Yellow, Green,}pub fn light_action(light: &TrafficLight) -> &'static str { match light { TrafficLight::Red => return "Stop", TrafficLight::Yellow => return "Caution", TrafficLight::Green => return "Go" }}
pub enum TrafficLight { Red, Yellow, Green,}pub fn light_action(light: &TrafficLight) -> &'static str { match light { TrafficLight::Red => "Stop", TrafficLight::Yellow => "Caution", TrafficLight::Green => "Go" }}
pub enum TrafficLight { // Define enum variants here Red, Yellow, Green}pub fn light_action(light: &TrafficLight) -> &'static str { // Your code here... match light { TrafficLight::Red => "Stop", TrafficLight::Yellow => "Caution", TrafficLight::Green => "Go" }}
pub enum TrafficLight { // Define enum variants here Red, Yellow, Green}pub fn light_action(light: &TrafficLight) -> &'static str { // Your code here... match light { TrafficLight::Red => "Stop", TrafficLight::Yellow => "Caution", TrafficLight::Green => "Go" }}
pub enum TrafficLight { // Define enum variants here Green, Yellow, Red,}pub fn light_action(light: &TrafficLight) -> &'static str { match light { TrafficLight::Green => "Go", TrafficLight::Yellow => "Caution", TrafficLight::Red => "Stop", }}
pub enum TrafficLight { // Define enum variants here Red, Yellow, Green,}pub fn light_action(light: &TrafficLight) -> &'static str { // Your code here... match light { TrafficLight::Red => "Stop", TrafficLight::Yellow => "Caution", TrafficLight::Green => "Go", }}
pub enum TrafficLight { Red, Yellow, Green}pub fn light_action(light: &TrafficLight) -> &'static str { match light { TrafficLight::Red => "Stop", TrafficLight::Yellow => "Caution", TrafficLight::Green => "Go", }}
pub enum TrafficLight { Red, Yellow, Green,}pub fn light_action(light: &TrafficLight) -> &'static str { match light { TrafficLight::Red => "Stop", TrafficLight::Yellow => "Caution", TrafficLight::Green => "Go", }}
pub enum TrafficLight { // Define enum variants here Red, Yellow, Green,}pub fn light_action(light: &TrafficLight) -> &'static str { // Your code here... match light { TrafficLight::Red => "Stop", TrafficLight::Yellow=> "Caution", TrafficLight::Green =>"Go", }}
pub enum TrafficLight { // Define enum variants here Red, Yellow, Green}pub fn light_action(light: &TrafficLight) -> &'static str { // Your code here... match light{ TrafficLight::Red => "Stop", TrafficLight::Yellow => "Caution", TrafficLight::Green => "Go" }}
pub enum TrafficLight { Red, Green, Yellow}pub fn light_action(light: &TrafficLight) -> &'static str { match light { TrafficLight::Red => "Stop", TrafficLight::Green => "Go", TrafficLight::Yellow => "Caution", }}
pub enum TrafficLight { // Define enum variants here Red, Yellow, Green,}pub fn light_action(light: &TrafficLight) -> &'static str { // Your code here... match light { TrafficLight::Red => "Stop", TrafficLight::Yellow => "Caution", TrafficLight::Green => "Go", }}
pub enum TrafficLight { // Define enum variants here Red, Yellow, Green}pub fn light_action(light: &TrafficLight) -> &'static str { match light{ TrafficLight::Red=> "Stop", TrafficLight::Yellow=> "Caution", TrafficLight::Green=> "Go", }}
pub enum TrafficLight { Red, Yellow, Green,}pub fn light_action(light: &TrafficLight) -> &'static str { match light { TrafficLight::Red => "Stop", TrafficLight::Yellow => "Caution", TrafficLight::Green => "Go", }}