pub struct Options<'a> {
    pub width: usize,
    pub initial_indent: &'a str,
    pub subsequent_indent: &'a str,
    pub break_words: bool,
    pub wrap_algorithm: WrapAlgorithm,
    pub word_separator: WordSeparator,
    pub word_splitter: WordSplitter,
}
Expand description

Holds configuration options for wrapping and filling text.

Fields

width: usize

The width in columns at which the text will be wrapped.

initial_indent: &'a str

Indentation used for the first line of output. See the Options::initial_indent method.

subsequent_indent: &'a str

Indentation used for subsequent lines of output. See the Options::subsequent_indent method.

break_words: bool

Allow long words to be broken if they cannot fit on a line. When set to false, some lines may be longer than self.width. See the Options::break_words method.

wrap_algorithm: WrapAlgorithm

Wrapping algorithm to use, see the implementations of the wrap_algorithms::WrapAlgorithm trait for details.

word_separator: WordSeparator

The line breaking algorithm to use, see word_separators::WordSeparator trait for an overview and possible implementations.

word_splitter: WordSplitter

The method for splitting words. This can be used to prohibit splitting words on hyphens, or it can be used to implement language-aware machine hyphenation.

Implementations

Creates a new Options with the specified width. Equivalent to

Options {
    width: width,
    initial_indent: "",
    subsequent_indent: "",
    break_words: true,
    #[cfg(feature = "unicode-linebreak")]
    word_separator: WordSeparator::UnicodeBreakProperties,
    #[cfg(not(feature = "unicode-linebreak"))]
    word_separator: WordSeparator::AsciiSpace,
    #[cfg(feature = "smawk")]
    wrap_algorithm: WrapAlgorithm::new_optimal_fit(),
    #[cfg(not(feature = "smawk"))]
    wrap_algorithm: WrapAlgorithm::FirstFit,
    word_splitter: WordSplitter::HyphenSplitter,
}

Note that the default word separator and wrap algorithms changes based on the available Cargo features. The best available algorithms are used by default.

Change self.initial_indent. The initial indentation is used on the very first line of output.

Examples

Classic paragraph indentation can be achieved by specifying an initial indentation and wrapping each paragraph by itself:

use textwrap::{wrap, Options};

let options = Options::new(16).initial_indent("    ");
assert_eq!(wrap("This is a little example.", options),
           vec!["    This is a",
                "little example."]);

Change self.subsequent_indent. The subsequent indentation is used on lines following the first line of output.

Examples

Combining initial and subsequent indentation lets you format a single paragraph as a bullet list:

use textwrap::{wrap, Options};

let options = Options::new(12)
    .initial_indent("* ")
    .subsequent_indent("  ");
#[cfg(feature = "smawk")]
assert_eq!(wrap("This is a little example.", options),
           vec!["* This is",
                "  a little",
                "  example."]);

// Without the `smawk` feature, the wrapping is a little different:
#[cfg(not(feature = "smawk"))]
assert_eq!(wrap("This is a little example.", options),
           vec!["* This is a",
                "  little",
                "  example."]);

Change self.break_words. This controls if words longer than self.width can be broken, or if they will be left sticking out into the right margin.

Examples
use textwrap::{wrap, Options};

let options = Options::new(4).break_words(true);
assert_eq!(wrap("This is a little example.", options),
           vec!["This",
                "is a",
                "litt",
                "le",
                "exam",
                "ple."]);

Change self.word_separator.

See word_separators::WordSeparator for details on the choices.

Change self.wrap_algorithm.

See the wrap_algorithms::WrapAlgorithm trait for details on the choices.

Change self.word_splitter. The word_splitters::WordSplitter is used to fit part of a word into the current line when wrapping text.

Examples
use textwrap::{Options, WordSplitter};
let opt = Options::new(80);
assert_eq!(opt.word_splitter, WordSplitter::HyphenSplitter);
let opt = opt.word_splitter(WordSplitter::NoHyphenation);
assert_eq!(opt.word_splitter, WordSplitter::NoHyphenation);

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
Converts to this type from the input type.
Converts to this type from the input type.

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.