rust - Result<usize, std::io::error::Error> 在 Rust 1.0.0 中没有实现 expect

标签 rust

在“猜谜游戏”教程示例 ( here ) 中,我的编译器给出了以下错误,我将其解释为 expectread_line 上不存在的 Result .

error: type `core::result::Result<usize, std::io::error::Error>` does not implement any method in scope named `expect`

违规代码:

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());
}

我可以用 .expect() 删除行并使用上面的注释行并使其正常工作。我担心的是它看起来像 io::stdin().read_line 的结果类型是core::result::Result而不是 std::io::Result教程中提到。

如果我在 Rust Playground 上运行相同的代码,它似乎运行良好,所以它可能是我环境中的某些东西,但我想不出它可能是什么。

其他可能相关的信息:

  • 我在重新签署 SSL 证书的公司代理后面,所以 rust-lang 网站推荐的安装脚本不起作用
  • 我使用 rust-1.0.0-x86_64-pc-windows-gnu.msi 安装
  • 我正在使用上述“1.0.0”安装程序中包含的 cargo
  • 我已将 rustc 和 cargo 的路径手动添加到我的环境中 PATH
  • 我在 64 位 Windows 7 上使用 cygwin

TL;DR

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

最佳答案

Rust 1.0 于 2015-05-15 发布,比之前一年多。虽然 Rust 1.x 旨在向后兼容(在 Rust 1.x 上编写的代码应该在 Rust 1.(x+1) 上运行),但它旨在 < em>向前兼容性(在 Rust 1.x 上编写的代码应该适用于 Rust 1.(x-1))。

如果您仅限于 Rust 1.0,阅读 Rust 1.11(当前版本)的文档并不是最有用的。

最好的选择是update to the newest version of Rust .

也就是说,documentation for Rust 1.0 is available online (我认为您在本地也安装了它的副本)。 checkout the guessing game ,我们看到:

io::stdin()
    .read_line(&mut guess)
    .ok()
    .expect("Failed to read line");

也就是说,我们将 Result 转换为 Option。查看 Result in 1.0.0 的 API ,我们可以看到它确实没有 expect 方法。

如果您检查 current docs for Result::expect ,你可以看到它是在 Rust 1.4 中引入的。

关于rust - Result<usize, std::io::error::Error> 在 Rust 1.0.0 中没有实现 expect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39648102/

相关文章:

rust - 为什么我需要 mod 关键字来访问文件中同一级别的 Rust 结构?

iterator - 如何跳过 Rust 中迭代器的前 n 项?

rust - 是否需要在使用时导入特征?

rust - Cargo 构建结果为 "No match for id"

Rust 编译错误 - 无法推断类型参数的类型

multidimensional-array - 是否有一个 Rust ndarray 等价于切片上的 numpy 算术?

c# - 如何将结构向量从 Rust 返回到 C#?

rust - 宏扩展忽略 token `let` 和任何后续

rust - 在请求守卫中访问 Rocket 0.4 数据库连接池

oauth-2.0 - 如何在 Rust 中使用 google_speech1 发送语音到文本请求?