The situation
It is 03:00 at the Kestrel Ridge weather station. The wind is doing something unpleasant to the antenna mast, the coffee is a memory, and the process that prints the hourly sensor report just died with an error message nobody has bothered to translate from the original Fortran.
The paper log printer still works. The sensors still work. What is missing is the code between them — four small functions the report generator calls once per sensor. The 06:00 handover expects a stack of readable reports.
That's you. Four functions, no external crates, std only.
Your mission
Implement these four functions exactly as specified.
1. to_fahrenheit(celsius: f64) -> f64
The overnight logbook is still in Fahrenheit because the station is older than the metric system's reputation.
| Input | Output |
|---|---|
0.0 | 32.0 |
100.0 | 212.0 |
-40.0 | -40.0 |
21.5 | 70.7 |
2. classify(celsius: f64) -> String
Each reading gets a band label. The boundaries matter — the shift supervisor reads these, and "COLD" at exactly zero has caused an argument before.
| Reading | Label |
|---|---|
below 0.0 | FREEZING |
0.0 up to (but not including) 10.0 | COLD |
10.0 up to (but not including) 25.0 | MILD |
25.0 and above | HOT |
3. bar(celsius: f64) -> String
The printer is a dot-matrix unit, so charts are made of #. One # for every full 5 °C above zero, never more than 8, never negative.
| Reading | Bar |
|---|---|
-3.2 | (empty string) |
0.0 | (empty string) |
4.9 | (empty string) |
5.0 | # |
21.5 | #### |
40.0 | ######## |
100.0 | ######## (clamped) |
4. format_row(label: &str, celsius: f64) -> String
One line of the report, assembled from the parts above. This is the whole point of the exercise: fixed columns, so a stack of rows lines up on paper.
The layout, left to right:
| Segment | Width | Rule |
|---|---|---|
| sensor label | 10 | left aligned, padded with spaces |
| temperature | 7 | right aligned, exactly one decimal |
°C + 2 spaces | – | literal |
| bar | 8 | left aligned, padded with spaces |
| 2 spaces + band label | – | literal, then the label from classify |
In other words, exactly this format string:
format!("{label:<10}{celsius:>7.1}°C {bar:<8} {band}")Worked examples — copy these into main and eyeball them:
format_row("cabin", 21.5) -> "cabin 21.5°C #### MILD"
format_row("outside", -3.2) -> "outside -3.2°C FREEZING"
format_row("engine bay", 68.0) -> "engine bay 68.0°C ######## HOT"Note the third one: a label that already fills its 10 columns is not truncated, it just doesn't get padding. And no row ever ends in a space.
How you're graded
Press Check and the station's own test harness runs against your code. You cannot see the tests, but you can see their names when they fail:
warmup_fahrenheit_conversionclassify_hits_every_bandclassify_boundaries_are_exact← the argument-causing onebar_scales_and_clampsformat_row_composes_the_whole_lineformat_row_handles_a_full_width_label
Run compiles and runs main without grading, so use it to print things while you work.
Stretch goals
Nothing here is graded — they are for when the four functions pass and you still have shift left.
- Print a full report in
mainwith a header row that lines up with the columns. - Add a
°Fcolumn toformat_rowusingto_fahrenheit, at width 7 with one decimal. - Rewrite
barwithout an explicit loop. There is astrmethod that repeats a string. - Make
classifyreturn&'static strinstead ofStringand see which call sites the compiler makes you change. (Chapter 5 explains why that version is cheaper.)