konst::string

Function get_range

Source
pub const fn get_range(string: &str, start: usize, end: usize) -> Option<&str>
Expand description

A const equivalent of string.get(start..end).

§Alternatives

For a const equivalent of string.get(start..) there’s get_from.

For a const equivalent of string.get(..end) there’s get_up_to.

§Example

use konst::string;

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

const SUB0: Option<&str> = string::get_range(STR, 0, 3);
assert_eq!(SUB0, Some("foo"));

const SUB1: Option<&str> = string::get_range(STR, 0, 7);
assert_eq!(SUB1, Some("foo bar"));

const SUB2: Option<&str> = string::get_range(STR, 4, 11);
assert_eq!(SUB2, Some("bar baz"));

const SUB3: Option<&str> = string::get_range(STR, 0, 1000);
assert_eq!(SUB3, None);