Skip to content

Commit

Permalink
Rollup merge of rust-lang#40122 - robinst:process-add-example-for-wri…
Browse files Browse the repository at this point in the history
…ting-to-stdin, r=alexcrichton

Example for how to provide stdin using std::process::Command

Spawning a child process and writing to its stdin is a bit tricky due to
`as_mut` and having to use a limited borrow. An example for this might
help newer users.

r? @steveklabnik
  • Loading branch information
frewsxcv authored Feb 28, 2017
2 parents 15cd43b + 8079bf3 commit 6ac7bf5
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/libstd/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,31 @@
//!
//! assert!(ecode.success());
//! ```
//!
//! Calling a command with input and reading its output:
//!
//! ```no_run
//! use std::process::{Command, Stdio};
//! use std::io::Write;
//!
//! let mut child = Command::new("/bin/cat")
//! .stdin(Stdio::piped())
//! .stdout(Stdio::piped())
//! .spawn()
//! .expect("failed to execute child");
//!
//! {
//! // limited borrow of stdin
//! let stdin = child.stdin.as_mut().expect("failed to get stdin");
//! stdin.write_all(b"test").expect("failed to write to stdin");
//! }
//!
//! let output = child
//! .wait_with_output()
//! .expect("failed to wait on child");
//!
//! assert_eq!(b"test", output.stdout.as_slice());
//! ```
#![stable(feature = "process", since = "1.0.0")]

Expand Down

0 comments on commit 6ac7bf5

Please sign in to comment.