44 lines
7.9 KiB
HTML
44 lines
7.9 KiB
HTML
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="Fill `text` in-place without reallocating the input string."><meta name="keywords" content="rust, rustlang, rust-lang, fill_inplace"><title>fill_inplace in textwrap - Rust</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceCodePro-Semibold.ttf.woff2"><link rel="stylesheet" type="text/css" href="../normalize.css"><link rel="stylesheet" type="text/css" href="../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" type="text/css" href="../ayu.css" disabled><link rel="stylesheet" type="text/css" href="../dark.css" disabled><link rel="stylesheet" type="text/css" href="../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../storage.js"></script><script src="../crates.js"></script><script defer src="../main.js"></script>
|
||
<noscript><link rel="stylesheet" href="../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../favicon.svg"></head><body class="rustdoc fn"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="sidebar"><div class="sidebar-menu" role="button">☰</div><a class="sidebar-logo" href="../textwrap/index.html"><div class="logo-container"><img class="rust-logo" src="../rust-logo.png" alt="logo"></div>
|
||
</a><div class="sidebar-elems"><h2 class="location">Other items in<br><a href="index.html">textwrap</a></h2><div id="sidebar-vars" data-name="fill_inplace" data-ty="fn" data-relpath=""></div><script defer src="sidebar-items.js"></script></div></nav><main><div class="width-limiter"><div class="sub-container"><a class="sub-logo-container" href="../textwrap/index.html"><img class="rust-logo" src="../rust-logo.png" alt="logo"></a><nav class="sub"><div class="theme-picker"><button id="theme-picker" aria-label="Pick another theme!" aria-haspopup="menu" title="themes"><img width="18" height="18" alt="Pick another theme!" src="../brush.svg"></button><div id="theme-choices" role="menu"></div></div><form class="search-form"><div class="search-container"><div><select id="crate-search"><option value="All crates">All crates</option></select><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"></div><button type="button" id="help-button" title="help">?</button><a id="settings-menu" href="../settings.html" title="settings"><img width="18" height="18" alt="Change settings" src="../wheel.svg"></a></div></form></nav></div><section id="main-content" class="content"><h1 class="fqn"><span class="in-band">Function <a href="index.html">textwrap</a>::<wbr><a class="fn" href="#">fill_inplace</a><button id="copy-path" onclick="copy_path(this)" title="Copy item path to clipboard"><img src="../clipboard.svg" width="19" height="18" alt="Copy item path"></button></span><span class="out-of-band"><span id="render-detail"><a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class="inner">−</span>]</a></span><a class="srclink" href="../src/textwrap/lib.rs.html#1152-1185" title="goto source code">[src]</a></span></h1><div class="docblock item-decl"><pre class="rust fn"><code>pub fn fill_inplace(text: &mut <a class="struct" href="https://doc.rust-lang.org/1.59.0/alloc/string/struct.String.html" title="struct alloc::string::String">String</a>, width: <a class="primitive" href="https://doc.rust-lang.org/1.59.0/std/primitive.usize.html">usize</a>)</code></pre></div><details class="rustdoc-toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Fill <code>text</code> in-place without reallocating the input string.</p>
|
||
<p>This function works by modifying the input string: some <code>' '</code>
|
||
characters will be replaced by <code>'\n'</code> characters. The rest of the
|
||
text remains untouched.</p>
|
||
<p>Since we can only replace existing whitespace in the input with
|
||
<code>'\n'</code>, we cannot do hyphenation nor can we split words longer
|
||
than the line width. We also need to use <code>AsciiSpace</code> as the word
|
||
separator since we need <code>' '</code> characters between words in order to
|
||
replace some of them with a <code>'\n'</code>. Indentation is also ruled out.
|
||
In other words, <code>fill_inplace(width)</code> behaves as if you had called
|
||
<a href="fn.fill.html" title="fill"><code>fill</code></a> with these options:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="ident">Options</span> {
|
||
<span class="ident">width</span>: <span class="ident">width</span>,
|
||
<span class="ident">initial_indent</span>: <span class="string">""</span>,
|
||
<span class="ident">subsequent_indent</span>: <span class="string">""</span>,
|
||
<span class="ident">break_words</span>: <span class="bool-val">false</span>,
|
||
<span class="ident">word_separator</span>: <span class="ident">WordSeparator::AsciiSpace</span>,
|
||
<span class="ident">wrap_algorithm</span>: <span class="ident">WrapAlgorithm::FirstFit</span>,
|
||
<span class="ident">word_splitter</span>: <span class="ident">WordSplitter::NoHyphenation</span>,
|
||
};</code></pre></div>
|
||
<p>The wrap algorithm is <a href="wrap_algorithms/enum.WrapAlgorithm.html#variant.FirstFit" title="WrapAlgorithm::FirstFit"><code>WrapAlgorithm::FirstFit</code></a> since this
|
||
is the fastest algorithm — and the main reason to use
|
||
<code>fill_inplace</code> is to get the string broken into newlines as fast
|
||
as possible.</p>
|
||
<p>A last difference is that (unlike <a href="fn.fill.html" title="fill"><code>fill</code></a>) <code>fill_inplace</code> can
|
||
leave trailing whitespace on lines. This is because we wrap by
|
||
inserting a <code>'\n'</code> at the final whitespace in the input string:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">text</span> <span class="op">=</span> <span class="ident">String::from</span>(<span class="string">"Hello World!"</span>);
|
||
<span class="ident">textwrap::fill_inplace</span>(<span class="kw-2">&mut</span> <span class="ident">text</span>, <span class="number">10</span>);
|
||
<span class="macro">assert_eq!</span>(<span class="ident">text</span>, <span class="string">"Hello \nWorld!"</span>);</code></pre></div>
|
||
<p>If we didn’t do this, the word <code>World!</code> would end up being
|
||
indented. You can avoid this if you make sure that your input text
|
||
has no double spaces.</p>
|
||
<h2 id="performance" class="section-header"><a href="#performance">Performance</a></h2>
|
||
<p>In benchmarks, <code>fill_inplace</code> is about twice as fast as <a href="fn.fill.html" title="fill"><code>fill</code></a>.
|
||
Please see the <a href="https://github.com/mgeisler/textwrap/blob/master/benches/linear.rs"><code>linear</code>
|
||
benchmark</a>
|
||
for details.</p>
|
||
</div></details></section><section id="search" class="content hidden"></section></div></main><div id="rustdoc-vars" data-root-path="../" data-current-crate="textwrap" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.59.0 (9d1b2106e 2022-02-23)" ></div>
|
||
</body></html> |