#stdout #stdio #stdin #cross-platform #standard #redirect #process

stdio-utils

Utilities for working with the process standard input and output

3 releases

0.1.2 Mar 11, 2025
0.1.1 Mar 11, 2025
0.1.0 Mar 11, 2025

#27 in #stdio

Download history 153/week @ 2025-03-05 217/week @ 2025-03-12

370 downloads per month
Used in async-os-pipe

Apache-2.0

10KB
153 lines

stdio-utils

A set of cross-platform utilities for handling the standard input output in Rust.

use std::fs::{read_to_string, File};
use std::io::Result;

use stdio_utils::{null, StdioOverride as _};

fn main() -> Result<()> {
    println!("Now you see me");

    // redirect stdout to /dev/null
    {
        let _guard = null()?.override_stdout()?;
        println!("Now you don't");
    }

    // stdout to the console is restored
    println!("Now you see me again");

    // redirect stdout to ./output.txt
    {
        let _guard = File::create("./output.txt")?.override_stdout()?;
        println!("Now you see me if you search");
    }

    // stdout to the console is restored
    let msg = read_to_string("./output.txt")?;
    println!("{msg:?}");

    Ok(())
}

This should print

Now you see me
Now you see me again
"Now you see me if you search\n"

Dependencies

~0–7.5MB
~53K SLoC