Pybites Logo Rust Platform

Control Flow: If Statements & Conditions

Level: intro (score: 1)

In this exercise, you'll implement a function that returns a clothing recommendation based on the current temperature (in Celsius).

Your function should accept a temperature as an f32 and return an appropriate message as a static string slice (&'static str).

Imagine you're building a simple weather app—just like a choose‐your‐own‐adventure story where the weather decides your outfit!

For example, if it’s freezing outside, you’d want to bundle up; if it’s warm, a t‑shirt might be just right.

Use a series of if/else if/else statements to cover the following scenarios:

  • If the temperature is below 0.0°C, return: "Brr! It's freezing. Wear a heavy coat and scarf!"
  • If the temperature is 0.0°C or above but less than 10.0°C, return: "Chilly weather. Don't forget your jacket."
  • If the temperature is 10.0°C or above but less than 20.0°C, return: "Cool day. A sweater should do."
  • If the temperature is 20.0°C or above but less than 30.0°C, return: "Warm and pleasant. A t-shirt is fine."
  • Otherwise (30.0°C or above), return: "Hot day. Stay cool with shorts and a hat!"

Hint: Use an if/else if/else chain to check the conditions in order. Think of it as the code deciding your outfit—just like choosing your adventure!

Note: We need to start with if/else here. More advanced Rust coders will use match with if guards for conditions and code demonstration. We'll cover that in a future exercise ...

Happy coding, learn Rust one Bite at a time!