3 releases
0.1.2 | Mar 11, 2025 |
---|---|
0.1.1 | Mar 11, 2025 |
0.1.0 | Mar 11, 2025 |
#27 in #stdio
370 downloads per month
Used in async-os-pipe
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