Jump to content

Programming Fundamentals/Variables/Rust

From Wikiversity

variables.rs

[edit | edit source]
// This program converts a Fahrenheit temperature to Celsius.
//
// References:
// https://www.geeksforgeeks.org/standard-i-o-in-rust/
// https://www.includehelp.com/rust/read-a-float-number-from-the-user.aspx

use std::io;

fn main() {
    println!("Enter Fahrenheit temperature:");
    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");

    let celsius = (fahrenheit - 32.0) * 5.0 / 9.0;
    
    println!("{}° Fahrenheit is {}° Celsius", fahrenheit, celsius);
}

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]