Reputation: 998
I've noticed that in Rust, we can't use the byte notation for values larger than 128, that is
let x = "\x01\x17\x7f"
is fine since all chars are < 128, but
let x = "\x01\x17\x80"
will fail since \x80
= 128.
Is there any way to still write string-like objects in that format?
Upvotes: 0
Views: 1070
Reputation: 29991
Above 127 you enter the realm of Unicode and must use the \u{codepoint}
escape sequence:
let x = "\u{80}";
Note however that 0x80 by itself isn't a valid byte in a UTF-8 string, so this turns out as two bytes:
let x = "\u{80}";
for b in x.bytes() {
println!("{:X}", b);
}
prints
C2
80
If you instead need the value 0x80, you can't use a string and must use a byte slice:
fn main() {
let x = b"\x80";
for b in x {
println!("{:X}", b);
}
}
prints
80
Upvotes: 4