Cannot seem to successfully compile

i'm trying this code below after reading some online reference on how to get user input. but it doesn't compile.

use std::io;

fn main() {
    let mut user_input = String::new();
    io::stdin().read_line(&mut user_input);

    println!("input {} ", user_input);
}

i get this error

warning: unused `Result` that must be used
 --> test.rs:5:5
  |
5 |     io::stdin().read_line(&mut user_input);
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_must_use)]` on by default
  = note: this `Result` may be an `Err` variant, which should be handled

warning: 1 warning emitted

This isn't an error; it's a warning. The full compiler output is something along the lines of

   Compiling playground v0.0.1 (/playground)
warning: unused `Result` that must be used
 --> src/main.rs:5:5
  |
5 |     io::stdin().read_line(&mut user_input);
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_must_use)]` on by default
  = note: this `Result` may be an `Err` variant, which should be handled

warning: `playground` (bin "playground") generated 1 warning
    Finished dev [unoptimized + debuginfo] target(s) in 0.62s
with color

image

An error looks like

   Compiling playground v0.0.1 (/playground)
error: unused `Result` that must be used
 --> src/main.rs:7:5
  |
7 |     io::stdin().read_line(&mut user_input);
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: requested on the command line with `-D unused-must-use`
  = note: this `Result` may be an `Err` variant, which should be handled

error: could not compile `playground` due to previous error
with color

image

Note that the former says warning, whereas the latter says error. If you're using a color-capable terminal, the former will generally print using a yellow color, and the latter with a red color. Warnings don't prevent the code from compiling; if you cargo run, you'll run the compiled program.

The reason for the warning is that reading from stdin can fail for multiple reasons; for example, the program could not have a standard input because it wasn't run from a terminal. The easiest way to silence the warning is to use ? to hand up the error to the calling context; returning an error from main will result in displaying it to the terminal.

fn main() -> io::Result<()> {
    let mut user_input = String::new();
    io::stdin().read_line(&mut user_input)?;

    println!("input {} ", user_input);
    Ok(())
}
2 Likes

if it's a warning, it didn't asked for user input as i intended. and yes it was run from CLI (an Ubuntu WSL2 box). even when ran from the playground, it doesn't ask for user input. straight away gives that warning.

tried the code in VirtualBox (Debian 11) and it produces same warning. also, didn't asked for user input.

The playground doesn't really do user input. Running it on the playground indicates that, although it gives the warning, it does print "input", which indicates that the program is indeed running (but the playground just provides blank input).

Are you actually running the program on your machine or just compiling? Does it let you type something before outputting when it runs on your machine?

1 Like

There's no prompt for input involved here (unlike, say, python); it will just wait for input to be available. If you want to show an input prompt, you have to print it yourself, e.g.

let mut input = String::new();
print!("input? ");
io::stdout().flush()?;
io::stdin().read_line(&mut input)?;
println!("  got: {input}");

There's essentially two steps to running a Rust program. cargo build takes your code and compiles it into an executable format that your OS actually understands. It's at this step that errors and warnings are printed. cargo run first does a cargo build to create an up-to-date executable (thus showing warnings) and then actually runs the program.

If you want to run your program without showing warnings or other compilation messages, you can use cargo run -q to request that cargo be --quiet.

3 Likes

my bad. got confused between "rustc" and "cargo run". my code as is works just have to rearrange the layout.

use std::io;
use std::io::Write;

fn main() {
    let mut user_input = String::new();

    print!("input: ");
    io::stdout().flush();
    io::stdin().read_line(&mut user_input);

    println!("i received {user_input}");
}

using "cargo run" i still get the warning but it asks for input now:

warning: unused `Result` that must be used
 --> src/main.rs:8:2
  |
8 |     io::stdout().flush();
  |     ^^^^^^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_must_use)]` on by default
  = note: this `Result` may be an `Err` variant, which should be handled

warning: unused `Result` that must be used
 --> src/main.rs:9:5
  |
9 |     io::stdin().read_line(&mut user_input);
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: this `Result` may be an `Err` variant, which should be handled

warning: `tests` (bin "tests") generated 2 warnings
    Finished dev [unoptimized + debuginfo] target(s) in 0.48s
     Running `/home/rino/tests/target/debug/tests`
input: apple
i received apple