Struct textwrap::Options

source ·
#[non_exhaustive]
pub struct Options<'a> { pub width: usize, pub line_ending: LineEnding, 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 (Non-exhaustive)§

This struct is marked as non-exhaustive
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.
§width: usize

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

§line_ending: LineEnding

Line ending used for breaking lines.

§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.

The other fields are given default values as follows:

let options = Options::new(width);
assert_eq!(options.line_ending, LineEnding::LF);
assert_eq!(options.initial_indent, "");
assert_eq!(options.subsequent_indent, "");
assert_eq!(options.break_words, true);

#[cfg(feature = "unicode-linebreak")]
assert_eq!(options.word_separator, WordSeparator::UnicodeBreakProperties);
#[cfg(not(feature = "unicode-linebreak"))]
assert_eq!(options.word_separator, WordSeparator::AsciiSpace);

#[cfg(feature = "smawk")]
assert_eq!(options.wrap_algorithm, WrapAlgorithm::new_optimal_fit());
#[cfg(not(feature = "smawk"))]
assert_eq!(options.wrap_algorithm, WrapAlgorithm::FirstFit);

assert_eq!(options.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.line_ending. This specifies which of the supported line endings should be used to break the lines of the input text.

Examples
use textwrap::{refill, LineEnding, Options};

let options = Options::new(15).line_ending(LineEnding::CRLF);
assert_eq!(refill("This is a little example.", options),
           "This is a\r\nlittle example.");

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.

See Options::word_splitter instead if you want to control hyphenation.

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.

See Options::break_words instead if you want to control the handling of words longer than the line width.

Examples
use textwrap::{wrap, Options, WordSplitter};

// The default is WordSplitter::HyphenSplitter.
let options = Options::new(5);
assert_eq!(wrap("foo-bar-baz", &options),
           vec!["foo-", "bar-", "baz"]);

// The word is now so long that break_words kick in:
let options = Options::new(5)
    .word_splitter(WordSplitter::NoHyphenation);
assert_eq!(wrap("foo-bar-baz", &options),
           vec!["foo-b", "ar-ba", "z"]);

// If you want to breaks at all, disable both:
let options = Options::new(5)
    .break_words(false)
    .word_splitter(WordSplitter::NoHyphenation);
assert_eq!(wrap("foo-bar-baz", &options),
           vec!["foo-bar-baz"]);

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.