All the documentation I've found regarding flushing suggests that the proper way to flush stdout is as follows:
std::io::stdout().flush().expect("some error message");
This results in
no method named flush found for type std::io::Stdout in the current scope
What am I doing wrong?
You need to import the trait that implements the flush
method for Stdout
.
According to the documentation:
Therefore:
use std::io::Write; // <--- bring the trait into scope
fn main() {
std::io::stdout().flush().expect("some error message");
}
Playground example
Can anyone tell me what I'm doing wrong?
Yes; the compiler already does.
fn main() {
std::io::stdout().flush().expect("some error message");
}
error[E0599]: no method named `flush` found for type `std::io::Stdout` in the current scope
--> src/main.rs:3:23
|
3 | std::io::stdout().flush().expect("some error message");
| ^^^^^
|
= help: items from traits can only be used if the trait is in scope
= note: the following trait is implemented but not in scope, perhaps add a `use` for it:
candidate #1: `use std::io::Write;`
Emphasis on the help
and note
lines - use std::io::Write
.
All together:
use std::io::Write;
fn main() {
std::io::stdout().flush().expect("some error message");
}
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