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

⚓ Rust    📅 2025-06-20    👤 surdeus    👁️ 6      

surdeus

Warning

This post was published 54 days ago. The information described in this article may have changed.

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
}

1 post - 1 participant

Read full topic

🏷️ rust_feed