3
\$\begingroup\$

Introduction

I am recently learning Rust by reading The Rust Programming Language (a.k.a. the book). I have finished the first three chapters so far, and this is my attempt for the first exercise listed at the end of Chapter 3, which has a simple description:

Convert temperatures between Fahrenheit and Celsius.

My program repetitively prompts the user to input for a command (one of \${^{\circ}\text{F}} \to {^{\circ}\text{C}}\$, \${^{\circ}\text{C}} \to {^{\circ}\text{F}}\$, and quit), and asks for temperature values to be converted as floating-point values. If the user input cannot be properly interpreted, the program sends an error message and resumes executing if possible.

I have run rustfmt and clippy on my program. rustfmt taught me something new (removing , after {} in a match expression), and clippy did not report any problems. I have also tested the program with different inputs, and so far it seems to work properly.

Before moving on, I would like to know whether I am in the right direction. Feel free to point out edge cases I neglected, failures to conform to received Rust guidelines, or other things that I did wrong, so I can correct these mistakes as soon as possible and learn productively in the future.

Code

src/main.rs

use std::io;

fn main() {
    println!("This is a program that converts temperatures between Fahrenheit and Celsius.");

    loop {
        println!("\nEnter one of the following commands:");
        println!(" - 0: convert from Fahrenheit to Celsius");
        println!(" - 1: convert from Celsius to Fahrenheit");
        println!(" - 2: quit");

        let mut command = String::new();
        match io::stdin().read_line(&mut command) {
            Ok(_) => {}
            Err(_) => {
                println!("Failed to read command.");
                continue;
            }
        }

        let command: u32 = match command.trim().parse() {
            Ok(num) => num,
            Err(_) => {
                println!("Invalid command.");
                continue;
            }
        };
        match command {
            0 => fahrenheit_to_celsius(),
            1 => celsius_to_fahrenheit(),
            2 => break,
            _ => println!("Invalid command."),
        }
    }
}

fn fahrenheit_to_celsius() {
    println!("Enter the temperature in Fahrenheit.");

    let mut temperature = String::new();
    match io::stdin().read_line(&mut temperature) {
        Ok(_) => {}
        Err(_) => {
            println!("Failed to read temperature.");
            return;
        }
    }

    let temperature: f64 = match temperature.trim().parse() {
        Ok(t) => t,
        Err(_) => {
            println!("Invalid temperature.");
            return;
        }
    };
    let converted = (temperature - 32.0) / 1.8;
    println!("{} Fahrenheit = {} Celsius", temperature, converted);
}

fn celsius_to_fahrenheit() {
    println!("Enter the temperature in Celsius.");

    let mut temperature = String::new();
    match io::stdin().read_line(&mut temperature) {
        Ok(_) => {}
        Err(_) => {
            println!("Failed to read temperature.");
            return;
        }
    }

    let temperature: f64 = match temperature.trim().parse() {
        Ok(t) => t,
        Err(_) => {
            println!("Invalid temperature.");
            return;
        }
    };
    let converted = temperature * 1.8 + 32.0;
    println!("{} Celsius = {} Fahrenheit", temperature, converted);
}

Cargo.toml

[package]
name = "temperature"
version = "0.1.0"
authors = ["L. F."]
edition = "2018"

[dependencies]

Example session

Here's an example cargo run session:

This is a program that converts temperatures between Fahrenheit and Celsius.

Enter one of the following commands:
 - 0: convert from Fahrenheit to Celsius
 - 1: convert from Celsius to Fahrenheit
 - 2: quit
0
Enter the temperature in Fahrenheit.
200
200 Fahrenheit = 93.33333333333333 Celsius

Enter one of the following commands:
 - 0: convert from Fahrenheit to Celsius
 - 1: convert from Celsius to Fahrenheit
 - 2: quit
0
Enter the temperature in Fahrenheit.
123.4
123.4 Fahrenheit = 50.77777777777778 Celsius

Enter one of the following commands:
 - 0: convert from Fahrenheit to Celsius
 - 1: convert from Celsius to Fahrenheit
 - 2: quit
0
Enter the temperature in Fahrenheit.
cat
Invalid temperature.

Enter one of the following commands:
 - 0: convert from Fahrenheit to Celsius
 - 1: convert from Celsius to Fahrenheit
 - 2: quit
1
Enter the temperature in Celsius.
1.98e5
198000 Celsius = 356432 Fahrenheit

Enter one of the following commands:
 - 0: convert from Fahrenheit to Celsius
 - 1: convert from Celsius to Fahrenheit
 - 2: quit
1
Enter the temperature in Celsius.
unicorn
Invalid temperature.

Enter one of the following commands:
 - 0: convert from Fahrenheit to Celsius
 - 1: convert from Celsius to Fahrenheit
 - 2: quit
3
Invalid command.

Enter one of the following commands:
 - 0: convert from Fahrenheit to Celsius
 - 1: convert from Celsius to Fahrenheit
 - 2: quit
dog
Invalid command.

Enter one of the following commands:
 - 0: convert from Fahrenheit to Celsius
 - 1: convert from Celsius to Fahrenheit
 - 2: quit
2
\$\endgroup\$

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.