Crates.io | stdio-utils |
lib.rs | stdio-utils |
version | 0.1.2 |
created_at | 2025-03-11 17:27:23.674296+00 |
updated_at | 2025-03-11 21:57:56.134869+00 |
description | Utilities for working with the process standard input and output |
homepage | https://github.com/jprendes/stdio-utils |
repository | https://github.com/jprendes/stdio-utils |
max_upload_size | |
id | 1588234 |
size | 31,207 |
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"