Created
January 24, 2020 16:01
-
-
Save rust-play/95537166fc3a26dd88e55ae975cd0527 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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