|
10 | 10 |
|
11 | 11 | //! A module for working with processes.
|
12 | 12 | //!
|
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. |
14 | 16 | //!
|
15 |
| -//! Basic usage where we try to execute the `cat` shell command: |
| 17 | +//! # Spawning a process |
16 | 18 | //!
|
17 |
| -//! ```should_panic |
| 19 | +//! The [`Command`] struct is used to configure and spawn processes: |
| 20 | +//! |
| 21 | +//! ``` |
18 | 22 | //! use std::process::Command;
|
19 | 23 | //!
|
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}; |
24 | 47 | //!
|
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"); |
27 | 66 | //!
|
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()); |
29 | 69 | //! ```
|
30 | 70 | //!
|
31 |
| -//! Calling a command with input and reading its output: |
| 71 | +//! Note that [`ChildStderr`] and [`ChildStdout`] implement [`Write`] and |
| 72 | +//! [`ChildStdin`] implements [`Read`]: |
32 | 73 | //!
|
33 | 74 | //! ```no_run
|
34 | 75 | //! use std::process::{Command, Stdio};
|
|
52 | 93 | //!
|
53 | 94 | //! assert_eq!(b"test", output.stdout.as_slice());
|
54 | 95 | //! ```
|
| 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 |
55 | 116 |
|
56 | 117 | #![stable(feature = "process", since = "1.0.0")]
|
57 | 118 |
|
|
0 commit comments