Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Code shared from the Rust Playground
use std::process::{Command, Stdio};
fn main() {
let mut rev = Command::new("rev")
.stdin(Stdio::piped())
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.spawn()
.expect("rev failed to spawn");
let mut stdin: Stdio = rev.stdin.take().expect("rev had no stdin?").into();
while Command::new("true")
.status()
.map(|status| status.success())
.unwrap_or(false)
{
let mut fortune = Command::new("fortune")
.stdin(Stdio::null())
.stdout(stdin)
.stderr(Stdio::inherit())
.spawn()
.expect("fortune failed to spawn");
if !fortune
.wait()
.expect("waiting for fortune to finish failed")
.success()
{
panic!("fortune failed??");
}
// **************** But this next part will always fail, can't get the pipe back
// **************** have to use a third party library to call the `dup` / `dup2` syscall
// **************** directly.
stdin = fortune
.stdout
.take()
.expect("hey, fortune didn't give stdout back!")
.into();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.