Migration from GitHub
This commit is contained in:
5
chapter_03/tempconverter/Cargo.lock
generated
Normal file
5
chapter_03/tempconverter/Cargo.lock
generated
Normal file
@@ -0,0 +1,5 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
[[package]]
|
||||
name = "tempconverter"
|
||||
version = "0.1.0"
|
||||
9
chapter_03/tempconverter/Cargo.toml
Normal file
9
chapter_03/tempconverter/Cargo.toml
Normal file
@@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "tempconverter"
|
||||
version = "0.1.0"
|
||||
authors = ["rarmknecht <rarmknecht@gmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
61
chapter_03/tempconverter/src/main.rs
Normal file
61
chapter_03/tempconverter/src/main.rs
Normal file
@@ -0,0 +1,61 @@
|
||||
use std::io;
|
||||
|
||||
fn main() {
|
||||
|
||||
println!("Welcome to Temp Converter!");
|
||||
|
||||
// Loop the Main Menu
|
||||
loop {
|
||||
println!("Chose:\n\t[1] F to C\n\t[2] C to F\n\t[3] Quit");
|
||||
|
||||
let mut choice = String::new();
|
||||
io::stdin()
|
||||
.read_line(&mut choice)
|
||||
.expect("Failed to read line");
|
||||
let choice = choice.trim();
|
||||
|
||||
match choice {
|
||||
"1" => {
|
||||
println!("F to C it is!");
|
||||
let c = f_to_c(get_temp());
|
||||
println!("That's {} in Celsius", c);
|
||||
}
|
||||
"2" => {
|
||||
println!("C to F it is!");
|
||||
let f = c_to_f(get_temp());
|
||||
println!("That's {} in Fahrenheit", f);
|
||||
}
|
||||
"3" => {
|
||||
println!("Good Bye!");
|
||||
break;
|
||||
}
|
||||
_ => continue,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn f_to_c(f: f64) -> f64 {
|
||||
(f - 32.0) * (5.0/9.0)
|
||||
}
|
||||
|
||||
fn c_to_f(c: f64) -> f64 {
|
||||
(c * (9.0/5.0)) + 32.0
|
||||
}
|
||||
|
||||
fn get_temp() -> f64 {
|
||||
loop {
|
||||
println!("Enter your starting temp:");
|
||||
let mut temp = String::new();
|
||||
io::stdin()
|
||||
.read_line(&mut temp)
|
||||
.expect("Failed to read line");
|
||||
let temp = temp.trim();
|
||||
|
||||
let f : f64 = match temp.parse() {
|
||||
Ok(num) => num,
|
||||
Err(_) => continue,
|
||||
};
|
||||
|
||||
return f
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user