Skip to content

Commit

Permalink
Unrolled build for rust-lang#124992
Browse files Browse the repository at this point in the history
Rollup merge of rust-lang#124992 - foresterre:example/is-terminal, r=ChrisDenton

Add example to IsTerminal::is_terminal
  • Loading branch information
rust-timer authored May 19, 2024
2 parents 1d1283e + 0b6baf6 commit e7751ad
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions library/std/src/io/stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1161,7 +1161,41 @@ pub trait IsTerminal: crate::sealed::Sealed {
/// starting with `msys-` or `cygwin-` and ending in `-pty` will be considered terminals.
/// Note that this [may change in the future][changes].
///
/// # Examples
///
/// An example of a type for which `IsTerminal` is implemented is [`Stdin`]:
///
/// ```no_run
/// use std::io::{self, IsTerminal, Write};
///
/// fn main() -> io::Result<()> {
/// let stdin = io::stdin();
///
/// // Indicate that the user is prompted for input, if this is a terminal.
/// if stdin.is_terminal() {
/// print!("> ");
/// io::stdout().flush()?;
/// }
///
/// let mut name = String::new();
/// let _ = stdin.read_line(&mut name)?;
///
/// println!("Hello {}", name.trim_end());
///
/// Ok(())
/// }
/// ```
///
/// The example can be run in two ways:
///
/// - If you run this example by piping some text to it, e.g. `echo "foo" | path/to/executable`
/// it will print: `Hello foo`.
/// - If you instead run the example interactively by running the executable directly, it will
/// panic with the message "Expected input to be piped to the process".
///
///
/// [changes]: io#platform-specific-behavior
/// [`Stdin`]: crate::io::Stdin
#[stable(feature = "is_terminal", since = "1.70.0")]
fn is_terminal(&self) -> bool;
}
Expand Down

0 comments on commit e7751ad

Please sign in to comment.