excerpt
Derives a short, readable excerpt from a longer text — for a card, header or preview where a full paragraph does not fit.
Unlike truncate (a mechanical cut at exactly max_chars), this prefers to cut at the end
of a whole sentence (., ! or ?) when one fits within max_chars, even if that leaves
the result shorter than the limit. Only when no sentence fits does it fall back to the last
whole word before the limit — it never returns partial-word text.
Internal whitespace (line breaks, repeated spaces) is collapsed to single spaces first (see
squish).
Known limitation: the sentence-boundary check is a simple heuristic (punctuation followed by
whitespace or the end of the text) — it does not special-case abbreviations ("Mr.") or
decimal numbers ("3.14"), which can be misread as a sentence end.
Import
Section titled “Import”Cargo feature string (enabled by default). To compile only this module:
or in Cargo.toml:
Signature
Section titled “Signature”Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
text | &str | The text to excerpt. |
max_chars | usize | The maximum length of the result, ellipsis included. |
ellipsis | &str | Appended only when falling back to a word-boundary cut (a sentence-boundary cut never needs one). |
Returns
Section titled “Returns”String — The original text, squished, unchanged if already within max_chars; otherwise a shortened
version.
