konst::string

Function str_up_to

Source
pub const fn str_up_to(string: &str, len: usize) -> &str
Expand description

A const equivalent of &string[..len].

If string.len() < len, this simply returns string back.

§Panics

This function panics if len is inside the string but doesn’t fall on a char boundary.

§Example

use konst::string::str_up_to;

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

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

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

const SUB2: &str = str_up_to(STR, 11);
assert_eq!(SUB2, STR);

const SUB3: &str = str_up_to(STR, 100);
assert_eq!(SUB3, STR);