macro_rules! try_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.
Returns Some(sliced), or None if the index is out of range or, for strings,
if the slice would split a unicode codepoint.
Alternately use slice! if you want to panic on error instead.
const STR: Option<&str> = try_slice!("const slice", ..5); // Some("const")
const BYTES: Option<&[u8]> = try_slice!(b"01234", 1..=3); // Some(b"123")
const BYTES2: &[u8] = unwrap_some!(BYTES); // b"123"
const RANGE: Range<usize> = (BYTES2[0] - b'0') as usize..(BYTES2[2] - b'0') as usize;
const STR2: Option<&str> = try_slice!(unwrap_some!(STR), RANGE); // Some("on")