0
0
Rustprogramming~30 mins

Basic match usage in Rust - Mini Project: Build & Apply

Choose your learning style9 modes available
Basic match usage
๐Ÿ“– Scenario: Imagine you are building a simple program that checks the weather condition and gives a message based on it.
๐ŸŽฏ Goal: You will create a variable for the weather condition, then use a match statement to print a message depending on the weather.
๐Ÿ“‹ What You'll Learn
Create a variable called weather with the exact value "sunny".
Create a variable called temperature with the exact value 25.
Use a match statement on the weather variable with arms for "sunny", "rainy", and a catch-all _.
Print the message for the matched weather condition.
๐Ÿ’ก Why This Matters
๐ŸŒ Real World
Using <code>match</code> is common in Rust programs to handle different cases clearly and safely, like responding to user input or different program states.
๐Ÿ’ผ Career
Understanding <code>match</code> helps you write clear and bug-free Rust code, a skill valued in systems programming, embedded devices, and backend development.
Progress0 / 4 steps
1
Create the weather and temperature variables
Create a variable called weather and set it to the string "sunny". Also create a variable called temperature and set it to the number 25.
Rust
Need a hint?

Use let to create variables. Strings use double quotes.

2
Add a match statement for weather
Add a match statement on the variable weather. Create arms for "sunny", "rainy", and a catch-all arm _. For now, just write empty blocks {} for each arm.
Rust
Need a hint?

Use match weather { ... } with arms separated by commas.

3
Add messages inside match arms
Inside the match arms, add println! statements to print these exact messages:
For "sunny": "It's a bright sunny day!"
For "rainy": "Don't forget your umbrella."
For the catch-all _: "Weather unknown."
Rust
Need a hint?

Use println!("message") inside each arm's block.

4
Print the temperature after the match
After the match statement, add a println! statement to print the temperature in this exact format: "The temperature is 25 degrees." Use the variable temperature inside the message with an f-string style placeholder.
Rust
Need a hint?

Use println!("The temperature is {} degrees.", temperature); to print the temperature.