pub(crate) fn truncate_at_char_boundary(s: &str, max_bytes: usize) -> &strExpand description
Shorten s to at most max_bytes, cutting at a character boundary.
Returns s unchanged when it already fits. When it does not, the cut moves
down to the nearest boundary at or below max_bytes, so the result can be
shorter than the budget but is never longer and never invalid.
Illustrative rather than executable: this helper is crate-internal, and a doctest compiles as its own crate, so no import path reaches it. The cases below are asserted for real in this module’s tests.
truncate_at_char_boundary:
truncate_at_char_boundary("hello", 10) == "hello"
truncate_at_char_boundary("hello", 3) == "hel"
// "α" is two bytes, so a budget of 3 fits one of them, not one and a half.
truncate_at_char_boundary("ααα", 3) == "α"