about summary refs log tree commit diff
path: root/src/libstd/process.rs
diff options
context:
space:
mode:
authorBryan Tan <techniux@gmail.com>2017-10-13 23:38:55 -0700
committerBryan Tan <techniux@gmail.com>2017-10-14 19:19:19 -0700
commit5243a98b48da3bdac81e9685526d731e88dc48a1 (patch)
tree8ba902e4c32796357a64e167c26c285b6883b261 /src/libstd/process.rs
parent7778906bee9e8e93792353824548044a758b72f4 (diff)
downloadrust-5243a98b48da3bdac81e9685526d731e88dc48a1.tar.gz
rust-5243a98b48da3bdac81e9685526d731e88dc48a1.zip
Add a brief description and two examples to std::process
Diffstat (limited to 'src/libstd/process.rs')
-rw-r--r--src/libstd/process.rs50
1 files changed, 43 insertions, 7 deletions
diff --git a/src/libstd/process.rs b/src/libstd/process.rs
index 7d8ce4154fb..d3e60e3dff9 100644
--- a/src/libstd/process.rs
+++ b/src/libstd/process.rs
@@ -10,24 +10,57 @@
 
 //! A module for working with processes.
 //!
+//! This module provides a [`Command`] struct that can be used to configure and
+//! spawn a process, as well as a [`Child`] struct that represents a running or
+//! terminated process.
+//!
 //! # Examples
 //!
-//! Basic usage where we try to execute the `cat` shell command:
+//! Hello world, `std::process` edition:
 //!
-//! ```should_panic
-//! use std::process::Command;
+//! ```
+//! use std::process:Command;
 //!
-//! let mut child = Command::new("/bin/cat")
-//!                         .arg("file.txt")
+//! // Note that by default, the output of the command will be sent to stdout
+//! let child = Command::new("echo")
+//!                         .arg("Hello world")
 //!                         .spawn()
-//!                         .expect("failed to execute child");
+//!                         .expect("Failed to start process");
 //!
 //! let ecode = child.wait()
-//!                  .expect("failed to wait on child");
+//!                  .expect("Failed to wait on child");
 //!
 //! assert!(ecode.success());
 //! ```
 //!
+//! Piping output from one command into another command:
+//!
+//! ```
+//! use std::process::{Command, Stdio};
+//!
+//! // stdout must be configured with `Stdio::piped` in order to use
+//! // `echo_child.stdout`
+//! let echo_child = Command::new("echo")
+//!     .arg("Oh no, a tpyo!")
+//!     .stdout(Stdio::piped())
+//!     .spawn()
+//!     .expect("Failed to start echo process");
+//!
+//! // Note that `echo_child` is moved here, but we won't be needing
+//! // `echo_child` anymore
+//! let echo_out = echo_child.stdout.expect("Failed to open echo stdout");
+//!
+//! let mut sed_child = Command::new("sed")
+//!     .arg("s/tpyo/typo/")
+//!     .stdin(Stdio::from(echo_out))
+//!     .stdout(Stdio::piped())
+//!     .spawn()
+//!     .expect("Failed to start sed process");
+//!
+//! let output = sed_child.wait_with_output().expect("Failed to wait on sed");
+//! assert_eq!(b"Oh no, a typo!\n", output.stdout.as_slice());
+//! ```
+//!
 //! Calling a command with input and reading its output:
 //!
 //! ```no_run
@@ -52,6 +85,9 @@
 //!
 //! assert_eq!(b"test", output.stdout.as_slice());
 //! ```
+//!
+//! [`Command`]: struct.Command.html
+//! [`Child`]: struct.Child.html
 
 #![stable(feature = "process", since = "1.0.0")]