结果<usize,std :: io :: error :: Error>未在Rust 1.0.0中实现expect

Gus*_*Gus 1 rust

在"划拳"教程例子(在这里),我的编译器是给下面的错误,我解释为,expect不上不存在read_lineResult.

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_linecore::result::Result而不是std::io::Result在本教程中提到.

如果我在Rust操场上运行相同的代码,它似乎运行正常,所以它可能在我的环境中,但我不能想到它可能是什么.

其他可能相关的信息:

  • 我是一个重新签署SSL证书的公司代理,因此rust-lang网站推荐的安装脚本不起作用
  • 我安装使用 rust-1.0.0-x86_64-pc-windows-gnu.msi
  • 我正在使用上述"1.0.0"安装程序中包含的货物
  • 我手动将生锈和货物的路径添加到我的环境中 PATH
  • 我在64位Windows 7上使用cygwin

TL; DR

我需要更改什么才能expect()在我的环境中编译该行?

She*_*ter 7

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)

也就是说,我们从一个转换ResultOption.检查Result1.0.0中的API ,我们可以看到它确实没有expect方法.

如果您查看当前的文档Result::expect,可以看到它是在Rust 1.4中引入的.

  • 啊啊.我现在正在推出最新版本; 谢谢!现在我想知道我在哪里获得了1.0.0版本的链接; 当我回溯我的步骤时,我找不到它. (2认同)