Function textwrap::wrap_iter[][src]

pub fn wrap_iter(s: &str, width: usize) -> IntoWrapIter<'_, HyphenSplitter>
Notable traits for IntoWrapIter<'a, S>
impl<'a, S: WordSplitter> Iterator for IntoWrapIter<'a, S> type Item = Cow<'a, str>;
Expand description

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

This function creates a Wrapper on the fly with default settings. It then calls the into_wrap_iter method. Hence, the return value is an IntoWrapIter, not a WrapIter as the function name would otherwise suggest.

If you need to set a language corpus for automatic hyphenation, or need to wrap many strings, then it is suggested to create a Wrapper and call its wrap_iter or into_wrap_iter methods.

Examples

use std::borrow::Cow;
use textwrap::wrap_iter;

let mut wrap20_iter = wrap_iter("Zero-cost abstractions.", 20);
assert_eq!(wrap20_iter.next(), Some(Cow::from("Zero-cost")));
assert_eq!(wrap20_iter.next(), Some(Cow::from("abstractions.")));
assert_eq!(wrap20_iter.next(), None);

let mut wrap25_iter = wrap_iter("Zero-cost abstractions.", 25);
assert_eq!(wrap25_iter.next(), Some(Cow::from("Zero-cost abstractions.")));
assert_eq!(wrap25_iter.next(), None);