Skip to content

Commit 4c99211

Browse files
committed
Rollup merge of rust-lang#45106 - Pirh:process_stdio_docs, r=dtolnay
Add links and examples for std::process::Stdio As per rust-lang#29370
2 parents 9687c2e + 9772003 commit 4c99211

File tree

1 file changed

+108
-1
lines changed

1 file changed

+108
-1
lines changed

src/libstd/process.rs

Lines changed: 108 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -742,21 +742,128 @@ impl fmt::Debug for Output {
742742
}
743743
}
744744

745-
/// Describes what to do with a standard I/O stream for a child process.
745+
/// Describes what to do with a standard I/O stream for a child process when
746+
/// passed to the [`stdin`], [`stdout`], and [`stderr`] methods of [`Command`].
747+
///
748+
/// [`stdin`]: struct.Command.html#method.stdin
749+
/// [`stdout`]: struct.Command.html#method.stdout
750+
/// [`stderr`]: struct.Command.html#method.stderr
751+
/// [`Command`]: struct.Command.html
746752
#[stable(feature = "process", since = "1.0.0")]
747753
pub struct Stdio(imp::Stdio);
748754

749755
impl Stdio {
750756
/// A new pipe should be arranged to connect the parent and child processes.
757+
///
758+
/// # Examples
759+
///
760+
/// With stdout:
761+
///
762+
/// ```no_run
763+
/// use std::process::{Command, Stdio};
764+
///
765+
/// let output = Command::new("echo")
766+
/// .arg("Hello, world!")
767+
/// .stdout(Stdio::piped())
768+
/// .output()
769+
/// .expect("Failed to execute command");
770+
///
771+
/// assert_eq!(String::from_utf8_lossy(&output.stdout), "Hello, world!\n");
772+
/// // Nothing echoed to console
773+
/// ```
774+
///
775+
/// With stdin:
776+
///
777+
/// ```no_run
778+
/// use std::io::Write;
779+
/// use std::process::{Command, Stdio};
780+
///
781+
/// let mut child = Command::new("rev")
782+
/// .stdin(Stdio::piped())
783+
/// .stdout(Stdio::piped())
784+
/// .spawn()
785+
/// .expect("Failed to spawn child process");
786+
///
787+
/// {
788+
/// let mut stdin = child.stdin.as_mut().expect("Failed to open stdin");
789+
/// stdin.write_all("Hello, world!".as_bytes()).expect("Failed to write to stdin");
790+
/// }
791+
///
792+
/// let output = child.wait_with_output().expect("Failed to read stdout");
793+
/// assert_eq!(String::from_utf8_lossy(&output.stdout), "!dlrow ,olleH\n");
794+
/// ```
751795
#[stable(feature = "process", since = "1.0.0")]
752796
pub fn piped() -> Stdio { Stdio(imp::Stdio::MakePipe) }
753797

754798
/// The child inherits from the corresponding parent descriptor.
799+
///
800+
/// # Examples
801+
///
802+
/// With stdout:
803+
///
804+
/// ```no_run
805+
/// use std::process::{Command, Stdio};
806+
///
807+
/// let output = Command::new("echo")
808+
/// .arg("Hello, world!")
809+
/// .stdout(Stdio::inherit())
810+
/// .output()
811+
/// .expect("Failed to execute command");
812+
///
813+
/// assert_eq!(String::from_utf8_lossy(&output.stdout), "");
814+
/// // "Hello, world!" echoed to console
815+
/// ```
816+
///
817+
/// With stdin:
818+
///
819+
/// ```no_run
820+
/// use std::process::{Command, Stdio};
821+
///
822+
/// let output = Command::new("rev")
823+
/// .stdin(Stdio::inherit())
824+
/// .stdout(Stdio::piped())
825+
/// .output()
826+
/// .expect("Failed to execute command");
827+
///
828+
/// println!("You piped in the reverse of: {}", String::from_utf8_lossy(&output.stdout));
829+
/// ```
755830
#[stable(feature = "process", since = "1.0.0")]
756831
pub fn inherit() -> Stdio { Stdio(imp::Stdio::Inherit) }
757832

758833
/// This stream will be ignored. This is the equivalent of attaching the
759834
/// stream to `/dev/null`
835+
///
836+
/// # Examples
837+
///
838+
/// With stdout:
839+
///
840+
/// ```no_run
841+
/// use std::process::{Command, Stdio};
842+
///
843+
/// let output = Command::new("echo")
844+
/// .arg("Hello, world!")
845+
/// .stdout(Stdio::null())
846+
/// .output()
847+
/// .expect("Failed to execute command");
848+
///
849+
/// assert_eq!(String::from_utf8_lossy(&output.stdout), "");
850+
/// // Nothing echoed to console
851+
/// ```
852+
///
853+
/// With stdin:
854+
///
855+
/// ```no_run
856+
/// use std::process::{Command, Stdio};
857+
///
858+
/// let output = Command::new("rev")
859+
/// .stdin(Stdio::null())
860+
/// .stdout(Stdio::piped())
861+
/// .output()
862+
/// .expect("Failed to execute command");
863+
///
864+
/// assert_eq!(String::from_utf8_lossy(&output.stdout), "");
865+
/// // Ignores any piped-in input
866+
/// ```
760867
#[stable(feature = "process", since = "1.0.0")]
761868
pub fn null() -> Stdio { Stdio(imp::Stdio::Null) }
762869
}

0 commit comments

Comments
 (0)