Skip to content

Why whitespace is been removed after wrap? #572

Answered by mgeisler
linrongbin16 asked this question in Q&A
Discussion options

You must be logged in to vote

Good question! The whitespace at the ends of the lines is removed since it's normally unwanted.

The idea is that each line now takes up at most 10 columns. If the trailing whitespace was left there, then this would not be true.

It goes further than this: the whitespace between the words is attached to the left word, and it is removed at the end of lines. So running

pub fn main() {
    dbg!(textwrap::wrap("This   is   a    demo   ", 10));
}

gives me

[/app/example.rs:2:5] textwrap::wrap("This   is   a    demo   ", 10) = [
    "This   is",
    "a    demo",
]

See https://godbolt.org/z/Kn9GTPvoM for an example of this (the normal Playground gives me an error right now).

What happens is that t…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@linrongbin16
Comment options

Answer selected by linrongbin16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants