Flush() the standard output on terminal


#1

Hello

Since 1.0.0 Beta i am very confused. :smile:
How i can flush() the standard output in the Terminal, for:

use std::thread;
use std::io::Write; // ? 

fn main(){
    let interval = 1000;
    for x in 17u8..180{
        thread::sleep_ms(interval);
        print!("\r{} = {}", x, x as char);
        // Write.flush();
    }
}

sorry for my bad english. :smile:

Greetings


#2

std::io::Write is just the name of the trait that flush() is declared in. You need to actually call it on something that implements that trait. For stdout, you can call std::io::stdout() to get something that wraps standard out and implements the Write trait.

use std::thread;
use std::io::Write;
use std::io::stdout;

fn main(){
    let interval = 1000;
    for x in 17u8..180{
        thread::sleep_ms(interval);
        print!("\r{} = {}", x, x as char);
        stdout().flush();
    }
}

#3

yay, a lot of thanks, it works fine.
and now i understand this. :smile: