pub enum WordSeparator {
    AsciiSpace,
    Custom(fn(line: &str) -> Box<dyn Iterator<Item = Word<'_>> + '_>),
}
Expand description

Describes where words occur in a line of text.

The simplest approach is say that words are separated by one or more ASCII spaces (' '). This works for Western languages without emojis. A more complex approach is to use the Unicode line breaking algorithm, which finds break points in non-ASCII text.

The line breaks occur between words, please see WordSplitter for options of how to handle hyphenation of individual words.

Examples

use textwrap::core::Word;
use textwrap::WordSeparator::AsciiSpace;

let words = AsciiSpace.find_words("Hello World!").collect::<Vec<_>>();
assert_eq!(words, vec![Word::from("Hello "), Word::from("World!")]);

Variants§

§

AsciiSpace

Find words by splitting on runs of ' ' characters.

Examples

use textwrap::core::Word;
use textwrap::WordSeparator::AsciiSpace;

let words = AsciiSpace.find_words("Hello   World!").collect::<Vec<_>>();
assert_eq!(words, vec![Word::from("Hello   "),
                       Word::from("World!")]);
§

Custom(fn(line: &str) -> Box<dyn Iterator<Item = Word<'_>> + '_>)

Find words using a custom word separator

Implementations§

Create a new word separator.

The best available algorithm is used by default, i.e., [WordSeparator::UnicodeBreakProperties] if available, otherwise WordSeparator::AsciiSpace.

Find all words in line.

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more

Compare two word separators.

use textwrap::WordSeparator;

assert_eq!(WordSeparator::AsciiSpace, WordSeparator::AsciiSpace);
#[cfg(feature = "unicode-linebreak")] {
    assert_eq!(WordSeparator::UnicodeBreakProperties,
               WordSeparator::UnicodeBreakProperties);
}

Note that WordSeparator::Custom values never compare equal:

use textwrap::WordSeparator;
use textwrap::core::Word;
fn word_separator(line: &str) -> Box<dyn Iterator<Item = Word<'_>> + '_> {
    Box::new(line.split_inclusive(' ').map(Word::from))
}
assert_ne!(WordSeparator::Custom(word_separator),
           WordSeparator::Custom(word_separator));
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.