📖 Scenario: You are building a simple program to represent different types of messages in a chat application. Each message can be a text message, an image with a URL, or a location with latitude and longitude.
🎯 Goal: Create an enum called Message with different variants holding data, then write code to handle each message type and print a description.
📋 What You'll Learn
Create an enum Message with variants Text, Image, and Location
The Text variant holds a String
The Image variant holds a String representing the URL
The Location variant holds two f64 values for latitude and longitude
Create a variable msg of type Message with the Image variant and URL "https://example.com/pic.png"
Use a match expression on msg to print a message describing the content
💡 Why This Matters
🌍 Real World
Enums with data are useful to represent different types of related information in one variable, like messages, events, or commands.
💼 Career
Understanding enums with data is important for Rust programming jobs, especially in systems programming, web backend, and applications requiring safe and clear data handling.
Progress0 / 4 steps
1
Create the Message enum with data
Create an enum called Message with three variants: Text(String), Image(String), and Location(f64, f64).
Rust
Hint
Use the enum keyword and define variants with data inside parentheses.
2
Create a variable msg with the Image variant
Create a variable called msg of type Message and assign it the Image variant with the URL "https://example.com/pic.png".
Rust
Hint
Use let to create msg and Message::Image(String::from(...)) to assign the variant.
3
Use match to handle msg and print descriptions
Write a match expression on msg with arms for Text, Image, and Location. For each, bind the data to variables and print a descriptive message using println!. Use text for Text, url for Image, and lat, lon for Location.
Rust
Hint
Use match with pattern matching to extract data and print messages.
4
Print the message description
Run the program to print the description of the msg variable. The output should be: Image message with URL: https://example.com/pic.png
Rust
Hint
Make sure to wrap your code in a main function and run it to see the output.
Practice
(1/5)
1. What is the main purpose of enums with data in Rust?
easy
A. To group related values that can be different types under one name
B. To create multiple copies of the same data
C. To perform arithmetic operations on numbers
D. To store only strings in a list
Solution
Step 1: Understand what enums with data do
Enums with data allow each variant to hold different types or amounts of data, grouping related but different information.
Step 2: Compare options with this understanding
Only To group related values that can be different types under one name correctly describes grouping related values of different types under one name.
Final Answer:
To group related values that can be different types under one name -> Option A
Quick Check:
Enums with data = group different types [OK]
Hint: Enums group different data types under one name [OK]
Common Mistakes:
Thinking enums only store one data type
Confusing enums with arrays or vectors
Assuming enums perform calculations
2. Which of the following is the correct syntax to define an enum variant with data in Rust?
easy
A. enum Color { Red(u8, u8, u8), Blue }
B. enum Color { Red = (u8, u8, u8), Blue }
C. enum Color { Red: (u8, u8, u8), Blue }
D. enum Color { Red -> (u8, u8, u8), Blue }
Solution
Step 1: Recall enum variant syntax with data
In Rust, enum variants with data are defined by listing the variant name followed by parentheses containing the data types.
Step 2: Check each option
enum Color { Red(u8, u8, u8), Blue } uses correct syntax: Red(u8, u8, u8). Options A, B, and C use invalid syntax with ->, =, or : which are incorrect for enum variant data.
Final Answer:
enum Color { Red(u8, u8, u8), Blue } -> Option A
Quick Check:
Variant with data uses parentheses () [OK]
Hint: Use parentheses () after variant name for data [OK]