macro_rules! slice { ($slicable:expr, $index:expr) => { ... }; }
Expand description
Slice an item in a const context. The first argument is the item to slice, and the second is the slice index, which can be a usize or any usize range type. Panics if the index is out of range or, for strings, if the slice would split a unicode codepoint.
Alternately use try_slice! to get an Option instead of panicing.
const STR: &str = slice!("const slice", ..5); // "const"
const BYTES: &[u8] = slice!(b"01234", 1..=3); // b"123"
const RANGE: Range<usize> = (BYTES[0] - b'0') as usize..(BYTES[2] - b'0') as usize;
const STR2: &str = slice!(STR, RANGE); // "on"