Why do I get a warning about std::result::Result not being used?

So here is my code:

fn main()
{
    let reader = std::io::stdin();
    let mut line = String::new();

    reader.read_line(&mut line); // Getting warning over here

    print!("Line: {}", line);
}

So why do I get a warning about this line? If I ommit it, then it doesn't take input from the user so why does it suggest to get rid of it, is this some bug in the compiler?

warning: unused `std::result::Result` that must be used
 --> src\main.rs:6:5
  |
6 |     reader.read_line(&mut line);
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_must_use)]` on by default
  = note: this `Result` may be an `Err` variant, which should be handled

warning: 1 warning emitted

    Finished dev [unoptimized + debuginfo] target(s) in 0.45s
     Running `target\debug\learning.exe`
hello
Line: hello

read_line() might fail for various reasons, like the input being closed. It returns a Result<_,_> to tell you whether or not it was successful. The compiler warning tells you that you’re not handling the failure case. There are several ways to handle this, but the most straigntforward is to halt the program with an error message if read_line fails:

reader.read_line(&mut line).expect(“Couldn’t read from stdin”);
1 Like

Ah I see thanks :slight_smile:

The answer to that question is almost always "no". Especially if you are a beginner with the language, that is massively unlikely, and IMHO it's quite an arrogant (how to put this more nicely?) assumption to make that the compiler, that has been around for more than a decade and survived the scrutiny of hundreds of developers, has a bug in such a trivial case.

1 Like

While this is true, it’s also a perfectly natural doubt to have; we shouldn’t be berating people for it. Striving to understand the cause of unexpected behavior is, after all, the path to expertise.

The Rust compiler has plenty of suboptimal behaviors, some by design and others by chance. If there’s any arrogance on display here, it’s your assumption that “beginners” are incapable of identifying them.

3 Likes

No, it's just Bayesianism. As a beginner, I wouldn't start immediately accusing a technology of being buggy while I don't understand it thoroughly.

1 Like

That’s because you’ve become an expert at using and learning new technologies. It can take a few rounds of learning new tech before that particular lesson sets in; lots of people here are beginners at both Rust and learning at the same time.

I’d also like to point out that there was neither an assumption nor an accusation of the compiler being buggy. @Joe232 asked a question that he had, and you interpreted it in that light.

3 Likes

As @2e71828 pointed out, I wasn't accusing the compiler being buggy. I was just a bit confused as to why I got that warning and simply asking if I made a mistake or if there is something wrong with the compiler. If I was 100% certain that there was a bug in the compiler, why wouldn't I just open up an issue over at github on the Rust repository and report the bug there?

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.