(Rust) Silence stderr and stdout, optionally rerouting it.
Stdout Gagging
println!;
println!;
let shh = stdout.unwrap;
println!;
drop;
println!;
Stderr Gagging
println!;
eprintln!;
let shh = stderr.unwrap;
eprintln!;
drop;
eprintln!;
Redirecting Example
println!;
use ;
spawn;
println!;
eprintln!;
// This will exit and close the spawned thread.
// In most cases you will want to setup a channel and send a break signal to the loop,
// and then join the thread back into it once you are finished.
Scoping
The struct Shh
implements the Drop
trait. Upon going out of scope, the redirection is reset and resources are cleaned up. A Shh
will only last for the scope, and where no local variable is used, the silencing will not work.
Example - Silencing Dropped Early
println!;
stdout.unwrap; // Shh struct is created, and dropped, here
println!;
To fix this, just assign a local variable
println!;
let shh = stdout.unwrap; // Shh struct is created here
println!;
drop; // and dropped here
println!;