Skip to main content

truncate_utf8

Function truncate_utf8 

Source
pub fn truncate_utf8(s: &str, max: usize) -> &str
Expand description

Truncate s to at most max bytes, landing on a valid UTF-8 character boundary.

Unlike naive &s[..max] this avoids splitting multi-byte characters (emoji, CJK).

§Arguments

  • s - The string slice to truncate.
  • max - Maximum byte length. If max exceeds s.len(), the entire string is returned unchanged.

§Returns

A subslice of s that is at most max bytes long and ends on a valid UTF-8 character boundary.

§Panics

Does not panic — uses str::floor_char_boundary internally which clamps safely.