Help us measure site usage?

With your permission, we use self-hosted analytics to understand page usage. It is optional and does not affect the Service. Learn more.

RustbeginnercliAI generated

Temperature Converter CLI with Clap

Build a small command-line tool in Rust that converts temperatures between Celsius, Fahrenheit and Kelvin. You run it like this: tempconv 100 --from c --to f, and it prints 100.00 °C = 212.00 °F. Invalid input, such as an unknown unit or a temperature below absolute zero, gets a clear error message instead of a crash. It's a good first Rust project: you'll learn how to use Cargo, define command-line arguments with the clap crate, use enums and match, write functions that return Result, and write unit tests.

Estimate
~7h
Steps
5
Completed by
0
Proposed by
codeseed.app

clap

Project roadmap

  1. 01

    Create the project

    ~1h

    Install Rust with rustup if you don't have it, then run cargo new tempconv. Add clap with the derive feature by running cargo add clap --features derive. Run cargo run to check that the default Hello, world! program works. Done when: the project builds and clap is listed as a dependency in Cargo.toml.

  2. 02

    Define the arguments with clap

    ~1.5h

    Create a Unit enum with the variants Celsius, Fahrenheit and Kelvin, deriving clap's ValueEnum, and give each variant a short alias such as #[value(alias = "c")] so the user can type c, f or k. Then define an Args struct with #[derive(Parser)] holding the value (an f64) and two options, --from and --to, both of type Unit. Parse it in main and print what was received. Done when: tempconv 100 --from c --to f prints the parsed values, and --help shows all three arguments.

  3. 03

    Write the conversion

    ~2h

    Write a function convert(value: f64, from: Unit, to: Unit) -> Result<f64, String>. Convert the input to Kelvin first, then from Kelvin to the target unit, using match on the enums. Return an error when the input is below absolute zero (0 K, -273.15 °C or -459.67 °F). Converting a unit to itself returns the same value. Done when: main calls convert and prints the result.

  4. 04

    Print results and errors clearly

    ~1h

    Print the result with two decimals and the unit symbols, for example 100.00 °C = 212.00 °F. If convert returns an error, print it to stderr with eprintln! and exit with code 1 using std::process::exit. Done when: a valid conversion prints the formatted line, and tempconv -300 --from c --to k prints an error and exits with code 1.

  5. 05

    Add unit tests

    ~1.5h

    In a #[cfg(test)] module, write tests for all six conversion pairs using known values (0 °C = 32 °F, 100 °C = 373.15 K and so on), a same-unit conversion, a round trip (Celsius to Fahrenheit and back returns the original value within 0.0001), and the below-absolute-zero error. Run them with cargo test. Done when: cargo test passes with at least 9 tests.

Ready to build this?

Get a GitHub repo and start building. Your AI reviewer checks each step as you go.

~7h · 5 steps

Tech stack

clap