Function konst::string::str_range

source ·
pub const fn str_range(string: &str, start: usize, end: usize) -> &str
Expand description

A const equivalent of &string[start..end].

If start >= end or string.len() < start , this returns an empty string.

If string.len() < end, this returns the string from start.

§Alternatives

For a const equivalent of &string[start..] there’s str_from.

For a const equivalent of &string[..end] there’s str_up_to.

§Panics

This function panics if either start or end are inside the string and don’t fall on a char boundary.

§Example

use konst::string::str_range;

const STR: &str = "foo bar baz";

const SUB0: &str = str_range(STR, 0, 3);
assert_eq!(SUB0, "foo");

const SUB1: &str = str_range(STR, 0, 7);
assert_eq!(SUB1, "foo bar");

const SUB2: &str = str_range(STR, 4, 11);
assert_eq!(SUB2, "bar baz");

const SUB3: &str = str_range(STR, 0, 1000);
assert_eq!(SUB3, STR);