Advertisement
axyd

Rust Grade Calculator

Sep 15th, 2017
400
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 0.63 KB | None | 0 0
  1. rust 1.17.0
  2. use std::io;
  3.  
  4. fn main() {
  5.    
  6.     println!("\n\tEnter your score: ");
  7.    
  8.     //read user input
  9.     let mut input= String::new(); io::stdin()  
  10.         .read_line(&mut input)
  11.         .expect("failed to read input.");
  12.     //convert input to integer
  13.     let score: i32 = input.trim().parse().expect("invalid input");
  14.    
  15.     let s= calc(score);
  16.    
  17.     print!("\n\tYour grade: {}",s);
  18.    
  19.     return ;
  20. }
  21.  
  22. fn calc(sco: i32) -> &'static str{
  23.     let mut gra= "F";
  24.    
  25.     match sco {    
  26.         90...100 => { gra= "A"; }  
  27.         80...89 => { gra= "B"; }
  28.         70...79 => { gra= "C"; }
  29.         60...69 => { gra= "D"; }
  30.         _ => { println!("\n\tERROR: Invalid score\n"); }   
  31.     }
  32.     gra
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement