shadow_rs

Macro str_index

Source
macro_rules! str_index {
    ($string:expr, $index:expr $(,)*) => { ... };
}
Expand description

Indexes a &'static str constant.

§Signature

This macro acts like a function of this signature:

fn str_index(input: &'static str, range: impl SomeIndex) -> &'static str

and is evaluated at compile-time.

This accepts the same range arguments as str_splice

§Example

use const_format::str_index;

use std::ops::RangeFrom;

assert_eq!(str_index!("foo bar baz", ..7), "foo bar");
assert_eq!(str_index!("foo bar baz", 4..7), "bar");
assert_eq!(str_index!("foo bar baz", 4..), "bar baz");

{
    const IN: &str = "hello world";
    const INDEX: RangeFrom<usize> = 6..;
    // You can pass `const`ants to this macro, not just literals
    const OUT_0: &str = str_index!(IN, INDEX);
    assert_eq!(OUT_0, "world");
}
{
    const OUT: &str = str_index!("hello world", 4);
    assert_eq!(OUT, "o");
}

§Invalid index

Invalid indices cause compilation errors.

const_format::str_index!("foo", 0..10);