macro_rules! print { ($($arg:tt)*) => { ... }; }
Expand description
Prints to stdout
.
Equivalent to the println!
macro except that a newline is not printed at
the end of the message.
Note that stdout is frequently line-buffered by default so it may be
necessary to use std::io::Write::flush()
to ensure the output is emitted
immediately.
NOTE: The print!
macro will lock the standard output on each call. If you call
print!
within a hot loop, this behavior may be the bottleneck of the loop.
To avoid this, lock stdout with AutoStream::lock
:
use std::io::Write as _;
let mut lock = anstream::stdout().lock();
write!(lock, "hello world").unwrap();
Use print!
only for the primary output of your program. Use
eprint!
instead to print error and progress messages.
NOTE: Not all print!
calls will be captured in tests like std::print!
- Capturing will automatically be activated in test binaries
- Otherwise, only when the
test
feature is enabled
§Panics
Panics if writing to stdout
fails for any reason except broken pipe.
Writing to non-blocking stdout can cause an error, which will lead this macro to panic.
§Examples
use std::io::Write as _;
use anstream::print;
use anstream::stdout;
print!("this ");
print!("will ");
print!("be ");
print!("on ");
print!("the ");
print!("same ");
print!("line ");
stdout().flush().unwrap();
print!("this string has a newline, why not choose println! instead?\n");
stdout().flush().unwrap();