我正在制作一个简单的控制台程序,用于在摄氏度和华氏度之间进行转换。该程序需要多次获取用户输入,一次是转换类型,另一次是要转换的值。当我运行该程序时,它可以编译和运行而不出现任何错误,但实际上它并没有返回任何值。
以下是程序代码:
use std::io;
// C to F: F = C*(9/5) + 32
// F to C: C = (F-32)*(5/9)
/**********Converts between Fahrenheit and Celsius*********/
fn main() -> () {
println!("Do you want to convert to Celsius or Fahrenheit? Input C or F");
let mut convert_type = String::new();
io::stdin()
.read_line(&mut convert_type)
.expect("Failed to conversion type.");
let t = String::from(convert_type);
println!("You want to convert to: {}", t);
println!("What temperature would you like to convert?");
let mut temp = String::new();
io::stdin()
.read_line(&mut temp)
.expect("Failed to read temperature.");
let temp: i32 = match temp.trim().parse() {
Ok(temp) => temp,
Err(_e) => -1,
};
if &t == "C" {
println!("{}", ctof(temp));
} else if &t == "F" {
println!("{}", ftoc(temp));
}
}
// Celsius to Fahrenheit
fn ctof(c: i32) -> i32 {
(c * (9 / 5)) + 32
}
//Fahrenheit to Celsius
fn ftoc(f: i32) -> i32 {
(f - 32) * (5 / 9)
}
这是控制台的一部分代码,你可以看到它没有输出转换结果:
cargo run --verbose
Compiling ftoc v0.1.0 (/Users/roberthayek/rustprojects/ftoc)
Running `rustc --crate-name ftoc src/main.rs --color always --crate-type bin --emit=dep-info,link -C debuginfo=2 -C metadata=8f02d379c2e5c97d -C extra-filename=-8f02d379c2e5c97d --out-dir /Users/roberthayek/rustprojects/ftoc/target/debug/deps -C incremental=/Users/roberthayek/rustprojects/ftoc/target/debug/incremental -L dependency=/Users/roberthayek/rustprojects/ftoc/target/debug/deps`
Finished dev [unoptimized + debuginfo] target(s) in 1.16s
Running `target/debug/ftoc`
Do you want to convert to Celsius or Fahrenheit? Input C or F
C
You want to convert to: C
What temperature would you like to convert?
0
printl!("{?}", t);
? - behnam