How to get status code of a program which was used as a pipe source without violating Rust's borrow rule?

I'm trying to pipe a program onto another, but I can't call source.wait() due to source.stdout being already moved.

use std::process::{Command, Stdio};

fn main() {
    let mut source = Command::new("echo").arg("hello").spawn().unwrap();
    Command::new("cowsay")
        .stdin(source.stdout.unwrap())
        .stdout(Stdio::inherit())
        .stderr(Stdio::inherit())
        .spawn()
        .unwrap()
        .wait()
        .unwrap();
    let source_status = source.wait().unwrap(); // error here
}

Use source.stdout.take().unwrap() instead of source.stdout.unwrap().

2 Likes