-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
56 lines (47 loc) · 1.51 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
use std::{io, num::ParseIntError};
use chrono::prelude::*;
fn test(input: &Result<i32,ParseIntError>) -> bool {
match input {
Ok(ok) => if ok > &0 {
return true
} else {
println!("Age must be non-zero and positive.");
return false;
},
Err(..) => {
println!("Input is not a number.");
return false;
}
}
}
fn calc_retire(age: i32, retire: i32) {
let years_left = retire - age;
if &years_left > &0 {
println!("You have {} years left until you can retire.", &years_left);
let dt = Utc::now();
println!("It's {}, so you can retire in {}.", dt.year(), dt.year() + &years_left);
} else {
println!("You can already retire!");
}
}
fn main() {
let age = loop {
println!("What is your current age?");
let mut age = String::new();
io::stdin()
.read_line(&mut age)
.expect("Failed to read line.");
let age_trimmed = age.trim().parse::<i32>();
if test(&age_trimmed) { break age_trimmed.unwrap() };
};
let retire = loop {
println!("At what age would you like to retire?");
let mut retire = String::new();
io::stdin()
.read_line(&mut retire)
.expect("Failed to read line.");
let retire_trimmed = retire.trim().parse::<i32>();
if test(&retire_trimmed) { break retire_trimmed.unwrap() };
};
calc_retire(age, retire);
}