There currently isn't a feature in the language nor in the standard library for combining multiple byte arrays together into a single slice at compile time. There happens to be a pending RFC proposing an extension to the concat!
macro, which currently only works for string literals. There are no guarantees that it will ever be approved, though.
A few alternative solutions follow.
- Make a UTF-8 string and call
as_bytes()
. As a string slice however, it won't be possible to include certain character combinations or write a compact representation of [0u8; 250]
.
pub const BYTES: &[u8] = concat!("abcdef", "\0\0\0\0\0\0\0\0\0\0").as_bytes();
- Some crates may claim to provide the solution that you are looking for. See
proc_concat_bytes
, for example:
use proc_concat_bytes::concat_bytes;
let c_str = &concat_bytes!(b"Hello World!", b'\0')[..]).unwrap();
- Write the bytes to a file, then use
include_bytes!
.
const BYTES: &[u8; 258] = include_bytes!("bytes.bin");
- Enter before-main land and construct a static vector with
lazy_static!
. We would then have a vector constructed by the program, but should fulfil the intended requirements once built.
use lazy_static::lazy_static;
lazy_static! {
static ref BYTES: Vec<u8> = {
let mut data = b"abcdefg".to_vec();
data.extend(&[0u8; 250][..]);
data
};
}
See also: