pub enum WordSeparator {
    AsciiSpace,
    UnicodeBreakProperties,
    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!")]);
§

UnicodeBreakProperties

Split line into words using Unicode break properties.

This word separator uses the Unicode line breaking algorithm described in Unicode Standard Annex #14 to find legal places to break lines. There is a small difference in that the U+002D (Hyphen-Minus) and U+00AD (Soft Hyphen) don’t create a line break: to allow a line break at a hyphen, use WordSplitter::HyphenSplitter. Soft hyphens are not currently supported.

§Examples

Unlike WordSeparator::AsciiSpace, the Unicode line breaking algorithm will find line break opportunities between some characters with no intervening whitespace:

#[cfg(feature = "unicode-linebreak")] {
use textwrap::core::Word;
use textwrap::WordSeparator::UnicodeBreakProperties;

assert_eq!(UnicodeBreakProperties.find_words("Emojis: 😂😍").collect::<Vec<_>>(),
           vec![Word::from("Emojis: "),
                Word::from("😂"),
                Word::from("😍")]);

assert_eq!(UnicodeBreakProperties.find_words("CJK: 你好").collect::<Vec<_>>(),
           vec![Word::from("CJK: "),
                Word::from("你"),
                Word::from("好")]);
}

A U+2060 (Word Joiner) character can be inserted if you want to manually override the defaults and keep the characters together:

#[cfg(feature = "unicode-linebreak")] {
use textwrap::core::Word;
use textwrap::WordSeparator::UnicodeBreakProperties;

assert_eq!(UnicodeBreakProperties.find_words("Emojis: 😂\u{2060}😍").collect::<Vec<_>>(),
           vec![Word::from("Emojis: "),
                Word::from("😂\u{2060}😍")]);
}

The Unicode line breaking algorithm will also automatically suppress break breaks around certain punctuation characters::

#[cfg(feature = "unicode-linebreak")] {
use textwrap::core::Word;
use textwrap::WordSeparator::UnicodeBreakProperties;

assert_eq!(UnicodeBreakProperties.find_words("[ foo ] bar !").collect::<Vec<_>>(),
           vec![Word::from("[ foo ] "),
                Word::from("bar !")]);
}
§

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

Find words using a custom word separator

Implementations§

source§

impl WordSeparator

source

pub const fn new() -> Self

Create a new word separator.

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

source

pub fn find_words<'a>( &self, line: &'a str ) -> Box<dyn Iterator<Item = Word<'a>> + 'a>

Find all words in line.

Trait Implementations§

source§

impl Clone for WordSeparator

source§

fn clone(&self) -> WordSeparator

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for WordSeparator

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl PartialEq for WordSeparator

source§

fn eq(&self, other: &Self) -> bool

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));
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Copy for WordSeparator

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

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

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.