Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created January 24, 2020 16:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rust-play/95537166fc3a26dd88e55ae975cd0527 to your computer and use it in GitHub Desktop.
Save rust-play/95537166fc3a26dd88e55ae975cd0527 to your computer and use it in GitHub Desktop.
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