Skip to main content

truncate_with_ellipsis

Function truncate_with_ellipsis 

Source
pub(crate) fn truncate_with_ellipsis(s: &str, max_bytes: usize) -> Cow<'_, str>
Expand description

truncate_at_char_boundary, with an marking what was cut.

Borrows when nothing was removed, so the common case of a string that already fits allocates nothing. The ellipsis is appended only on an actual truncation — a caller can therefore tell “this is the whole value” from “there was more” by looking at the output, which is the point of printing it.

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_with_ellipsis:

truncate_with_ellipsis("hello", 10)  ==  "hello"
truncate_with_ellipsis("hello", 3)  ==  "hel…"