Sunday, July 17, 2016

Area of the Circle Solver in Rust

A simple program that will ask the user to give the radius of the circle and then our program will solve for the area of the circle using Rust as our 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

circle.rs

// Area of the Circle 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!("\tArea of the Circle Program in Rust");
       println!("\n");

       let pi : f32 = 3.1416; 

    let radius: f32 = grab_input("Enter the radius of the circle ")
        .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_area = pi * radius * radius;
                  
    println!("\n"); 
    println!("The given radius is {:.2} the area of the circle is {:.2}.",radius,solve_area);
    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