How to output a u8 slice in hexadecimal in Rust?
Hello, colleagues! I'm new to Rust and trying to figure out how to display byte data. I have a `&[u8]` slice, for example, a byte array obtained after reading a file or hashing. I need to output it to the console as a familiar hexadecimal string, like it's done in other languages (e.g., `binascii.hexlify` in Python or `%02x` in C).
The problem is that a simple `println!("{:?}", slice)` outputs something like `[72, 101, 108, 108, 111]` — these are decimal numbers, but I need a string like `48656c6c6f`. I tried using `{:X}` or `{:02x}` in the format string, but it doesn't work for slices directly.
Here's an example of the code I have:
```rust
fn main() {
let data: &[u8] = &[72, 101, 108, 108, 111]; // "Hello" in ASCII
// How to output data as "48656c6c6f"?
}
```
No answers yet.
Log in to leave an answer