axyd icon

Rust Grade Calculator

axyd | PRO | 09/15/17 07:37:07 PM UTC | 0 ⭐ | 718 👁️ | Never ⏰ | []
Rust |

630 B

|

None

|

0 👍

/

0 👎

rust 1.17.0
use std::io;
 
fn main() {
    
    println!("\n\tEnter your score: ");
    
    //read user input
    let mut input= String::new(); io::stdin()   
        .read_line(&mut input) 
        .expect("failed to read input."); 
    //convert input to integer
    let score: i32 = input.trim().parse().expect("invalid input");
    
    let s= calc(score);
    
    print!("\n\tYour grade: {}",s);
    
    return ;
}
 
fn calc(sco: i32) -> &'static str{
    let mut gra= "F";
    
    match sco {     
        90...100 => { gra= "A"; }   
        80...89 => { gra= "B"; }
        70...79 => { gra= "C"; }
        60...69 => { gra= "D"; }
        _ => { println!("\n\tERROR: Invalid score\n"); }    
    }
    gra
}

Comments