Friday, July 29, 2016

Year Level Checker in Rust

A simple program that I wrote using Rust programming language that will ask the user what is its year level and then our program will determine whether the user is freshmen, sophomore, junior or senior. The code is very simple and easy to understand.

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


// Year Level Checker in Rust Programming language.
// Written By: Mr. Jake R. Pomperada, MAED-IT
// July 28, 2016

use std::io::{self, Write};
use std::fmt::Display;
use std::process;
 
fn main() {
       println!("\n");
       println!("\tYear Level Checker in Rust");
       println!("\n");
 
    let year: i32 = grab_input("Enter the Year Level of the Student  ")
        .unwrap_or_else(|e| exit_err(&e, e.raw_os_error().unwrap_or(-1)))
        .trim()
        .parse()
        .unwrap_or_else(|e| exit_err(&e, 2));

   if year == 1 {
    println!("\n"); 
    println!("You are belong to Freshmen.");
  }
  else if year == 2 {
    println!("\n"); 
    println!("You are belong to Sophomore.");
else if year == 3 {
    println!("\n"); 
    println!("You are belong to Juniors.");
else if year == 4 {
    println!("\n"); 
    println!("You are belong to Seniors.");
else {
    println!("\n");
    println!("Sorry Invalid Year Level !!! Try again.");
   }
 
    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