rustio-redirection

How to redirect child process output to stderr?


I am trying to start a process with the Command API and redirect its standard output to standard error. The following fails:

Command::new("tput")
    .arg("rc")
    .stdout(io::stderr())
    .status()
    .expect("failed to run tput");

because Command::new("tput").arg("rc").stdout(<XXX>) expects a std::process::Stdio:

expected struct `std::process::Stdio`, found struct `std::io::Stderr`

The equivalent in Bash would probably be tput rc > /dev/stderr.

I would like to know how to do this properly.


Solution

  • As of Rust 1.74 which contains rust-lang/rust#114590, the code works exactly as you originally tried it. This didn't used to work in older versions of Rust.

    Command::new("tput")
        .arg("rc")
        .stdout(io::stderr())
        .status()
        .expect("failed to run tput");