Skip to content

Commit 5243a98

Browse files
committed
Add a brief description and two examples to std::process
1 parent 7778906 commit 5243a98

File tree

1 file changed

+43
-7
lines changed

1 file changed

+43
-7
lines changed

src/libstd/process.rs

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,57 @@
1010

1111
//! A module for working with processes.
1212
//!
13+
//! This module provides a [`Command`] struct that can be used to configure and
14+
//! spawn a process, as well as a [`Child`] struct that represents a running or
15+
//! terminated process.
16+
//!
1317
//! # Examples
1418
//!
15-
//! Basic usage where we try to execute the `cat` shell command:
19+
//! Hello world, `std::process` edition:
1620
//!
17-
//! ```should_panic
18-
//! use std::process::Command;
21+
//! ```
22+
//! use std::process:Command;
1923
//!
20-
//! let mut child = Command::new("/bin/cat")
21-
//! .arg("file.txt")
24+
//! // Note that by default, the output of the command will be sent to stdout
25+
//! let child = Command::new("echo")
26+
//! .arg("Hello world")
2227
//! .spawn()
23-
//! .expect("failed to execute child");
28+
//! .expect("Failed to start process");
2429
//!
2530
//! let ecode = child.wait()
26-
//! .expect("failed to wait on child");
31+
//! .expect("Failed to wait on child");
2732
//!
2833
//! assert!(ecode.success());
2934
//! ```
3035
//!
36+
//! Piping output from one command into another command:
37+
//!
38+
//! ```
39+
//! use std::process::{Command, Stdio};
40+
//!
41+
//! // stdout must be configured with `Stdio::piped` in order to use
42+
//! // `echo_child.stdout`
43+
//! let echo_child = Command::new("echo")
44+
//! .arg("Oh no, a tpyo!")
45+
//! .stdout(Stdio::piped())
46+
//! .spawn()
47+
//! .expect("Failed to start echo process");
48+
//!
49+
//! // Note that `echo_child` is moved here, but we won't be needing
50+
//! // `echo_child` anymore
51+
//! let echo_out = echo_child.stdout.expect("Failed to open echo stdout");
52+
//!
53+
//! let mut sed_child = Command::new("sed")
54+
//! .arg("s/tpyo/typo/")
55+
//! .stdin(Stdio::from(echo_out))
56+
//! .stdout(Stdio::piped())
57+
//! .spawn()
58+
//! .expect("Failed to start sed process");
59+
//!
60+
//! let output = sed_child.wait_with_output().expect("Failed to wait on sed");
61+
//! assert_eq!(b"Oh no, a typo!\n", output.stdout.as_slice());
62+
//! ```
63+
//!
3164
//! Calling a command with input and reading its output:
3265
//!
3366
//! ```no_run
@@ -52,6 +85,9 @@
5285
//!
5386
//! assert_eq!(b"test", output.stdout.as_slice());
5487
//! ```
88+
//!
89+
//! [`Command`]: struct.Command.html
90+
//! [`Child`]: struct.Child.html
5591
5692
#![stable(feature = "process", since = "1.0.0")]
5793

0 commit comments

Comments
 (0)