Simple Calculations
Level: easy (score: 2)
💡 Individual exercises are great, but nothing beats building a real-world app with Rust.
Check out our Rust Intro Cohort Program!
Implement two functions:
celsius_to_fahrenheit
that converts a temperature from Celsius to Fahrenheit.is_even
that checks if a number is even.
Given the following inputs, the functions should return the expected results:
let temp_f = celsius_to_fahrenheit(0.0); // 32.0 let is_num_even = is_even(2); // true
fn celsius_to_fahrenheit(celsius: f64) -> f64 {
// Implement conversion from Celsius to Fahrenheit
}
fn is_even(n: u32) -> bool {
// Implement check for even number
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_celsius_to_fahrenheit() {
assert_eq!(celsius_to_fahrenheit(0.0), 32.0);
assert_eq!(celsius_to_fahrenheit(100.0), 212.0);
assert_eq!(celsius_to_fahrenheit(-40.0), -40.0);
}
#[test]
fn test_is_even() {
assert_eq!(is_even(2), true);
assert_eq!(is_even(3), false);
assert_eq!(is_even(0), true);
}
}