[Solved] Rust u8 literal notation

#1
  1. I know that Rust has a char literal notation: https://doc.rust-lang.org/std/primitive.char.html

  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];
0 Likes

#2

let t = b'x';

3 Likes

#3

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)

2 Likes