Loops in Rust: The Power of `for`
Level: intro (score: 1)
In this exercise, you'll implement a function to compute the sum of numbers within a given range using Rust's for
loop.
Task
Implement the function: sum_numbers(start: u32, end: u32) -> u32
- Use a for
loop over an inclusive range (start..=end
) to compute the sum.
- If start
is greater than end
, return 0
.
- Think of iterating over a fixed set of items, much like processing daily sales records from a known date range.
Hints
- Use
start..=end
to create an inclusive range. - Check if
start > end
before looping (imagine this function as a counter adding up sales revenue between two given dates. If the range is invalid (start > end
), no sales are counted).
Happy coding! 🚀