Skip to content

Commit 851ffd9

Browse files
committed
Add example to IsTerminal::is_terminal
1 parent 2cce088 commit 851ffd9

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

library/std/src/io/stdio.rs

+33
Original file line numberDiff line numberDiff line change
@@ -1161,7 +1161,40 @@ 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::prelude::*;
1170+
/// use std::io::{self, IsTerminal};
1171+
///
1172+
/// fn main() -> io::Result<()> {
1173+
/// let stdin = io::stdin();
1174+
///
1175+
/// if stdin.is_terminal() {
1176+
/// panic!("Expected input to be piped to the process");
1177+
/// }
1178+
///
1179+
/// let mut name = String::new();
1180+
/// let _ = stdin.read_line(&mut name)?;
1181+
///
1182+
/// println!("Hello {name}");
1183+
///
1184+
/// Ok(())
1185+
/// }
1186+
/// ```
1187+
///
1188+
/// The example can be run in two ways:
1189+
///
1190+
/// - If you run this example by piping some text to it, e.g. `printf foo | path/to/executable`
1191+
/// it will print: `Hello foo`.
1192+
/// - If you instead run the example interactively by running the executable directly, it will
1193+
/// panic with the message "Expected input to be piped to the process".
1194+
///
1195+
///
11641196
/// [changes]: io#platform-specific-behavior
1197+
/// [`Stdin`]: crate::io::Stdin
11651198
#[stable(feature = "is_terminal", since = "1.70.0")]
11661199
fn is_terminal(&self) -> bool;
11671200
}

0 commit comments

Comments
 (0)