Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Result<usize, std::io::error::Error> does not implement expect in Rust 1.0.0

Tags:

rust

In the "Guessing Game" tutorial example (here), my compiler is giving the following error, which I interpreted to mean that expect doesn't exist on read_line's Result.

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

The offending code:

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

I can remove the line(s) with .expect()and use the commented lines above and get it working. My concern is that it looks like the resulting type from io::stdin().read_line is core::result::Result and not the std::io::Result mentioned in the tutorial.

If I run that same code on the Rust playground it seems to run fine, so it's probably something in my environment, but I can't think what it could be.

Other possibly-relevant information:

  • I'm behind a corporate proxy that re-signs SSL certs, so the install script recommended by the rust-lang website didn't work
  • I installed using rust-1.0.0-x86_64-pc-windows-gnu.msi
  • I'm using the cargo included in the above "1.0.0" installer
  • I've manually added the path to rustc and cargo to my environment PATH
  • I'm using cygwin on 64-bit Windows 7

TL;DR

What do I need to change so that the expect() line compiles in my environment?

like image 553
Gus Avatar asked Nov 28 '25 01:11

Gus


2 Answers

Rust 1.0 was released on 2015-05-15, over one year prior. While Rust 1.x aims for backwards compatibility (code written on Rust 1.x should work on Rust 1.(x+1)), it does not aim for forwards compatibility (code written on Rust 1.x should work for Rust 1.(x-1)).

Reading the documentation for Rust 1.11 (the current release) is not going to be the most useful thing if you are limited to Rust 1.0.

Your best bet is to update to the newest version of Rust.

That being said, the documentation for Rust 1.0 is available online (I think you also have a copy of it installed locally). Checking out the guessing game, we see:

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

That is, we convert from a Result to an Option. Checking out the API for Result in 1.0.0, we can see that it indeed doesn't have an expect method.

If you check the current docs for Result::expect, you can see that it was introduced in Rust 1.4.

like image 165
Shepmaster Avatar answered Nov 29 '25 23:11

Shepmaster


The expect() method for Result<T, E> was introduced only in Rust 1.4.0 (source).

like image 40
ljedrz Avatar answered Nov 30 '25 00:11

ljedrz