Jump to content

Programming Fundamentals/Conditions/Rust

From Wikiversity

conditions.rs

[edit | edit source]
// This program asks the user to select Fahrenheit or Celsius conversion
// and input a given temperature. Then the program converts the given 
// temperature and displays the result.
//
// References:
// https://www.geeksforgeeks.org/standard-i-o-in-rust/
// https://www.includehelp.com/rust/read-a-float-number-from-the-user.aspx
// https://doc.rust-lang.org/book/ch03-03-how-functions-work.html
// https://blog.thoughtram.io/string-vs-str-in-rust/
// https://doc.rust-lang.org/rust-by-example/flow_control/if_else.html
// https://doc.rust-lang.org/rust-by-example/flow_control/match.html
// https://doc.rust-lang.org/book/appendix-02-operators.html

use std::io;

fn main() {
    // main could either be an if-else structure or a match structure
    
    // get String and convert to slice (&str)
    let choice = &get_choice()[..];
    
    // if-else approach
    if choice == "C" || choice == "c" {
        let temperature = get_temperature("Fahrenheit");
        let result = calculate_celsius(temperature);
        display_result(temperature, "Fahrenheit", result, "Celsius");
    } else if choice == "F" || choice == "f" {
        let temperature = get_temperature("Celsius");
        let result = calculate_fahrenheit(temperature);
        display_result(temperature, "Celsius", result, "Fahrenheit");
    } else {
        println!("You must enter C to convert to Celsius or F to convert to Fahrenheit.")
    }

    // match approach
    match choice {
        "C" | "c" => {
            let temperature = get_temperature("Fahrenheit");
            let result = calculate_celsius(temperature);
            display_result(temperature, "Fahrenheit", result, "Celsius");
        }
        
        "F" | "f" => {
            let temperature = get_temperature("Celsius");
            let result = calculate_fahrenheit(temperature);
            display_result(temperature, "Celsius", result, "Fahrenheit");
        }
        
        _ => {
            println!("You must enter C to convert to Celsius or F to convert to Fahrenheit.")
        }
    }
    
}

fn get_choice() -> String {
    println!("Enter C to convert to Celsius or F to convert to Fahrenheit:");
    let mut line = String::new();
    io::stdin().read_line(&mut line).expect("Error reading line");
    let choice:String = line.trim().to_string();
    return choice;
}

fn get_temperature(label:&str) -> f32 {
    println!("Enter {} temperature:", label);
    let mut line = String::new();
    io::stdin().read_line(&mut line).expect("Error reading line");
    let fahrenheit:f32 = line.trim().parse().expect("Not a valid number");
    return fahrenheit;
}

fn calculate_celsius(fahrenheit:f32) -> f32 {
    let celsius = (fahrenheit - 32.0) * 5.0 / 9.0;
    return celsius;    
}

fn calculate_fahrenheit(celsius:f32) -> f32 {
    let fahrenheit = celsius * 9.0 / 5.0 + 32.0;
    return fahrenheit;    
}

fn display_result(temperature:f32, from_label:&str, result:f32, to_label:&str) {
    println!("{}° {} is {}° {}", temperature, from_label, result, to_label);
}

Try It

[edit | edit source]

Copy and paste the code above into one of the following free online development environments or use your own Rust compiler / interpreter / IDE.

See Also

[edit | edit source]