Skip to main content

truncate_to_chars

Function truncate_to_chars 

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

Truncate text to at most max_chars characters (not bytes), appending TRUNCATION_MARKER when anything was cut. Returns (text, was_truncated).

Counts and slices by Unicode scalar values, so it never panics on a multi-byte boundary the way &text[..n] would. Note: when applied to rendered Markdown this may cut through markup (mid-link, mid-emphasis) — accepted as the simple, predictable behavior; callers wanting intact markup should fetch the item without a cap.