Skip to main content

truncate_within

Function truncate_within 

Source
pub fn truncate_within(text: &str, max_len: usize, ellipsis: &str) -> String
Expand description

Truncate text to max_len chars, reserving room for ellipsis so the returned string never exceeds max_len chars.

This differs from truncate_text, which appends the ellipsis after taking max_len chars (yielding up to max_len + ellipsis.len() chars). Use this when the total rendered width must stay within a hard budget.

assert_eq!(truncate_within("hello world", 8, "..."), "hello...");
assert_eq!(truncate_within("hi", 8, "..."), "hi");
assert_eq!(truncate_within("hello", 3, "…"), "he…");