Sunday, July 17, 2016

Fahrenheit To Celsius Program in Rust

A simple program that I wrote that will ask the user the temperature in Fahrenheit and then it will convert into Celsius equivalent using Rust as my programming language.

Add me at Facebook my address is jakerpomperada@gmail.com and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.




Sample Program Output

Program Listing

fahrenheit.rs

// Fahrenheit To Celsius Program in Rust Programming language.
// Written By: Mr. Jake R. Pomperada, MAED-IT
// July 17, 2016

use std::io::{self, Write};
use std::fmt::Display;
use std::process;

 
fn main() {
       println!("\n");
       println!("\tFahrenheit To Celsius Program in Rust");
       println!("\n");
 
    let fahren: f32 = grab_input("Give temperature in Fahrenheit  ")
        .unwrap_or_else(|e| exit_err(&e, e.raw_os_error().unwrap_or(-1)))
        .trim()
        .parse()
        .unwrap_or_else(|e| exit_err(&e, 2));

     let  solve_celsius = (fahren-32.0)* 5.0/9.0;
                
    println!("\n"); 
    println!("The temperature in Fahrenheit is {:.2} its equivalent in Celsius is {:.2}.",fahren,solve_celsius);
    println!("\n"); 
    println!("End of Program");
}
 
fn grab_input(msg: &str) -> io::Result<String> {
    let mut buf = String::new();
    print!("{}: ", msg);
    try!(io::stdout().flush());
 
    try!(io::stdin().read_line(&mut buf));
    Ok(buf)
}
 
fn exit_err<T: Display>(msg: T, code: i32) -> ! {
    let _ = writeln!(&mut io::stderr(), "Error: {}", msg);
    process::exit(code)
}

No comments:

Post a Comment