pub trait SplitParagraphs {
// Required method
fn paragraphs(&self) -> Paragraphs<'_> ⓘ;
}
Expand description
Trait extending str
with paragraphs
.
Required Methods§
Sourcefn paragraphs(&self) -> Paragraphs<'_> ⓘ
fn paragraphs(&self) -> Paragraphs<'_> ⓘ
Returns an iterator over paragraphs of a string, as string slices.
A paragraph consists of one or more lines containing non-whitespace characters, separated by empty lines or lines containing only whitespace.
Paragraphs always contain at least one line with at least one non-whitespace character.
Paragraphs never contain empty lines or whitespace-only lines.
Paragraphs support line endings that are either newlines (\n
) or
carriage return followed by line feed (\r\n
).
Line terminators between paragraphs are not included in the returned slices.
Line terminators within paragraphs are preserved in their original form.
Handling of line endings matches lines
. See its documentation for more details.
§Examples
Basic usage:
let text = "foo\r\nbar\n\nbaz\r";
let mut paragraphs = text.paragraphs();
assert_eq!(Some("foo\r\nbar"), paragraphs.next());
// Trailing carriage return is included in the last paragraph
assert_eq!(Some("baz\r"), paragraphs.next());
assert_eq!(None, paragraphs.next());
The final paragraph does not require any ending:
let text = "\n\n\nfoo\nbar\n\r\nbaz";
let mut paragraphs = text.paragraphs();
assert_eq!(Some("foo\nbar"), paragraphs.next());
assert_eq!(Some("baz"), paragraphs.next());
assert_eq!(None, paragraphs.next());