[Solved] Rust u8 literal notation

  1. I know that Rust has a char literal notation: char - Rust

  2. I want to create u8 literals (in particular, chars 0 -> 127). Is there a better way than

        let x = "x";
        let t = x.as_bytes()[0];

let t = b'x';

5 Likes

As @scottmcm mentioned, there is the b'c' notation, or the b"string" notation for strings. This will actually turn them into a &'static u8 (Or u8 for char notation). Also note that you can only use standard ASCII characters. This won't work for chars with an accent for example; (Playground)

4 Likes