Function textwrap::core::wrap_first_fit[][src]

pub fn wrap_first_fit<T: Fragment, F: Fn(usize) -> usize>(
    fragments: &[T],
    line_widths: F
) -> Vec<&[T]>

Wrap abstract fragments into lines with a first-fit algorithm.

The line_widths map line numbers (starting from 0) to a target line width. This can be used to implement hanging indentation.

The fragments must already have been split into the desired widths, this function will not (and cannot) attempt to split them further when arranging them into lines.

First-Fit Algorithm

This implements a simple “greedy” algorithm: accumulate fragments one by one and when a fragment no longer fits, start a new line. There is no look-ahead, we simply take first fit of the fragments we find.

While fast and predictable, this algorithm can produce poor line breaks when a long fragment is moved to a new line, leaving behind a large gap:

use textwrap::core::{find_words, wrap_first_fit, Word};

// Helper to convert wrapped lines to a Vec<String>.
fn lines_to_strings(lines: Vec<&[Word<'_>]>) -> Vec<String> {
    lines.iter().map(|line| {
        line.iter().map(|word| &**word).collect::<Vec<_>>().join(" ")

let text = "These few words will unfortunately not wrap nicely.";
let words = find_words(text).collect::<Vec<_>>();
assert_eq!(lines_to_strings(wrap_first_fit(&words, |_| 15)),
           vec!["These few words",
                "will",  // <-- short line
                "not wrap",

// We can avoid the short line if we look ahead:
#[cfg(feature = "smawk")]
assert_eq!(lines_to_strings(textwrap::core::wrap_optimal_fit(&words, |_| 15)),
           vec!["These few",
                "words will",
                "not wrap",

The wrap_optimal_fit function was used above to get better line breaks. It uses an advanced algorithm which tries to avoid short lines. This function is about 4 times faster than wrap_optimal_fit.


Imagine you're building a house site and you have a number of tasks you need to execute. Things like pour foundation, complete framing, install plumbing, electric cabling, install insulation.

The construction workers can only work during daytime, so they need to pack up everything at night. Because they need to secure their tools and move machines back to the garage, this process takes much more time than the time it would take them to simply switch to another task.

You would like to make a list of tasks to execute every day based on your estimates. You can model this with a program like this:

use textwrap::core::{wrap_first_fit, Fragment};

struct Task<'a> {
    name: &'a str,
    hours: usize,   // Time needed to complete task.
    sweep: usize,   // Time needed for a quick sweep after task during the day.
    cleanup: usize, // Time needed for full cleanup if day ends with this task.

impl Fragment for Task<'_> {
    fn width(&self) -> usize { self.hours }
    fn whitespace_width(&self) -> usize { self.sweep }
    fn penalty_width(&self) -> usize { self.cleanup }

// The morning tasks
let tasks = vec![
    Task { name: "Foundation",  hours: 4, sweep: 2, cleanup: 3 },
    Task { name: "Framing",     hours: 3, sweep: 1, cleanup: 2 },
    Task { name: "Plumbing",    hours: 2, sweep: 2, cleanup: 2 },
    Task { name: "Electrical",  hours: 2, sweep: 1, cleanup: 2 },
    Task { name: "Insulation",  hours: 2, sweep: 1, cleanup: 2 },
    Task { name: "Drywall",     hours: 3, sweep: 1, cleanup: 2 },
    Task { name: "Floors",      hours: 3, sweep: 1, cleanup: 2 },
    Task { name: "Countertops", hours: 1, sweep: 1, cleanup: 2 },
    Task { name: "Bathrooms",   hours: 2, sweep: 1, cleanup: 2 },

// Fill tasks into days, taking `day_length` into account. The
// output shows the hours worked per day along with the names of
// the tasks for that day.
fn assign_days<'a>(tasks: &[Task<'a>], day_length: usize) -> Vec<(usize, Vec<&'a str>)> {
    let mut days = Vec::new();
    // Assign tasks to days. The assignment is a vector of slices,
    // with a slice per day.
    let assigned_days: Vec<&[Task<'a>]> = wrap_first_fit(&tasks, |i| day_length);
    for day in assigned_days.iter() {
        let last = day.last().unwrap();
        let work_hours: usize = day.iter().map(|t| t.hours + t.sweep).sum();
        let names = day.iter().map(|t|<Vec<_>>();
        days.push((work_hours - last.sweep + last.cleanup, names));

// With a single crew working 8 hours a day:
    assign_days(&tasks, 8),
        (7, vec!["Foundation"]),
        (8, vec!["Framing", "Plumbing"]),
        (7, vec!["Electrical", "Insulation"]),
        (5, vec!["Drywall"]),
        (7, vec!["Floors", "Countertops"]),
        (4, vec!["Bathrooms"]),

// With two crews working in shifts, 16 hours a day:
    assign_days(&tasks, 16),
        (14, vec!["Foundation", "Framing", "Plumbing"]),
        (15, vec!["Electrical", "Insulation", "Drywall", "Floors"]),
        (6, vec!["Countertops", "Bathrooms"]),

Apologies to anyone who actually knows how to build a house and knows how long each step takes :-)