-
Notifications
You must be signed in to change notification settings - Fork 1
/
t3.rs
59 lines (53 loc) · 1.64 KB
/
t3.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
57
58
59
// warning: constant `...` should have an upper case name such as ...
const XRATE_US2_EUR: f64 = 0.5;
const XRATE_US2_RMB: f64 = 7.0;
fn main() {
println!("Please input the amount of US dollars");
let mut buf = String::new();
std::io::stdin().read_line(&mut buf).unwrap();
let usd = match buf.split_whitespace().next() {
Some(r) => {
match r.parse::<f64>() {
Ok(r) => r,
Err(e) => {
println!("{:?}: Illegal number of US dollars.", e);
std::process::exit(255);
}
}
},
None => {
// Nothing from stdin
println!("No input... Exiting.");
std::process::exit(255);
}
};
println!("Convert to Euro(E) or RMB(R)?");
// Clear buffer (as buffered stdio)
buf.clear();
std::io::stdin().read_line(&mut buf).unwrap();
let selection = match buf.chars().next() {
Some(r) => r,
None => {
// This happends when you input ^D(NUL) (since \n is also a character)
println!("No input... Exiting.");
std::process::exit(255);
}
};
let result = match selection {
'E' | 'e' => usd * XRATE_US2_EUR,
'R' | 'r' => usd * XRATE_US2_RMB,
_ => {
// Not EUR nor RMB
println!("Wrong type of conversion, exiting.");
std::process::exit(1);
}
};
println!(
"{} US dollars converts to {} {}",
usd, result, match selection {
'E' | 'e' => "euro",
'R' | 'r' => "yuan",
_ => "that much"
}
);
}