Build a temperature converter
Quick answer
Combine functions, numeric expressions and arrays to build a small Celsius-to-Fahrenheit converter. The conversion logic should be separate from displaying the result, so you can reuse and check it.
Plan and verify the conversion
For Celsius c, Fahrenheit is c * 9.0 / 5.0 + 32.0. Use f64 so division preserves fractional values. A useful check is that 0°C gives 32°F and 100°C gives 212°F. Format with :.1 to display one decimal place without changing the underlying result.
fn fahrenheit(c: f64) -> f64 { c * 9.0 / 5.0 + 32.0 }fn main() { println!("{:.1}", fahrenheit(25.0)); }Expected stdout (not executed):
77.0The converter returns a number and main controls display.
fn fahrenheit(c: f64) -> f64 { c * 9.0 / 5.0 + 32.0 }fn main() { for c in [0.0, 100.0, -40.0] { println!("{c:.1} C -> {:.1} F", fahrenheit(c)); }}Expected stdout (not executed):
0.0 C -> 32.0 F100.0 C -> 212.0 F-40.0 C -> -40.0 FThe three cases include freezing, boiling and the point where both scales agree.
Common mistake
Using integer 9 / 5 yields 1, not 1.8. Use floating-point operands for this formula.
Try it yourself
Add the reverse converter (f - 32.0) * 5.0 / 9.0 and test 77°F and -40°F.
Static examples for Rust 1.85.0, edition 2024. Run them locally in a disposable project; ByteMind execution support and these outputs have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Build a temperature converter — practice quiz
Choose one answer per question, then review the explanation.
What Fahrenheit value corresponds to -40°C?
Why use `9.0 / 5.0` instead of `9 / 5` for the conversion factor?
Does displaying a temperature with `{f:.1}` change the stored f64 value?