When printing a u8
array in Rust using println!("{:?}", some_u8_slice);
this prints the numeric values (as it should).
What is the most direct way to format the characters as-is into the string without assuming any particular encoding.
Something like iterating over the byte string and writing each character to the stdout
(without so much hassle).
Can this be done using Rusts format!
?
Otherwise whats the most convenient way to print a u8 slice?
If I can't assume a particular encoding, the way I normally do it is with the
std::ascii::escape_default
function. Basically, it will show most ASCII characters as they are, and then escape everything else. The downside is that you won't see every possible Unicode codepoint even if portions of your strict are correct UTF-8, but it does the job for most uses:Output:
foo\xe2\x98\x83bar\xffbaz
Another approach is to lossily decode the contents into a string and print that. If there's any invalid UTF-8, you'll get a Unicode replacement character instead of hex escapes of the raw bytes, but you will get to see all valid UTF-8 encoded Unicode codepoints:
Output:
foo☃bar�baz
The variant using
escape_default()
:If you just want to shovel the raw bytes unescaped to stdout, which can be especially useful when the output is redirected to a pipe or a file then following should do the job:
The
flush
is necessary sincewrite_all
immediately followed by a program exit fails to deliver the bytes to the underlying file descriptor.The simplest way is
stdout().write_all(some_u8_slice)
. This will simply output the bytes, with no regard for their encoding. This is useful for binary data, or text in some unknown encoding where you want to preserve the original encoding.If you want to treat the data as a string and you know that the encoding is UTF-8 (or a UTF-8 subset like ASCII) then you can do this:
This will check that the data is valid UTF-8 before printing it.