improved error handling in temperature

This commit is contained in:
Pecha 2019-10-14 10:37:17 +10:00
parent babbf76f6a
commit 41ae39c75e
5 changed files with 12 additions and 4 deletions

2
temperature/.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
/target
**/*.rs.bk

View file

@ -2,5 +2,5 @@
# It is not intended for manual editing. # It is not intended for manual editing.
[[package]] [[package]]
name = "temperature" name = "temperature"
version = "0.1.0" version = "0.2.0"

View file

@ -1,6 +1,6 @@
[package] [package]
name = "temperature" name = "temperature"
version = "0.1.0" version = "0.2.0"
authors = ["Pecha <petralawson@tuta.io>"] authors = ["Pecha <petralawson@tuta.io>"]
edition = "2018" edition = "2018"

View file

@ -5,8 +5,8 @@ fn main() {
loop { loop {
let mut scale = String::new(); let mut scale = String::new();
let mut temperature = String::new(); let mut temperature = String::new();
let mut o_scale = String::new();
let mut o_scale = String::new();
println!("Please enter a scale to convert from- Farenheit (F) or Celsius (C)"); println!("Please enter a scale to convert from- Farenheit (F) or Celsius (C)");
io::stdin().read_line(&mut scale) io::stdin().read_line(&mut scale)
.expect("ALERTA ALERTA ANTIFASCISTA"); .expect("ALERTA ALERTA ANTIFASCISTA");
@ -24,7 +24,13 @@ fn main() {
println!("Please enter a temperature"); println!("Please enter a temperature");
io::stdin().read_line(&mut temperature) io::stdin().read_line(&mut temperature)
.expect("ALERTA ALERTA ANTIFASCISTA"); .expect("ALERTA ALERTA ANTIFASCISTA");
let mut temperature: f32 = temperature.trim().parse().unwrap(); let mut temperature: f32 = match temperature.trim().parse() {
Ok(x) => x,
Err(_) => {
println!("Error: not recognised as a number, please try again.");
continue;
}
};
// [°C] = ([°F] - 32) x 5/9 // [°C] = ([°F] - 32) x 5/9
//[°F] = [°C] x 9/5 + 32 //[°F] = [°C] x 9/5 + 32

Binary file not shown.