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:
rust-1.0.0-x86_64-pc-windows-gnu.msiPATHTL;DR
What do I need to change so that the expect() line compiles in my environment?
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.
The expect() method for Result<T, E> was introduced only in Rust 1.4.0 (source).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With