How to output b'A' as b'A' and not as 65 using format specifier?

fn main() {
let b = 0x12ffa;
let c = 0o7765;
let d = b'A';

println!("{:#x}\n{:#o}\n{???}\n",b,c,d,);

}

I don't think there is a formatter for that. You can wrap your bytes in a bstr::Bstr for printing though. A description of Bstr's Display implementation can be found here.

2 Likes
println!("{} {:?}", d as char, d as char);
// A 'A'
3 Likes

I want without casting)

You could always use a wrapper function:

fn print_as_char(x: u8) -> impl Display {
    struct C(u8);
    impl Display for C {
        fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
            write!(f, "b{:?}", char::from(self.0))
        }
    }
    C(x)
}

I needed a formatter, but as I understand it there is no formatter

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.