diff options
| author | Martijn <garm@ilumeo.com> | 2024-05-11 00:56:41 +0200 |
|---|---|---|
| committer | Martijn <garm@ilumeo.com> | 2024-05-19 20:00:02 +0200 |
| commit | 0b6baf6130d2fa67c2f81a80cf53a5b337845181 (patch) | |
| tree | 3d3cc6457fd2a6dafd4f3d41813766adc0c3d1c6 /library/std/src/io/stdio.rs | |
| parent | 2d89cee6258ed0029268ef33e13cc63e433bd243 (diff) | |
| download | rust-0b6baf6130d2fa67c2f81a80cf53a5b337845181.tar.gz rust-0b6baf6130d2fa67c2f81a80cf53a5b337845181.zip | |
Add example to IsTerminal::is_terminal
Diffstat (limited to 'library/std/src/io/stdio.rs')
| -rw-r--r-- | library/std/src/io/stdio.rs | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/library/std/src/io/stdio.rs b/library/std/src/io/stdio.rs index 07fa9259e0b..c8968b74b12 100644 --- a/library/std/src/io/stdio.rs +++ b/library/std/src/io/stdio.rs @@ -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; } |
