在"划拳"教程例子(在这里),我的编译器是给下面的错误,我解释为,expect不上不存在read_line的Result.
error: type `core::result::Result<usize, std::io::error::Error>` does not implement any method in scope named `expect`
Run Code Online (Sandbox Code Playgroud)
违规代码:
use std::io;
fn main() {
println!("**********************");
println!("***Guess the number***");
println!("**********************");
let mut guess = String::new();
io::stdin()
.read_line(&mut guess)
.expect("Failed to read line"); //<<-- error on this line
//let guess_result=io::stdin().read_line(&mut guess);
println!("You guessed: {}", guess);
// println!("Result: {}", guess_result.is_ok());
}
Run Code Online (Sandbox Code Playgroud)
我可以删除行,.expect()并使用上面的注释行并使其工作.我担心的是,它看起来像结果类型由io::stdin().read_line是core::result::Result而不是std::io::Result在本教程中提到.
如果我在Rust操场上运行相同的代码,它似乎运行正常,所以它可能在我的环境中,但我不能想到它可能是什么.
其他可能相关的信息:
rust-1.0.0-x86_64-pc-windows-gnu.msiPATHTL; DR
我需要更改什么才能expect()在我的环境中编译该行?
Rust 1.0于2015-05-15发布,超过一年.虽然锈1.x的目的是为向后兼容性(写上锈病的1.x代码应在锈病1.工作(X + 1)),但它不旨在用于向前兼容性(写上锈病的1.x代码应为锈1工作(X-1)).
如果您仅限于Rust 1.0,那么阅读Rust 1.11(当前版本)的文档并不是最有用的.
您最好的选择是更新到最新版本的Rust.
话虽这么说,Rust 1.0的文档可以在线获得(我想你也有本地安装的副本).检查猜谜游戏,我们看到:
io::stdin()
.read_line(&mut guess)
.ok()
.expect("Failed to read line");
Run Code Online (Sandbox Code Playgroud)
也就是说,我们从一个转换Result到Option.检查Result1.0.0中的API ,我们可以看到它确实没有expect方法.
如果您查看当前的文档Result::expect,可以看到它是在Rust 1.4中引入的.