Print in same line

Print in same line

Reading Time: ~ 1 minute

“Normal” Print

If you want to print with a line break at the end, it is pretty easy:

println!("Here is your message!");
println!("With {} placeholders too!", 1);

Which would output:

Here is your message!
With 1 placeholders too!

 

Print in the same line

This needs some more increments.

  1. We need to import some things
  2. If we want to print it on the screen right away we need to flush the buffer which might be waiting to print.
use std::io::{stdout, Write};

print!("This should be ");
stdout().flush().unwrap();

print!("in one line!");
stdout().flush().unwrap();
This should be in one line!

Of course, you can avoid the stdout flush repetition if you want. But for the purpose of demonstration I’ve repeated

Leave your opinion, start a discussion, share an experience or just a compliment...

%d bloggers like this: