Struct textwrap::Wrapper [] [src]

pub struct Wrapper<'a> {
    pub width: usize,
    pub corpus: Option<&'a Corpus>,
}

A Wrapper holds settings for wrapping and filling text.

The algorithm used by the wrap method works by doing a single scan over words in the input string and splitting them into one or more lines. The time and memory complexity is O(n) where n is the length of the input string.

Fields

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

The hyphenation corpus (if any) used for automatic hyphenation.

Methods

impl<'a> Wrapper<'a>
[src]

Create a new Wrapper for wrapping at the specified width.

Fill a line of text at self.width characters. Strings are wrapped based on their displayed width, not their size in bytes.

The result is a string with newlines between each line. Use the wrap method if you need access to the individual lines.

use textwrap::Wrapper;

let wrapper = Wrapper::new(15);
assert_eq!(wrapper.fill("Memory safety without garbage collection."),
           "Memory safety\nwithout garbage\ncollection.");

This method simply joins the lines produced by wrap. As such, it inherits the O(n) time and memory complexity where n is the input string length.

Wrap a line of text at self.width characters. Strings are wrapped based on their displayed width, not their size in bytes.

use textwrap::Wrapper;

let wrap15 = Wrapper::new(15);
assert_eq!(wrap15.wrap("Concurrency without data races."),
           vec!["Concurrency",
                "without data",
                "races."]);

let wrap20 = Wrapper::new(20);
assert_eq!(wrap20.wrap("Concurrency without data races."),
           vec!["Concurrency without",
                "data races."]);

This method does a single scan over the input string, it has an O(n) time and memory complexity where n is the input string length.