Skip to content

Commit 9debf51

Browse files
committed
Auto merge of #32338 - lukaspustina:doc-std-process, r=alexcrichton
Extends rustdoc on how to caputure output - The documentation is quite about how to caputure a process' output when using ` std::process::Child::wait_with_output()`. - This PR adds an example for this particular use case.
2 parents ccc7e95 + 5613372 commit 9debf51

File tree

1 file changed

+37
-11
lines changed

1 file changed

+37
-11
lines changed

src/libstd/process.rs

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ use sys_common::{AsInner, AsInnerMut, FromInner, IntoInner};
3838
/// let mut child = Command::new("/bin/cat")
3939
/// .arg("file.txt")
4040
/// .spawn()
41-
/// .unwrap_or_else(|e| { panic!("failed to execute child: {}", e) });
41+
/// .expect("failed to execute child");
4242
///
4343
/// let ecode = child.wait()
44-
/// .unwrap_or_else(|e| { panic!("failed to wait on child: {}", e) });
44+
/// .expect("failed to wait on child");
4545
///
4646
/// assert!(ecode.success());
4747
/// ```
@@ -195,7 +195,8 @@ impl FromInner<AnonPipe> for ChildStderr {
195195
/// .arg("-c")
196196
/// .arg("echo hello")
197197
/// .output()
198-
/// .unwrap_or_else(|e| { panic!("failed to execute process: {}", e) });
198+
/// .expect("failed to execute proces");
199+
///
199200
/// let hello = output.stdout;
200201
/// ```
201202
#[stable(feature = "process", since = "1.0.0")]
@@ -305,15 +306,16 @@ impl Command {
305306
///
306307
/// # Examples
307308
///
308-
/// ```
309+
/// ```should_panic
309310
/// use std::process::Command;
310-
/// let output = Command::new("cat").arg("foo.txt").output().unwrap_or_else(|e| {
311-
/// panic!("failed to execute process: {}", e)
312-
/// });
311+
/// let output = Command::new("/bin/cat").arg("file.txt").output()
312+
/// .expect("failed to execute process");
313313
///
314314
/// println!("status: {}", output.status);
315315
/// println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
316316
/// println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
317+
///
318+
/// assert!(output.status.success());
317319
/// ```
318320
#[stable(feature = "process", since = "1.0.0")]
319321
pub fn output(&mut self) -> io::Result<Output> {
@@ -328,14 +330,15 @@ impl Command {
328330
///
329331
/// # Examples
330332
///
331-
/// ```
333+
/// ```should_panic
332334
/// use std::process::Command;
333335
///
334-
/// let status = Command::new("ls").status().unwrap_or_else(|e| {
335-
/// panic!("failed to execute process: {}", e)
336-
/// });
336+
/// let status = Command::new("/bin/cat").arg("file.txt").status()
337+
/// .expect("failed to execute process");
337338
///
338339
/// println!("process exited with: {}", status);
340+
///
341+
/// assert!(status.success());
339342
/// ```
340343
#[stable(feature = "process", since = "1.0.0")]
341344
pub fn status(&mut self) -> io::Result<ExitStatus> {
@@ -499,6 +502,29 @@ impl Child {
499502
/// before waiting. This helps avoid deadlock: it ensures that the
500503
/// child does not block waiting for input from the parent, while
501504
/// the parent waits for the child to exit.
505+
///
506+
/// By default, stdin, stdout and stderr are inherited from the parent.
507+
/// In order to capture the output into this `Result<Output>` it is
508+
/// necessary to create new pipes between parent and child. Use
509+
/// `stdout(Stdio::piped())` or `stderr(Stdio::piped())`, respectively.
510+
///
511+
/// # Examples
512+
///
513+
/// ```should_panic
514+
/// use std::process::{Command, Stdio};
515+
///
516+
/// let mut child = Command::new("/bin/cat")
517+
/// .arg("file.txt")
518+
/// .stdout(Stdio::piped())
519+
/// .spawn()
520+
/// .expect("failed to execute child");
521+
///
522+
/// let ecode = child.wait_with_output()
523+
/// .expect("failed to wait on child");
524+
///
525+
/// assert!(ecode.status.success());
526+
/// ```
527+
///
502528
#[stable(feature = "process", since = "1.0.0")]
503529
pub fn wait_with_output(mut self) -> io::Result<Output> {
504530
drop(self.stdin.take());

0 commit comments

Comments
 (0)