about summary refs log tree commit diff
diff options
context:
space:
mode:
authorCorey Farwell <coreyf@rwell.org>2017-02-28 08:33:07 -0500
committerGitHub <noreply@github.com>2017-02-28 08:33:07 -0500
commit6ac7bf5c675f0fec7660e7fbf25e159021c91d7d (patch)
tree46ab85c45fbd2a1c829d0bff5619b0b52095c361
parent15cd43b4aa4f8d059978334d09dda89fb9138ab0 (diff)
parent8079bf35c596eef32ec872d35e28c90e4744f61e (diff)
downloadrust-6ac7bf5c675f0fec7660e7fbf25e159021c91d7d.tar.gz
rust-6ac7bf5c675f0fec7660e7fbf25e159021c91d7d.zip
Rollup merge of #40122 - robinst:process-add-example-for-writing-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
-rw-r--r--src/libstd/process.rs25
1 files changed, 25 insertions, 0 deletions
diff --git a/src/libstd/process.rs b/src/libstd/process.rs
index 4ff35738b50..f846ef3e69e 100644
--- a/src/libstd/process.rs
+++ b/src/libstd/process.rs
@@ -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")]