Trait textwrap::WordSplitter[][src]

pub trait WordSplitter: Debug {
    fn split_points(&self, word: &str) -> Vec<usize>;
}

The WordSplitter trait describes where words can be split.

If the textwrap crate has been compiled with the hyphenation Cargo feature enabled, you will find an implementation of WordSplitter by the hyphenation::Standard struct. Use this struct for language-aware hyphenation:

#[cfg(feature = "hyphenation")]
{
    use hyphenation::{Language, Load, Standard};
    use textwrap::{wrap, Options};

    let text = "Oxidation is the loss of electrons.";
    let dictionary = Standard::from_embedded(Language::EnglishUS).unwrap();
    let options = Options::new(8).splitter(dictionary);
    assert_eq!(wrap(text, &options), vec!["Oxida-",
                                          "tion is",
                                          "the loss",
                                          "of elec-",
                                          "trons."]);
}

Please see the documentation for the hyphenation crate for more details.

Required methods

fn split_points(&self, word: &str) -> Vec<usize>[src]

Return all possible indices where word can be split.

The indices returned must be in range 0..word.len(). They should point to the index after the split point, i.e., after - if splitting on hyphens. This way, word.split_at(idx) will break the word into two well-formed pieces.

Examples

use textwrap::{HyphenSplitter, NoHyphenation, WordSplitter};
assert_eq!(NoHyphenation.split_points("cannot-be-split"), vec![]);
assert_eq!(HyphenSplitter.split_points("can-be-split"), vec![4, 7]);
Loading content...

Implementations on Foreign Types

impl<S: WordSplitter + ?Sized> WordSplitter for Box<S>[src]

impl<T: ?Sized + WordSplitter> WordSplitter for &T[src]

impl WordSplitter for Standard[src]

A hyphenation dictionary can be used to do language-specific hyphenation using patterns from the hyphenation crate.

Note: Only available when the hyphenation Cargo feature is enabled.

Loading content...

Implementors

impl WordSplitter for HyphenSplitter[src]

HyphenSplitter is the default WordSplitter used by Options::new. It will split words on any existing hyphens in the word.

It will only use hyphens that are surrounded by alphanumeric characters, which prevents a word like "--foo-bar" from being split into "--" and "foo-bar".

impl WordSplitter for NoHyphenation[src]

NoHyphenation implements WordSplitter by not splitting the word at all.

Loading content...