[−][src]Trait textwrap::word_splitters::WordSplitter
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).word_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>
[−]
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::word_splitters::{HyphenSplitter, NoHyphenation, WordSplitter}; assert_eq!(NoHyphenation.split_points("cannot-be-split"), vec![]); assert_eq!(HyphenSplitter.split_points("can-be-split"), vec![4, 7]);