How to make a [u8; 11] out of "hello world"?
This works:
pub const HELLO_WORLD: [u8] = "hello world".to_string().as_bytes();
but I need it to be a [u8; 11]
so that I can do simple byte matches, such as:
if buf[4..15] != HELLO_WORLD {
(This is to check "magic" strings in a file format.)
Is there a way of declaring:
pub const HELLO_WORLD: [u8; 11] = "hello world".to_string().as_bytes();
that does not cause an error? (An &[u8; 11]
would also be fine, it's having a type with a fixed length which is important.)
Rust has byte string literals, which are similar to string literals but prefixed with a b
. b"hello world"
will give you a &[u8; 11]
.