Search code examples
linuxiorust

Does Rust have bindings for tee(2)?


Does Rust have bindings for tee(2) in std::io or otherwise? And if there are no bindings, how would I get that functionality in a Rust program?


Solution

  • The tee method existed in the standard library, but it was deprecated in 1.6.

    You can use the tee crate to get the same functionality:

    extern crate tee;
    
    use tee::TeeReader;
    use std::io::Read;
    
    fn main() {
        let mut reader = "It's over 9000!".as_bytes();
        let mut teeout = Vec::new();
        let mut stdout = Vec::new();
        {
            let mut tee = TeeReader::new(&mut reader, &mut teeout);
            let _ = tee.read_to_end(&mut stdout);
        }
        println!("tee out -> {:?}", teeout);
        println!("std out -> {:?}", stdout);
    }
    

    (example from the repo)