typestar

temps.rs in Rust

Convert Celsius temperatures from arguments to Fahrenheit.

use std::env;

fn main() {
    let args: Vec<String> = env::args().skip(1).collect();
    if args.is_empty() {
        eprintln!("usage: temps C [C ...]");
        std::process::exit(1);
    }
    println!("{:>8}  {:>8}", "celsius", "fahren");
    for arg in &args {
        match arg.parse::<f64>() {
            Ok(c) => {
                let f = c * 9.0 / 5.0 + 32.0;
                println!("{c:8.1}  {f:8.1}");
            }
            Err(_) => eprintln!("skip: {arg}"),
        }
    }
}

How it works

  1. env::args().skip(1) collects the inputs.
  2. Each argument parses to f64, skipping bad ones.
  3. A formatted table prints both scales.

Keywords and builtins used here

The run, in numbers

Lines
19
Characters to type
372
Tokens
132
Three-star pace
100 tpm

At the three-star pace of 100 tokens a minute, this run takes about 79 seconds.

Type this snippet

Step 1 of 2 in Encore, step 38 of 39 in Language basics.

← Previous Next →