Rust Console output without macros


Example

// use Write trait that contains write() function
use std::io::Write;

fn main() {
    std::io::stdout().write(b"Hello, world!\n").unwrap();
}
  • The std::io::Write trait is designed for objects which accept byte streams. In this case, a handle to standard output is acquired with std::io::stdout().

  • Write::write() accepts a byte slice (&[u8]), which is created with a byte-string literal (b"<string>"). Write::write() returns a Result<usize, IoError>, which contains either the number of bytes written (on success) or an error value (on failure).

  • The call to Result::unwrap() indicates that the call is expected to succeed (Result<usize, IoError> -> usize), and the value is discarded.