3 The `std::Child` struct represents a running child process, and exposes the
4 `stdin`, `stdout` and `stderr` handles for interaction with the underlying
9 use std::io::prelude::*;
10 use std::process::{Command, Stdio};
12 static PANGRAM: &'static str =
13 "the quick brown fox jumped over the lazy dog\n";
16 // Spawn the `wc` command
17 let process = match Command::new("wc")
18 .stdin(Stdio::piped())
19 .stdout(Stdio::piped())
21 Err(why) => panic!("couldn't spawn wc: {}", why.description()),
22 Ok(process) => process,
25 // Write a string to the `stdin` of `wc`.
27 // `stdin` has type `Option<ChildStdin>`, but since we know this instance
28 // must have one, we can directly `unwrap` it.
29 match process.stdin.unwrap().write_all(PANGRAM.as_bytes()) {
30 Err(why) => panic!("couldn't write to wc stdin: {}",
32 Ok(_) => println!("sent pangram to wc"),
35 // Because `stdin` does not live after the above calls, it is `drop`ed,
36 // and the pipe is closed.
38 // This is very important, otherwise `wc` wouldn't start processing the
39 // input we just sent.
41 // The `stdout` field also has type `Option<ChildStdout>` so must be unwrapped.
42 let mut s = String::new();
43 match process.stdout.unwrap().read_to_string(&mut s) {
44 Err(why) => panic!("couldn't read wc stdout: {}",
46 Ok(_) => print!("wc responded with:\n{}", s),