Skip to content

Instantly share code, notes, and snippets.

@zzeroo
Created March 17, 2017 10:51
Show Gist options
  • Save zzeroo/95b438c321bb930d3bbf13c59c64dfed to your computer and use it in GitHub Desktop.
Save zzeroo/95b438c321bb930d3bbf13c59c64dfed to your computer and use it in GitHub Desktop.
Why does print!() not work in a loop?
fn main() {
loop {
print!(".");
::std::thread::sleep(::std::time::Duration::from_millis(1000));
}
}
@zzeroo
Copy link
Author

zzeroo commented Mar 17, 2017

Solution from @sebk from irc.mozilla.org#rust

11:51 Why does print!() not work in a loop? https://gist.github.com/zzeroo/95b438c321bb930d3bbf13c59c64dfed
11:52 zzeroo: stdout is probably linebufferd
11:52 so unless you send \n nothing will actually be send out
11:53 Ah without thread::sleep it works... danm lazyness ><
11:53 add std::io::stout().flush() to the loop and it should work
11:57 https://is.gd/mE7eRk

use std::io::Write;

fn main() {
    loop {
        print!(".");
        std::io::stdout().flush();
        std::thread::sleep(std::time::Duration::from_millis(1000));
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment