Trait textwrap::WordSplitter
source · pub trait WordSplitter: Debug {
// Required method
fn split_points(&self, word: &str) -> Vec<usize>;
}
Expand description
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§
sourcefn split_points(&self, word: &str) -> Vec<usize>
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::{HyphenSplitter, NoHyphenation, WordSplitter};
assert_eq!(NoHyphenation.split_points("cannot-be-split"), vec![]);
assert_eq!(HyphenSplitter.split_points("can-be-split"), vec![4, 7]);
Implementations on Foreign Types§
source§impl<S: WordSplitter + ?Sized> WordSplitter for Box<S>
impl<S: WordSplitter + ?Sized> WordSplitter for Box<S>
source§impl<T: ?Sized + WordSplitter> WordSplitter for &T
impl<T: ?Sized + WordSplitter> WordSplitter for &T
Implementors§
impl WordSplitter for HyphenSplitter
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
NoHyphenation
implements WordSplitter
by not splitting the
word at all.