Skip to content

Commit 7d475a2

Browse files
committed
Auto merge of #45295 - Technius:docs/process, r=steveklabnik
Improve std::process module docs Addresses part of #29370 I've changed the first `cat` example to a "Hello World" example involving echo, and I've also added another example showing how to pipe output. I'm still working on the module-level description. For now, I'd like feedback on the examples. r? @steveklabnik
2 parents 1fdcf52 + 84ab6ae commit 7d475a2

File tree

1 file changed

+72
-11
lines changed

1 file changed

+72
-11
lines changed

src/libstd/process.rs

+72-11
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,66 @@
1010

1111
//! A module for working with processes.
1212
//!
13-
//! # Examples
13+
//! This module is mostly concerned with spawning and interacting with child
14+
//! processes, but it also provides [`abort`] and [`exit`] for terminating the
15+
//! current process.
1416
//!
15-
//! Basic usage where we try to execute the `cat` shell command:
17+
//! # Spawning a process
1618
//!
17-
//! ```should_panic
19+
//! The [`Command`] struct is used to configure and spawn processes:
20+
//!
21+
//! ```
1822
//! use std::process::Command;
1923
//!
20-
//! let mut child = Command::new("/bin/cat")
21-
//! .arg("file.txt")
22-
//! .spawn()
23-
//! .expect("failed to execute child");
24+
//! let output = Command::new("echo")
25+
//! .arg("Hello world")
26+
//! .output()
27+
//! .expect("Failed to execute command");
28+
//!
29+
//! assert_eq!(b"Hello world\n", output.stdout.as_slice());
30+
//! ```
31+
//!
32+
//! Several methods on [`Command`], such as [`spawn`] or [`output`], can be used
33+
//! to spawn a process. In particular, [`output`] spawns the child process and
34+
//! waits until the process terminates, while [`spawn`] will return a [`Child`]
35+
//! that represents the spawned child process.
36+
//!
37+
//! # Handling I/O
38+
//!
39+
//! The [`stdout`], [`stdin`], and [`stderr`] of a child process can be
40+
//! configured by passing an [`Stdio`] to the corresponding method on
41+
//! [`Command`]. Once spawned, they can be accessed from the [`Child`]. For
42+
//! example, piping output from one command into another command can be done
43+
//! like so:
44+
//!
45+
//! ```no_run
46+
//! use std::process::{Command, Stdio};
2447
//!
25-
//! let ecode = child.wait()
26-
//! .expect("failed to wait on child");
48+
//! // stdout must be configured with `Stdio::piped` in order to use
49+
//! // `echo_child.stdout`
50+
//! let echo_child = Command::new("echo")
51+
//! .arg("Oh no, a tpyo!")
52+
//! .stdout(Stdio::piped())
53+
//! .spawn()
54+
//! .expect("Failed to start echo process");
55+
//!
56+
//! // Note that `echo_child` is moved here, but we won't be needing
57+
//! // `echo_child` anymore
58+
//! let echo_out = echo_child.stdout.expect("Failed to open echo stdout");
59+
//!
60+
//! let mut sed_child = Command::new("sed")
61+
//! .arg("s/tpyo/typo/")
62+
//! .stdin(Stdio::from(echo_out))
63+
//! .stdout(Stdio::piped())
64+
//! .spawn()
65+
//! .expect("Failed to start sed process");
2766
//!
28-
//! assert!(ecode.success());
67+
//! let output = sed_child.wait_with_output().expect("Failed to wait on sed");
68+
//! assert_eq!(b"Oh no, a typo!\n", output.stdout.as_slice());
2969
//! ```
3070
//!
31-
//! Calling a command with input and reading its output:
71+
//! Note that [`ChildStderr`] and [`ChildStdout`] implement [`Write`] and
72+
//! [`ChildStdin`] implements [`Read`]:
3273
//!
3374
//! ```no_run
3475
//! use std::process::{Command, Stdio};
@@ -52,6 +93,26 @@
5293
//!
5394
//! assert_eq!(b"test", output.stdout.as_slice());
5495
//! ```
96+
//!
97+
//! [`abort`]: fn.abort.html
98+
//! [`exit`]: fn.exit.html
99+
//!
100+
//! [`Command`]: struct.Command.html
101+
//! [`spawn`]: struct.Command.html#method.spawn
102+
//! [`output`]: struct.Command.html#method.output
103+
//!
104+
//! [`Child`]: struct.Child.html
105+
//! [`ChildStdin`]: struct.ChildStdin.html
106+
//! [`ChildStdout`]: struct.ChildStdout.html
107+
//! [`ChildStderr`]: struct.ChildStderr.html
108+
//! [`Stdio`]: struct.Stdio.html
109+
//!
110+
//! [`stdout`]: struct.Command.html#method.stdout
111+
//! [`stdin`]: struct.Command.html#method.stdin
112+
//! [`stderr`]: struct.Command.html#method.stderr
113+
//!
114+
//! [`Write`]: ../io/trait.Write.html
115+
//! [`Read`]: ../io/trait.Read.html
55116
56117
#![stable(feature = "process", since = "1.0.0")]
57118

0 commit comments

Comments
 (0)