From ba3d3a591ecf09629de0d8367a11089384bbb95e Mon Sep 17 00:00:00 2001 From: laurens Date: Sat, 2 May 2020 19:13:46 +0200 Subject: [PATCH] Add temperature convertor --- README.md | 2 ++ temperature_convertor/Cargo.toml | 9 +++++++++ temperature_convertor/src/main.rs | 23 +++++++++++++++++++++++ 3 files changed, 34 insertions(+) create mode 100644 temperature_convertor/Cargo.toml create mode 100644 temperature_convertor/src/main.rs diff --git a/README.md b/README.md index 519f68b..3546d9d 100644 --- a/README.md +++ b/README.md @@ -43,3 +43,5 @@ Interesting things to know -------------------------- * A char is 4 bytes in Rust! + +* Breaking with a value is only possible inside a 'loop' not a 'while/for' loop! diff --git a/temperature_convertor/Cargo.toml b/temperature_convertor/Cargo.toml new file mode 100644 index 0000000..cb3e6ff --- /dev/null +++ b/temperature_convertor/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "temparature_convertor" +version = "0.1.0" +authors = ["laurens "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/temperature_convertor/src/main.rs b/temperature_convertor/src/main.rs new file mode 100644 index 0000000..24e5841 --- /dev/null +++ b/temperature_convertor/src/main.rs @@ -0,0 +1,23 @@ +use std::io; +use std::process::exit; + +fn main() { + let mut fahr = String::new(); + + println!("Input fahrenheit number:"); + io::stdin().read_line(&mut fahr).expect("Failed to read user input"); + + // let fahr: f64 = fahr.trim().parse().expect("Not a number!"); + let fahr: f64 = match fahr.trim().parse() { + Ok(num) => num, + Err(_) => { + println!("Can't work with that!"); + exit(1); + } + }; + + + let celsius = (fahr - 32.0) / 1.8; + + println!("{} Fahrenheit is {} in Degrees Celsius", fahr, celsius); +}