1//! Hello world, via plain syscalls. 2 3#[cfg(all(feature = "std", not(windows)))] 4fn main() -> std::io::Result<()> { 5 // The message to print. It includes an explicit newline because we're 6 // not 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 // Safety: See [here] for the safety conditions for calling `stdout`. In 15 // this example, the code is inside `main` itself so we know how `stdout` 16 // is being used and we know that it's not dropped. 17 // 18 // [here]: https://docs.rs/rustix/*/rustix/io/fn.stdout.html#safety 19 let stdout = unsafe { rustix::io::stdout() }; 20 21 while !bytes.is_empty() { 22 match rustix::io::write(&stdout, bytes) { 23 // `write` can write fewer bytes than requested. In that case, 24 // continue writing with the remainder of the bytes. 25 Ok(n) => bytes = &bytes[n..], 26 27 // `write` can be interrupted before doing any work; if that 28 // happens, retry it. 29 Err(rustix::io::Errno::INTR) => (), 30 31 // `write` can also fail for external reasons, such as running out 32 // of storage space. 33 Err(err) => return Err(err.into()), 34 } 35 } 36 37 Ok(()) 38} 39 40#[cfg(any(not(feature = "std"), windows))] 41fn main() { 42 unimplemented!() 43} 44