Skip to main content

truncate_utf8

Function truncate_utf8 

Source
pub fn truncate_utf8(content: &str, max_chars: usize) -> (String, bool)
Expand description

Truncates a UTF-8 string to at most max_chars codepoints.

Returns (truncated_string, was_truncated). If max_chars == 0 returns empty string. Unicode-safe: never splits mid-codepoint (slice at char_indices boundary).

Resource: single pass via char_indices().nth; fast-path when content.len() <= max_chars (each scalar is ≥ 1 byte, so byte length bounds char count from above).

Prefer [take_utf8_capped] on the exec path when you already own a Vec<u8> — that reuses the buffer instead of allocating a second copy.