Skip to content

Commit

Permalink
Add example to IsTerminal::is_terminal
Browse files Browse the repository at this point in the history
  • Loading branch information
foresterre committed May 11, 2024
1 parent 19dacee commit 1e56160
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions library/std/src/io/stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1161,7 +1161,39 @@ 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};
///
/// fn main() -> io::Result<()> {
/// let stdin = io::stdin();
///
/// if stdin.is_terminal() {
/// panic!("Expected input to be piped to the process");
/// }
///
/// let mut name = String::new();
/// let _ = stdin.read_line(&mut name)?;
///
/// println!("Hello {name}");
///

Check failure on line 1182 in library/std/src/io/stdio.rs

View workflow job for this annotation

GitHub Actions / PR - mingw-check-tidy

trailing whitespace
/// Ok(())
/// }
/// ```
///
/// The example can be run in two ways:
///

Check failure on line 1188 in library/std/src/io/stdio.rs

View workflow job for this annotation

GitHub Actions / PR - mingw-check-tidy

trailing whitespace
/// - If you run this example by piping some text to it, e.g. `printf 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 1e56160

Please sign in to comment.