textwrap

Struct Options

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

source§

impl<'a> Options<'a>

source

pub const fn new(width: usize) -> Self

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.

source§

impl<'a> Options<'a>

source

pub fn initial_indent(self, indent: &'a str) -> Self

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."]);
source

pub fn subsequent_indent(self, indent: &'a str) -> Self

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."]);
source

pub fn break_words(self, setting: bool) -> Self

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."]);
source

pub fn word_separator(self, word_separator: WordSeparator) -> Options<'a>

Change self.word_separator.

See word_separators::WordSeparator for details on the choices.

source

pub fn wrap_algorithm(self, wrap_algorithm: WrapAlgorithm) -> Options<'a>

Change self.wrap_algorithm.

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

source

pub fn word_splitter(self, word_splitter: WordSplitter) -> Options<'a>

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§

source§

impl<'a> Clone for Options<'a>

source§

fn clone(&self) -> Options<'a>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<'a> Debug for Options<'a>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'a> From<&'a Options<'a>> for Options<'a>

source§

fn from(options: &'a Options<'a>) -> Self

Converts to this type from the input type.
source§

impl<'a> From<usize> for Options<'a>

source§

fn from(width: usize) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<'a> Freeze for Options<'a>

§

impl<'a> RefUnwindSafe for Options<'a>

§

impl<'a> Send for Options<'a>

§

impl<'a> Sync for Options<'a>

§

impl<'a> Unpin for Options<'a>

§

impl<'a> UnwindSafe for Options<'a>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> CloneToUninit for T
where T: Clone,

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

source§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

source§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.