Function stdout

Source
pub fn stdout() -> StdoutUnblock
Expand description

Get the synchronised blocking::Unblock-ed stdout. A best-effort attempt is made to flush the asynchronous standard output upon program exit (including things stored in the pipe it uses to do async things).

The returned handle cannot be locked in a re-entrant fashion while the lock is held, within a future/task that is being waited for. If you try, it will cause a deadlock.

Examples found in repository?
examples/helloworld-noflush.rs (line 10)
8fn main() -> std::io::Result<()> {
9    futures_lite::future::block_on(async {
10        let mut stdout_handle = astdio::stdout().lock().await;
11        // Note how we do not need to bother flushing this - it's auto-handled by the crate
12        stdout_handle.write_all(b"Hello world!\n").await
13    })
14}
More examples
Hide additional examples
examples/helloworld-re-syncify.rs (line 9)
6fn main() -> std::io::Result<()> {
7    futures_lite::future::block_on(async {
8        use std::io::Write;
9        let mut stdout_sync_handle = astdio::stdout().lock_into_sync().await;
10        // Note how we do not need to bother flushing this - it's auto-handled by the crate, and is
11        // basically just println! here.
12        //
13        // However - in reality, this is not ideal because `BlockOn` uses
14        // futures_lite::future::block_on internally - it's better to use the async traits
15        // internally where possible if you're inside a future like this.
16        writeln!(stdout_sync_handle, "Hello world, from sync!")
17    })
18}