我是一個新手鏽,並試圖在兩個數字閱讀和計算它們的商:錯誤讀取及計算商數
use std::io;
enum Option<T> {
None,
Some(T),
}
fn safe_div(n: i32, d: i32) -> Option<i32> {
if d == 0 {
return None;
}
return Some(n/d);
}
fn main() {
println!("Please input your numerator.");
let mut numerator = String::new();
io::stdin()
.read_line(&mut numerator)
.expect("Failed to read line");
println!("Please input your denominator.");
let mut denominator = String::new();
io::stdin()
.read_line(&mut denominator)
.expect("Failed to read line");
match safe_div(numerator, denominator) {
None => println!("Can't divide by zero!"),
Some(v) => println!("Quotient is {}", v),
}
}
,但我收到以下錯誤反覆幾次,當我嘗試編譯:
src/safe_div.rs:12:12: 12:21 error: mismatched types: expected
Option<i32>
, foundstd::option::Option<i32>
(expected enumOption
, found enumstd::option::Option
) [E0308]
我在這裏錯過了什麼?
我試過這個修復和編譯錯誤消失,但它給我的運行時錯誤,而不是:'線程''恐慌在'預期i32''和死而寧比打印商。 –
wogsland