File tree 1 file changed +33
-0
lines changed
1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change @@ -1161,7 +1161,40 @@ pub trait IsTerminal: crate::sealed::Sealed {
1161
1161
/// starting with `msys-` or `cygwin-` and ending in `-pty` will be considered terminals.
1162
1162
/// Note that this [may change in the future][changes].
1163
1163
///
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
+ ///
1164
1196
/// [changes]: io#platform-specific-behavior
1197
+ /// [`Stdin`]: crate::io::Stdin
1165
1198
#[ stable( feature = "is_terminal" , since = "1.70.0" ) ]
1166
1199
fn is_terminal ( & self ) -> bool ;
1167
1200
}
You can’t perform that action at this time.
0 commit comments