|
10 | 10 |
|
11 | 11 | //! A module for working with processes.
|
12 | 12 | //!
|
| 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 | +//! |
13 | 17 | //! # Examples
|
14 | 18 | //!
|
15 |
| -//! Basic usage where we try to execute the `cat` shell command: |
| 19 | +//! Hello world, `std::process` edition: |
16 | 20 | //!
|
17 |
| -//! ```should_panic |
18 |
| -//! use std::process::Command; |
| 21 | +//! ``` |
| 22 | +//! use std::process:Command; |
19 | 23 | //!
|
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") |
22 | 27 | //! .spawn()
|
23 |
| -//! .expect("failed to execute child"); |
| 28 | +//! .expect("Failed to start process"); |
24 | 29 | //!
|
25 | 30 | //! let ecode = child.wait()
|
26 |
| -//! .expect("failed to wait on child"); |
| 31 | +//! .expect("Failed to wait on child"); |
27 | 32 | //!
|
28 | 33 | //! assert!(ecode.success());
|
29 | 34 | //! ```
|
30 | 35 | //!
|
| 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 | +//! |
31 | 64 | //! Calling a command with input and reading its output:
|
32 | 65 | //!
|
33 | 66 | //! ```no_run
|
|
52 | 85 | //!
|
53 | 86 | //! assert_eq!(b"test", output.stdout.as_slice());
|
54 | 87 | //! ```
|
| 88 | +//! |
| 89 | +//! [`Command`]: struct.Command.html |
| 90 | +//! [`Child`]: struct.Child.html |
55 | 91 |
|
56 | 92 | #![stable(feature = "process", since = "1.0.0")]
|
57 | 93 |
|
|
0 commit comments