Description
Location
https://doc.rust-lang.org/std/io/struct.Stdout.html#method.lock
https://doc.rust-lang.org/std/io/struct.Stderr.html#method.lock
https://doc.rust-lang.org/std/io/struct.Stdin.html#method.lock
Summary
Currently code like this is fine and a convenient way to lock stdout to prevent other threads from interrupting while printing is happening (this is especially useful when the printing happens deep inside some library/component, where StdoutLock
can't be easily passed as an argument):
let stdout = io::stdout().lock();
println!("a");
println!("b");
println!("c");
drop(stdout);
As far as I understand, this works because Stdout::lock()
is reentrant. However, the documentation does not mention that Stdout::lock()
is reentrant.
Is Stdout::lock()
guaranteed to be reentrant in all future Rust versions, or is this an implementation detail that should not be relied on?
Either way it would be nice if the documentation of Stdout::lock()
made it clear what the expected behavior is and how much of it is an implementation detail.
The same question also applies to Stderr::lock()
and Stdin::lock()
.
Activity