stdio-utils

Crates.iostdio-utils
lib.rsstdio-utils
version0.1.2
created_at2025-03-11 17:27:23.674296+00
updated_at2025-03-11 21:57:56.134869+00
descriptionUtilities for working with the process standard input and output
homepagehttps://github.com/jprendes/stdio-utils
repositoryhttps://github.com/jprendes/stdio-utils
max_upload_size
id1588234
size31,207
Jorge Prendes (jprendes)

documentation

README

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"
Commit count: 9

cargo fmt