| 2 | |
| 3 | #[cfg(all(feature = "stdio", feature = "std", not(windows)))] |
| 4 | fn main() -> std::io::Result<()> { |
| 5 | // The message to print. It includes an explicit newline because we're not |
| 6 | // using `println!`, so we have to include the newline manually. |
| 7 | let message = "Hello, world!\n"; |
| 8 | |
| 9 | // The bytes to print. The `write` syscall operates on byte buffers and |
| 10 | // returns a byte offset if it writes fewer bytes than requested, so we |
| 11 | // need the ability to compute substrings at arbitrary byte offsets. |
| 12 | let mut bytes = message.as_bytes(); |
| 13 | |
| 14 | // In a std-using configuration, `stdout` is always open. |
| 15 | let stdout = rustix::stdio::stdout(); |
| 16 | |
| 17 | while !bytes.is_empty() { |
| 18 | match rustix::io::write(stdout, bytes) { |
| 19 | // `write` can write fewer bytes than requested. In that case, |
| 20 | // continue writing with the remainder of the bytes. |
| 21 | Ok(n) => bytes = &bytes[n..], |
| 22 | |
| 23 | // `write` can be interrupted before doing any work; if that |
| 24 | // happens, retry it. |
| 25 | Err(rustix::io::Errno::INTR) => (), |
| 26 | |
| 27 | // `write` can also fail for external reasons, such as running out |
| 28 | // of storage space. |
| 29 | Err(err) => return Err(err.into()), |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | Ok(()) |
| 34 | } |
| 35 | |
| 36 | #[cfg(any(not(feature = "stdio"), not(feature = "std"), windows))] |
| 37 | fn main() -> Result<(), &'static str> { |