4
\$\begingroup\$

It's one of the tasks suggested in the Rust Programming Language book by Steve Klabnik and Carol Nichols, and to a complete Rust newbie like me, figuring out converting scale from String to Vec<char>, was a bit of a challenge and a new concept, coming from Python.

I'd appreciate constructive criticism of what I've done wrong and right, pitfalls I may have fallen into, and possible optimizations available to be introduced to my code below.

use std::io;

fn main() {
    println!("Please type `exit` to quit the program.");

    loop {
        println!("Please input source scale.\n`C` for Celsius, `F` for Fahrenheit.");

        let mut scale = String::new();

        io::stdin()
            .read_line(&mut scale)
            .expect("Failed to read input.");

        if scale.trim() == "exit" {
            break;
        }

        let scale = scale.trim().to_uppercase();

        let scale: Vec<char> = scale.chars().collect();

        let scale = scale[0];

        let scale = match scale {
            'C' => 'C',
            'F' => 'F',
            _ => {
                println!("Incorrect scale.");
                continue;
            }
        };

        println!("Please input your source temperature with a trailing dot.");

        let mut temperature = String::new();

        io::stdin()
            .read_line(&mut temperature)
            .expect("Failed to read input.");

        if temperature.trim() == "exit" {
            break;
        }

        let temperature: f64 = match temperature.trim().parse() {
            Ok(num) => num,
            Err(_) => {
                println!("Please input a valid temperature.");
                continue;
            }
        };

        println!(
            "Converted temperature is: {}.",
            convert_temperature(scale, temperature)
        );
    }
}

fn convert_temperature(scale: char, temperature: f64) -> f64 {
    let converted_temperature: f64 = if scale == 'F' {
        (temperature as f64 - 32.0) / 1.8
    } else {
        temperature as f64 * 1.8 + 32.0
    };

    return converted_temperature;
}

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

You don't need to convert the strings to characters. This will make things much simpler while handling text.

use std::io;

fn main() {
    println!("Please type `exit` to quit the program.");

    loop {
        println!("Please input source scale.\n`C` for Celsius, `F` for Fahrenheit.");

        let mut scale = String::new();

        io::stdin()
            .read_line(&mut scale)
            .expect("Failed to read input.");

        if scale.trim().to_lowercase() == "exit" {
            break;
        }

        let scale = scale.trim().to_uppercase();
        if scale.ne("C") && scale.ne("F") {
                println!("Incorrect scale.");
                continue;
        };

        println!("Please input your source temperature: ");

        let mut temperature = String::new();

        io::stdin()
            .read_line(&mut temperature)
            .expect("Failed to read input.");

        if temperature.trim().to_lowercase() == "exit" {
            break;
        }

        let temperature: f64 = match temperature.trim().parse() {
            Ok(num) => num,
            Err(_) => {
                println!("Please input a valid temperature.");
                continue;
            }
        };

        println!(
            "Converted temperature is: {}.",
            convert_temperature(scale, temperature)
        );
    }
}

fn convert_temperature(scale: String, temperature: f64) -> f64 {
    let converted_temperature: f64 = if scale == "F" {
        (temperature as f64 - 32.0) / 1.8
    } else {
        temperature as f64 * 1.8 + 32.0
    };

    return converted_temperature;
}

s1.ne(s2) is a string function that checks if s1 is not equal to s2.

There is also no need for asking for a dot at the end of the number to be entered by the user.

\$\endgroup\$
4
  • 1
    \$\begingroup\$ It is much more common to use scale != "C" instead of scale.ne("C"). \$\endgroup\$
    – L. F.
    May 24 at 18:18
  • \$\begingroup\$ I agree. Wonder if there is any difference between these two? \$\endgroup\$
    – rnso
    May 25 at 1:24
  • 1
    \$\begingroup\$ They are semantically identical, but I have almost never seen ne directly used "in the wild." In fact, it took me a while before I realized that ne is the method that != desugars into. \$\endgroup\$
    – L. F.
    May 25 at 1:38
  • 1
    \$\begingroup\$ The function convert_temperature should be split into F2C() and C2F() functions. \$\endgroup\$
    – CWallach
    May 25 at 22:00

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.