Skip to content

Commit 20e1773

Browse files
committed
Add example to IsTerminal::is_terminal
1 parent 19dacee commit 20e1773

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

library/std/src/io/stdio.rs

+31
Original file line numberDiff line numberDiff line change
@@ -1161,7 +1161,38 @@ pub trait IsTerminal: crate::sealed::Sealed {
11611161
/// starting with `msys-` or `cygwin-` and ending in `-pty` will be considered terminals.
11621162
/// Note that this [may change in the future][changes].
11631163
///
1164+
/// # Examples
1165+
///
1166+
/// An example of a type for which `IsTerminal` is implemented is [`Stdin`]:
1167+
///
1168+
/// ```no_run
1169+
/// use std::io::{self, IsTerminal};
1170+
///
1171+
/// fn main() -> io::Result<()> {
1172+
/// let stdin = io::stdin();
1173+
///
1174+
/// if stdin.is_terminal() {
1175+
/// panic!("Expected input to be piped to the process");
1176+
/// }
1177+
///
1178+
/// let mut name = String::new();
1179+
/// let _ = stdin.read_line(&mut name)?;
1180+
///
1181+
/// println!("Hello {name}");
1182+
///
1183+
/// Ok(())
1184+
/// }
1185+
/// ```
1186+
///
1187+
/// The example can be run in two ways:
1188+
///
1189+
/// - If you run this example by piping some text to it, e.g. `printf foo | path/to/executable`
1190+
/// it will print: `Hello foo`.
1191+
/// - If you instead run the example interactively by running the executable directly, it will
1192+
/// panic with the message "Expected input to be piped to the process".
1193+
///
11641194
/// [changes]: io#platform-specific-behavior
1195+
/// [`Stdin`]: crate::io::Stdin
11651196
#[stable(feature = "is_terminal", since = "1.70.0")]
11661197
fn is_terminal(&self) -> bool;
11671198
}

0 commit comments

Comments
 (0)