Iterate over enum
Rust Programming Language
Problem
Rust program that defines an enum and iterates over its values.
Input
enum Fruit {Apple,Banana,Cherry,}fn main() {let fruits = [Fruit::Apple, Fruit::Banana, Fruit::Cherry];for fruit in fruits.iter() {match fruit {Fruit::Apple => println!("I like apples!"),Fruit::Banana => println!("Bananas are yummy!"),Fruit::Cherry => println!("Cherries are sweet!"),}}}{codeBox}
Output
I like apples!Bananas are yummy!Cherries are sweet!{codeBox}
Explanation
In this program, we define an enum called Fruit that has three variants: Apple, Banana, and Cherry. We then create an array of Fruit values containing one of each variant, and use a for loop to iterate over the values. Inside the loop, we use a match statement to print a message corresponding to each fruit.