<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/the-heat-is-on-handling-units-in-rust-j0klajwwg" -->

---
title: The heat is on: handling units in Rust | daily.dev
description: This tutorial adds a Fahrenheit display option to a Rust weather CLI and crate, then uses the exercise to teach the newtype pattern for handling units safely....
canonical: https://daily.dev/posts/the-heat-is-on-handling-units-in-rust-j0klajwwg
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: The heat is on: handling units in Rust | daily.dev
og:description: This tutorial adds a Fahrenheit display option to a Rust weather CLI and crate, then uses the exercise to teach the newtype pattern for handling units safely....
og:url: https://daily.dev/posts/the-heat-is-on-handling-units-in-rust-j0klajwwg
og:image: https://api.daily.dev/og/posts/J0klAjWWG.png
og:image:alt: The heat is on: handling units in Rust
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# The heat is on: handling units in Rust

**[Bitfield Consulting](https://daily.dev/sources/bitfieldconsulting)** · 9 min read · 1 upvotes · 0 comments

## Summary

This tutorial adds a Fahrenheit display option to a Rust weather CLI and crate, then uses the exercise to teach the newtype pattern for handling units safely. A naive 'into_fahrenheit' method on the Weather struct works but leaves temperature units ambiguous and unsafe to convert twice. The fix wraps f64 in tuple structs like Celsius, Fahrenheit, and a unit-agnostic Temperature type, with from_celsius and a conversion method. The post notes that Rust's type system can help prevent disasters like the Mars Climate Orbiter failure, and warns the same Temperature type is wrong for representing temperature differences rather than absolute values.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://bitfieldconsulting.com/posts/heat-is-on>

## Questions this post answers

### How do I use the newtype pattern in Rust to represent a temperature with units like Celsius or Fahrenheit?

Wrap the f64 value in a tuple struct, such as 'struct Celsius(f64)' or 'struct Fahrenheit(f64)', so the type itself conveys the unit system. You can then add conversion methods like 'from_celsius' and 'to_fahrenheit' on a unit-agnostic 'Temperature' struct, accessing the inner value with '.0' for an unnamed tuple field.

_Developers modeling physical quantities safely in Rust can revisit patterns like this one on daily.dev._

### Why shouldn't I use a plain f64 to represent a temperature value in Rust?

A plain f64 only stores a number, not the unit system it's measured in, so code calling a conversion method twice on an already-converted value silently produces a wrong result. This kind of unit confusion is blamed for real failures such as the Mars Climate Orbiter loss, where mixing metric and imperial units caused a navigation error.

_Anyone wiring up typed APIs in Rust can browse more type-safety techniques like this on daily.dev._

## Similar posts on daily.dev

- [When Rust Gets Ugly](https://daily.dev/posts/when-rust-gets-ugly-tyyir3taq) · corrode · 17 upvotes · 0 comments
- [Rust language adds algebraic floating-point methods](https://daily.dev/posts/rust-language-adds-algebraic-floating-point-methods-jimyxkos8) · InfoWorld · 1 upvotes · 0 comments
- [Where the sun keeps shinin': the provider pattern in Rust — Bitfield Consulting](https://daily.dev/posts/where-the-sun-keeps-shinin-the-provider-pattern-in-rust-bitfield-consulting-hqdyj4a3n) · Bitfield Consulting · 2 upvotes · 0 comments
- [some C habits I employ for the modern day](https://daily.dev/posts/some-c-habits-i-employ-for-the-modern-day-xxclsjssp) · Lobsters · 2 upvotes · 0 comments
- [Give Your Weather Report an Animated ASCII Spin in Linux Terminal](https://daily.dev/posts/give-your-weather-report-an-animated-ascii-spin-in-linux-terminal-my7vtl77b) · It's Foss · 1 upvotes · 0 comments

---

Tags: [#rust](https://daily.dev/tags/rust)

[View this post on daily.dev](https://daily.dev/posts/the-heat-is-on-handling-units-in-rust-j0klajwwg)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"The heat is on: handling units in Rust","url":"https://daily.dev/posts/the-heat-is-on-handling-units-in-rust-j0klajwwg","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/the-heat-is-on-handling-units-in-rust-j0klajwwg"},"datePublished":"2026-10-27T09:05:50.832Z","dateModified":"2026-10-27T09:06:23.304Z","description":"This tutorial adds a Fahrenheit display option to a Rust weather CLI and crate, then uses the exercise to teach the newtype pattern for handling units safely....","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/5856856c941a989af9729fafb1116aa8?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/5856856c941a989af9729fafb1116aa8?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"Bitfield Consulting","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Bitfield Consulting","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/47364ed1cd4f42b289127844bad2b204","url":"https://daily.dev/sources/bitfieldconsulting"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/the-heat-is-on-handling-units-in-rust-j0klajwwg","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":1},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"rust","timeRequired":"PT9M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Bitfield Consulting","item":"https://daily.dev/sources/bitfieldconsulting"},{"@type":"ListItem","position":3,"name":"The heat is on: handling units in Rust"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/the-heat-is-on-handling-units-in-rust-j0klajwwg#faq","mainEntity":[{"@type":"Question","name":"How do I use the newtype pattern in Rust to represent a temperature with units like Celsius or Fahrenheit?","acceptedAnswer":{"@type":"Answer","text":"Wrap the f64 value in a tuple struct, such as 'struct Celsius(f64)' or 'struct Fahrenheit(f64)', so the type itself conveys the unit system. You can then add conversion methods like 'from_celsius' and 'to_fahrenheit' on a unit-agnostic 'Temperature' struct, accessing the inner value with '.0' for an unnamed tuple field. Developers modeling physical quantities safely in Rust can revisit patterns like this one on daily.dev."}},{"@type":"Question","name":"Why shouldn't I use a plain f64 to represent a temperature value in Rust?","acceptedAnswer":{"@type":"Answer","text":"A plain f64 only stores a number, not the unit system it's measured in, so code calling a conversion method twice on an already-converted value silently produces a wrong result. This kind of unit confusion is blamed for real failures such as the Mars Climate Orbiter loss, where mixing metric and imperial units caused a navigation error. Anyone wiring up typed APIs in Rust can browse more type-safety techniques like this on daily.dev."}}]}
```

