Crate textwrap[][src]

The textwrap library provides functions for word wrapping and indenting text.

Wrapping Text

Wrapping text can be very useful in command-line programs where you want to format dynamic output nicely so it looks good in a terminal. A quick example:

fn main() {
    let text = "textwrap: a small library for wrapping text.";
    println!("{}", textwrap::fill(text, 18));

When you run this program, it will display the following output:

textwrap: a small
library for
wrapping text.

If you enable the hyphenation Cargo feature, you can get automatic hyphenation for a number of languages:

use hyphenation::{Language, Load, Standard};
use textwrap::{fill, Options};

fn main() {
    let text = "textwrap: a small library for wrapping text.";
    let dictionary = Standard::from_embedded(Language::EnglishUS).unwrap();
    let options = Options::new(18).splitter(dictionary);
    println!("{}", fill(text, &options));

The program will now output:

textwrap: a small
library for wrap-
ping text.

See also the unfill and refill functions which allow you to manipulate already wrapped text.

Wrapping Strings at Compile Time

If your strings are known at compile time, please take a look at the procedural macros from the textwrap-macros crate.

Displayed Width vs Byte Size

To word wrap text, one must know the width of each word so one can know when to break lines. This library will by default measure the width of text using the displayed width, not the size in bytes. The unicode-width Cargo feature controls this.

This is important for non-ASCII text. ASCII characters such as a and ! are simple and take up one column each. This means that the displayed width is equal to the string length in bytes. However, non-ASCII characters and symbols take up more than one byte when UTF-8 encoded: é is 0xc3 0xa9 (two bytes) and is 0xe2 0x9a 0x99 (three bytes) in UTF-8, respectively.

This is why we take care to use the displayed width instead of the byte count when computing line lengths. All functions in this library handle Unicode characters like this when the unicode-width Cargo feature is enabled (it is enabled by default).

Indentation and Dedentation

The textwrap library also offers functions for adding a prefix to every line of a string and to remove leading whitespace. As an example, the indent function allows you to turn lines of text into a bullet list:

let before = "
let after = "
* foo
* bar
* baz
assert_eq!(textwrap::indent(before, "* "), after);

Removing leading whitespace is done with dedent:

let before = "
let after = "
assert_eq!(textwrap::dedent(before), after);

Cargo Features

The textwrap library can be slimmed down as needed via a number of Cargo features. This means you only pay for the features you actually use.

The full dependency graph, where dashed lines indicate optional dependencies, is shown below:

Default Features

These features are enabled by default:

  • unicode-width: enables correct width computation of non-ASCII characters via the unicode-width crate. Without this feature, every char is 1 column wide, except for emojis which are 2 columns wide. See the core::display_width function for details.

    This feature can be disabled if you only need to wrap ASCII text, or if the functions in core are used directly with core::Fragments for which the widths have been computed in other ways.

  • smawk: enables linear-time wrapping of the whole paragraph via the smawk crate. See the core::wrap_optimal_fit function for details on the optimal-fit algorithm.

    This feature can be disabled if you only ever intend to use core::wrap_first_fit.

Optional Features

These Cargo features enable new functionality:



Building blocks for advanced wrapping functionality.



Simple and default way to split words: splitting on existing hyphens only.


Use this as a Options.splitter to avoid any kind of hyphenation:


Holds settings for wrapping and filling text.



The WordSplitter trait describes where words can be split.



Removes common leading whitespace from each line.


Fill a line of text at a given width.


Fill text in-place without reallocating the input string.


Add prefix to each non-empty line.


Refill a paragraph of wrapped text with a new width.


Return the current terminal width. If the terminal width cannot be determined (typically because the standard output is not connected to a terminal), a default width of 80 characters will be used.


Unpack a paragraph of already-wrapped text.


Wrap a line of text at a given width.


Wrap text into columns with a given total width.