Sophie

Sophie

distrib > Mageia > 6 > armv7hl > media > core-updates > by-pkgid > 4e2dbb669434a7691662cb2f0ad38972 > files > 13394

rust-doc-1.28.0-1.mga6.armv7hl.rpm

<!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="API documentation for the Rust `Write` trait in crate `std`."><meta name="keywords" content="rust, rustlang, rust-lang, Write"><title>std::io::Write - Rust</title><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="../../dark.css"><link rel="stylesheet" type="text/css" href="../../light.css" id="themeStyle"><script src="../../storage.js"></script><link rel="shortcut icon" href="https://doc.rust-lang.org/favicon.ico"></head><body class="rustdoc trait"><!--[if lte IE 8]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="sidebar"><div class="sidebar-menu">&#9776;</div><a href='../../std/index.html'><img src='https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png' alt='logo' width='100'></a><p class='location'>Trait Write</p><div class="sidebar-elems"><div class="block items"><a class="sidebar-title" href="#required-methods">Required Methods</a><div class="sidebar-links"><a href="#tymethod.write">write</a><a href="#tymethod.flush">flush</a></div><a class="sidebar-title" href="#provided-methods">Provided Methods</a><div class="sidebar-links"><a href="#method.write_all">write_all</a><a href="#method.write_fmt">write_fmt</a><a href="#method.by_ref">by_ref</a></div><a class="sidebar-title" href="#implementors">Implementors</a></div><p class='location'><a href='../index.html'>std</a>::<wbr><a href='index.html'>io</a></p><script>window.sidebarCurrent = {name: 'Write', ty: 'trait', relpath: ''};</script><script defer src="sidebar-items.js"></script></div></nav><div class="theme-picker"><button id="theme-picker" aria-label="Pick another theme!"><img src="../../brush.svg" width="18" alt="Pick another theme!"></button><div id="theme-choices"></div></div><script src="../../theme.js"></script><nav class="sub"><form class="search-form js-only"><div class="search-container"><input class="search-input" name="search" autocomplete="off" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><a id="settings-menu" href="../../settings.html"><img src="../../wheel.svg" width="18" alt="Change settings"></a></div></form></nav><section id="main" class="content"><h1 class='fqn'><span class='in-band'>Trait <a href='../index.html'>std</a>::<wbr><a href='index.html'>io</a>::<wbr><a class="trait" href=''>Write</a></span><span class='out-of-band'><span class='since' title='Stable since Rust version 1.0.0'>1.0.0</span><span id='render-detail'><a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class='inner'>&#x2212;</span>]</a></span><a class='srclink' href='../../src/std/io/mod.rs.html#1002-1218' title='goto source code'>[src]</a></span></h1><div class="docblock type-decl"><pre class='rust trait'>pub trait Write {
    fn <a href='#tymethod.write' class='fnname'>write</a>(&amp;mut self, buf: <a class="primitive" href="../primitive.slice.html">&amp;[</a><a class="primitive" href="../primitive.u8.html">u8</a><a class="primitive" href="../primitive.slice.html">]</a>) -&gt; <a class="type" href="../../std/io/type.Result.html" title="type std::io::Result">Result</a>&lt;<a class="primitive" href="../primitive.usize.html">usize</a>&gt;;
<div class='item-spacer'></div>    fn <a href='#tymethod.flush' class='fnname'>flush</a>(&amp;mut self) -&gt; <a class="type" href="../../std/io/type.Result.html" title="type std::io::Result">Result</a>&lt;<a class="primitive" href="../primitive.unit.html">()</a>&gt;;

    fn <a href='#method.write_all' class='fnname'>write_all</a>(&amp;mut self, buf: <a class="primitive" href="../primitive.slice.html">&amp;[</a><a class="primitive" href="../primitive.u8.html">u8</a><a class="primitive" href="../primitive.slice.html">]</a>) -&gt; <a class="type" href="../../std/io/type.Result.html" title="type std::io::Result">Result</a>&lt;<a class="primitive" href="../primitive.unit.html">()</a>&gt; { ... }
<div class='item-spacer'></div>    fn <a href='#method.write_fmt' class='fnname'>write_fmt</a>(&amp;mut self, fmt: <a class="struct" href="../../std/fmt/struct.Arguments.html" title="struct std::fmt::Arguments">Arguments</a>) -&gt; <a class="type" href="../../std/io/type.Result.html" title="type std::io::Result">Result</a>&lt;<a class="primitive" href="../primitive.unit.html">()</a>&gt; { ... }
<div class='item-spacer'></div>    fn <a href='#method.by_ref' class='fnname'>by_ref</a>(&amp;mut self) -&gt; <a class="primitive" href="../primitive.reference.html">&amp;mut </a>Self<br>&nbsp;&nbsp;&nbsp; <span class="where">where<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Self: <a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a></span>,
    { ... }
}</pre></div><div class='docblock'><p>A trait for objects which are byte-oriented sinks.</p>
<p>Implementors of the <code>Write</code> trait are sometimes called 'writers'.</p>
<p>Writers are defined by two required methods, <a href="#tymethod.write"><code>write</code></a> and <a href="#tymethod.flush"><code>flush</code></a>:</p>
<ul>
<li>
<p>The <a href="#tymethod.write"><code>write</code></a> method will attempt to write some data into the object,
returning how many bytes were successfully written.</p>
</li>
<li>
<p>The <a href="#tymethod.flush"><code>flush</code></a> method is useful for adaptors and explicit buffers
themselves for ensuring that all buffered data has been pushed out to the
'true sink'.</p>
</li>
</ul>
<p>Writers are intended to be composable with one another. Many implementors
throughout <a href="index.html"><code>std::io</code></a> take and provide types which implement the <code>Write</code>
trait.</p>
<h1 id="examples" class="section-header"><a href="#examples">Examples</a></h1>
<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">io</span>::<span class="ident">prelude</span>::<span class="kw-2">*</span>;
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">fs</span>::<span class="ident">File</span>;

<span class="kw">fn</span> <span class="ident">main</span>() <span class="op">-&gt;</span> <span class="ident">std</span>::<span class="ident">io</span>::<span class="prelude-ty">Result</span><span class="op">&lt;</span>()<span class="op">&gt;</span> {
    <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">buffer</span> <span class="op">=</span> <span class="ident">File</span>::<span class="ident">create</span>(<span class="string">&quot;foo.txt&quot;</span>)<span class="question-mark">?</span>;

    <span class="ident">buffer</span>.<span class="ident">write</span>(<span class="string">b&quot;some bytes&quot;</span>)<span class="question-mark">?</span>;
    <span class="prelude-val">Ok</span>(())
}<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Ause%20std%3A%3Aio%3A%3Aprelude%3A%3A*%3B%0Ause%20std%3A%3Afs%3A%3AFile%3B%0A%0Afn%20main()%20-%3E%20std%3A%3Aio%3A%3AResult%3C()%3E%20%7B%0A%20%20%20%20let%20mut%20buffer%20%3D%20File%3A%3Acreate(%22foo.txt%22)%3F%3B%0A%0A%20%20%20%20buffer.write(b%22some%20bytes%22)%3F%3B%0A%20%20%20%20Ok(())%0A%7D">Run</a></pre>
</div>
            <h2 id='required-methods' class='small-section-header'>
              Required Methods<a href='#required-methods' class='anchor'></a>
            </h2>
            <div class='methods'>
        <h3 id='tymethod.write' class='method'><span id='write.v' class='invisible'><code>fn <a href='#tymethod.write' class='fnname'>write</a>(&amp;mut self, buf: <a class="primitive" href="../primitive.slice.html">&amp;[</a><a class="primitive" href="../primitive.u8.html">u8</a><a class="primitive" href="../primitive.slice.html">]</a>) -&gt; <a class="type" href="../../std/io/type.Result.html" title="type std::io::Result">Result</a>&lt;<a class="primitive" href="../primitive.usize.html">usize</a>&gt;</code></span></h3><div class='docblock'><p>Write a buffer into this object, returning how many bytes were written.</p>
<p>This function will attempt to write the entire contents of <code>buf</code>, but
the entire write may not succeed, or the write may also generate an
error. A call to <code>write</code> represents <em>at most one</em> attempt to write to
any wrapped object.</p>
<p>Calls to <code>write</code> are not guaranteed to block waiting for data to be
written, and a write which would otherwise block can be indicated through
an <a href="../../std/result/enum.Result.html#variant.Err"><code>Err</code></a> variant.</p>
<p>If the return value is <a href="../../std/result/enum.Result.html#variant.Ok"><code>Ok(n)</code></a> then it must be guaranteed that
<code>0 &lt;= n &lt;= buf.len()</code>. A return value of <code>0</code> typically means that the
underlying object is no longer able to accept bytes and will likely not
be able to in the future as well, or that the buffer provided is empty.</p>
<h1 id="errors" class="section-header"><a href="#errors">Errors</a></h1>
<p>Each call to <code>write</code> may generate an I/O error indicating that the
operation could not be completed. If an error is returned then no bytes
in the buffer were written to this writer.</p>
<p>It is <strong>not</strong> considered an error if the entire buffer could not be
written to this writer.</p>
<p>An error of the <a href="../../std/io/enum.ErrorKind.html#variant.Interrupted"><code>ErrorKind::Interrupted</code></a> kind is non-fatal and the
write operation should be retried if there is nothing else to do.</p>
<h1 id="examples-1" class="section-header"><a href="#examples-1">Examples</a></h1>
<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">io</span>::<span class="ident">prelude</span>::<span class="kw-2">*</span>;
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">fs</span>::<span class="ident">File</span>;

<span class="kw">fn</span> <span class="ident">main</span>() <span class="op">-&gt;</span> <span class="ident">std</span>::<span class="ident">io</span>::<span class="prelude-ty">Result</span><span class="op">&lt;</span>()<span class="op">&gt;</span> {
    <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">buffer</span> <span class="op">=</span> <span class="ident">File</span>::<span class="ident">create</span>(<span class="string">&quot;foo.txt&quot;</span>)<span class="question-mark">?</span>;

    <span class="comment">// Writes some prefix of the byte string, not necessarily all of it.</span>
    <span class="ident">buffer</span>.<span class="ident">write</span>(<span class="string">b&quot;some bytes&quot;</span>)<span class="question-mark">?</span>;
    <span class="prelude-val">Ok</span>(())
}<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Ause%20std%3A%3Aio%3A%3Aprelude%3A%3A*%3B%0Ause%20std%3A%3Afs%3A%3AFile%3B%0A%0Afn%20main()%20-%3E%20std%3A%3Aio%3A%3AResult%3C()%3E%20%7B%0A%20%20%20%20let%20mut%20buffer%20%3D%20File%3A%3Acreate(%22foo.txt%22)%3F%3B%0A%0A%20%20%20%20%2F%2F%20Writes%20some%20prefix%20of%20the%20byte%20string%2C%20not%20necessarily%20all%20of%20it.%0A%20%20%20%20buffer.write(b%22some%20bytes%22)%3F%3B%0A%20%20%20%20Ok(())%0A%7D">Run</a></pre>
</div><h3 id='tymethod.flush' class='method'><span id='flush.v' class='invisible'><code>fn <a href='#tymethod.flush' class='fnname'>flush</a>(&amp;mut self) -&gt; <a class="type" href="../../std/io/type.Result.html" title="type std::io::Result">Result</a>&lt;<a class="primitive" href="../primitive.unit.html">()</a>&gt;</code></span></h3><div class='docblock'><p>Flush this output stream, ensuring that all intermediately buffered
contents reach their destination.</p>
<h1 id="errors-1" class="section-header"><a href="#errors-1">Errors</a></h1>
<p>It is considered an error if not all bytes could be written due to
I/O errors or EOF being reached.</p>
<h1 id="examples-2" class="section-header"><a href="#examples-2">Examples</a></h1>
<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">io</span>::<span class="ident">prelude</span>::<span class="kw-2">*</span>;
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">io</span>::<span class="ident">BufWriter</span>;
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">fs</span>::<span class="ident">File</span>;

<span class="kw">fn</span> <span class="ident">main</span>() <span class="op">-&gt;</span> <span class="ident">std</span>::<span class="ident">io</span>::<span class="prelude-ty">Result</span><span class="op">&lt;</span>()<span class="op">&gt;</span> {
    <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">buffer</span> <span class="op">=</span> <span class="ident">BufWriter</span>::<span class="ident">new</span>(<span class="ident">File</span>::<span class="ident">create</span>(<span class="string">&quot;foo.txt&quot;</span>)<span class="question-mark">?</span>);

    <span class="ident">buffer</span>.<span class="ident">write</span>(<span class="string">b&quot;some bytes&quot;</span>)<span class="question-mark">?</span>;
    <span class="ident">buffer</span>.<span class="ident">flush</span>()<span class="question-mark">?</span>;
    <span class="prelude-val">Ok</span>(())
}<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Ause%20std%3A%3Aio%3A%3Aprelude%3A%3A*%3B%0Ause%20std%3A%3Aio%3A%3ABufWriter%3B%0Ause%20std%3A%3Afs%3A%3AFile%3B%0A%0Afn%20main()%20-%3E%20std%3A%3Aio%3A%3AResult%3C()%3E%20%7B%0A%20%20%20%20let%20mut%20buffer%20%3D%20BufWriter%3A%3Anew(File%3A%3Acreate(%22foo.txt%22)%3F)%3B%0A%0A%20%20%20%20buffer.write(b%22some%20bytes%22)%3F%3B%0A%20%20%20%20buffer.flush()%3F%3B%0A%20%20%20%20Ok(())%0A%7D">Run</a></pre>
</div></div>
            <h2 id='provided-methods' class='small-section-header'>
              Provided Methods<a href='#provided-methods' class='anchor'></a>
            </h2>
            <div class='methods'>
        <h3 id='method.write_all' class='method'><span id='write_all.v' class='invisible'><code>fn <a href='#method.write_all' class='fnname'>write_all</a>(&amp;mut self, buf: <a class="primitive" href="../primitive.slice.html">&amp;[</a><a class="primitive" href="../primitive.u8.html">u8</a><a class="primitive" href="../primitive.slice.html">]</a>) -&gt; <a class="type" href="../../std/io/type.Result.html" title="type std::io::Result">Result</a>&lt;<a class="primitive" href="../primitive.unit.html">()</a>&gt;</code></span></h3><div class='docblock'><p>Attempts to write an entire buffer into this write.</p>
<p>This method will continuously call <a href="#tymethod.write"><code>write</code></a> until there is no more data
to be written or an error of non-<a href="../../std/io/enum.ErrorKind.html#variant.Interrupted"><code>ErrorKind::Interrupted</code></a> kind is
returned. This method will not return until the entire buffer has been
successfully written or such an error occurs. The first error that is
not of <a href="../../std/io/enum.ErrorKind.html#variant.Interrupted"><code>ErrorKind::Interrupted</code></a> kind generated from this method will be
returned.</p>
<h1 id="errors-2" class="section-header"><a href="#errors-2">Errors</a></h1>
<p>This function will return the first error of
non-<a href="../../std/io/enum.ErrorKind.html#variant.Interrupted"><code>ErrorKind::Interrupted</code></a> kind that <a href="#tymethod.write"><code>write</code></a> returns.</p>
<h1 id="examples-3" class="section-header"><a href="#examples-3">Examples</a></h1>
<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">io</span>::<span class="ident">prelude</span>::<span class="kw-2">*</span>;
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">fs</span>::<span class="ident">File</span>;

<span class="kw">fn</span> <span class="ident">main</span>() <span class="op">-&gt;</span> <span class="ident">std</span>::<span class="ident">io</span>::<span class="prelude-ty">Result</span><span class="op">&lt;</span>()<span class="op">&gt;</span> {
    <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">buffer</span> <span class="op">=</span> <span class="ident">File</span>::<span class="ident">create</span>(<span class="string">&quot;foo.txt&quot;</span>)<span class="question-mark">?</span>;

    <span class="ident">buffer</span>.<span class="ident">write_all</span>(<span class="string">b&quot;some bytes&quot;</span>)<span class="question-mark">?</span>;
    <span class="prelude-val">Ok</span>(())
}<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Ause%20std%3A%3Aio%3A%3Aprelude%3A%3A*%3B%0Ause%20std%3A%3Afs%3A%3AFile%3B%0A%0Afn%20main()%20-%3E%20std%3A%3Aio%3A%3AResult%3C()%3E%20%7B%0A%20%20%20%20let%20mut%20buffer%20%3D%20File%3A%3Acreate(%22foo.txt%22)%3F%3B%0A%0A%20%20%20%20buffer.write_all(b%22some%20bytes%22)%3F%3B%0A%20%20%20%20Ok(())%0A%7D">Run</a></pre>
</div><h3 id='method.write_fmt' class='method'><span id='write_fmt.v' class='invisible'><code>fn <a href='#method.write_fmt' class='fnname'>write_fmt</a>(&amp;mut self, fmt: <a class="struct" href="../../std/fmt/struct.Arguments.html" title="struct std::fmt::Arguments">Arguments</a>) -&gt; <a class="type" href="../../std/io/type.Result.html" title="type std::io::Result">Result</a>&lt;<a class="primitive" href="../primitive.unit.html">()</a>&gt;</code></span></h3><div class='docblock'><p>Writes a formatted string into this writer, returning any error
encountered.</p>
<p>This method is primarily used to interface with the
<a href="../macro.format_args.html"><code>format_args!</code></a> macro, but it is rare that this should
explicitly be called. The <a href="../macro.write.html"><code>write!</code></a> macro should be favored to
invoke this method instead.</p>
<p>This function internally uses the <a href="#method.write_all"><code>write_all</code></a> method on
this trait and hence will continuously write data so long as no errors
are received. This also means that partial writes are not indicated in
this signature.</p>
<h1 id="errors-3" class="section-header"><a href="#errors-3">Errors</a></h1>
<p>This function will return any I/O error reported while formatting.</p>
<h1 id="examples-4" class="section-header"><a href="#examples-4">Examples</a></h1>
<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">io</span>::<span class="ident">prelude</span>::<span class="kw-2">*</span>;
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">fs</span>::<span class="ident">File</span>;

<span class="kw">fn</span> <span class="ident">main</span>() <span class="op">-&gt;</span> <span class="ident">std</span>::<span class="ident">io</span>::<span class="prelude-ty">Result</span><span class="op">&lt;</span>()<span class="op">&gt;</span> {
    <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">buffer</span> <span class="op">=</span> <span class="ident">File</span>::<span class="ident">create</span>(<span class="string">&quot;foo.txt&quot;</span>)<span class="question-mark">?</span>;

    <span class="comment">// this call</span>
    <span class="macro">write</span><span class="macro">!</span>(<span class="ident">buffer</span>, <span class="string">&quot;{:.*}&quot;</span>, <span class="number">2</span>, <span class="number">1.234567</span>)<span class="question-mark">?</span>;
    <span class="comment">// turns into this:</span>
    <span class="ident">buffer</span>.<span class="ident">write_fmt</span>(<span class="macro">format_args</span><span class="macro">!</span>(<span class="string">&quot;{:.*}&quot;</span>, <span class="number">2</span>, <span class="number">1.234567</span>))<span class="question-mark">?</span>;
    <span class="prelude-val">Ok</span>(())
}<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Ause%20std%3A%3Aio%3A%3Aprelude%3A%3A*%3B%0Ause%20std%3A%3Afs%3A%3AFile%3B%0A%0Afn%20main()%20-%3E%20std%3A%3Aio%3A%3AResult%3C()%3E%20%7B%0A%20%20%20%20let%20mut%20buffer%20%3D%20File%3A%3Acreate(%22foo.txt%22)%3F%3B%0A%0A%20%20%20%20%2F%2F%20this%20call%0A%20%20%20%20write!(buffer%2C%20%22%7B%3A.*%7D%22%2C%202%2C%201.234567)%3F%3B%0A%20%20%20%20%2F%2F%20turns%20into%20this%3A%0A%20%20%20%20buffer.write_fmt(format_args!(%22%7B%3A.*%7D%22%2C%202%2C%201.234567))%3F%3B%0A%20%20%20%20Ok(())%0A%7D">Run</a></pre>
</div><div class="important-traits"><div class='tooltip'>ⓘ<span class='tooltiptext'>Important traits for <a class="primitive" href="../primitive.reference.html">&amp;'a mut </a>I</span></div><div class="content hidden"><h3 class="important">Important traits for <a class="primitive" href="../primitive.reference.html">&amp;'a mut </a>I</h3><code class="content"><span class="where fmt-newline">impl&lt;'a, I&gt; <a class="trait" href="../../std/iter/trait.Iterator.html" title="trait std::iter::Iterator">Iterator</a> for <a class="primitive" href="../primitive.reference.html">&amp;'a mut </a>I <span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;I: <a class="trait" href="../../std/iter/trait.Iterator.html" title="trait std::iter::Iterator">Iterator</a> + ?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>,&nbsp;</span></span><span class="where fmt-newline">    type <a href='../../std/iter/trait.Iterator.html#associatedtype.Item' class="type">Item</a> = &lt;I as <a class="trait" href="../../std/iter/trait.Iterator.html" title="trait std::iter::Iterator">Iterator</a>&gt;::<a class="type" href="../../std/iter/trait.Iterator.html#associatedtype.Item" title="type std::iter::Iterator::Item">Item</a>;</span><span class="where fmt-newline">impl&lt;'a, R:&nbsp;<a class="trait" href="../../std/io/trait.Read.html" title="trait std::io::Read">Read</a> + ?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>&gt; <a class="trait" href="../../std/io/trait.Read.html" title="trait std::io::Read">Read</a> for <a class="primitive" href="../primitive.reference.html">&amp;'a mut </a>R</span><span class="where fmt-newline">impl&lt;'a, W:&nbsp;<a class="trait" href="../../std/io/trait.Write.html" title="trait std::io::Write">Write</a> + ?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>&gt; <a class="trait" href="../../std/io/trait.Write.html" title="trait std::io::Write">Write</a> for <a class="primitive" href="../primitive.reference.html">&amp;'a mut </a>W</span></code></div></div><h3 id='method.by_ref' class='method'><span id='by_ref.v' class='invisible'><code>fn <a href='#method.by_ref' class='fnname'>by_ref</a>(&amp;mut self) -&gt; <a class="primitive" href="../primitive.reference.html">&amp;mut </a>Self <span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;Self: <a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>,&nbsp;</span></code></span></h3><div class='docblock'><p>Creates a &quot;by reference&quot; adaptor for this instance of <code>Write</code>.</p>
<p>The returned adaptor also implements <code>Write</code> and will simply borrow this
current writer.</p>
<h1 id="examples-5" class="section-header"><a href="#examples-5">Examples</a></h1>
<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">io</span>::<span class="ident">Write</span>;
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">fs</span>::<span class="ident">File</span>;

<span class="kw">fn</span> <span class="ident">main</span>() <span class="op">-&gt;</span> <span class="ident">std</span>::<span class="ident">io</span>::<span class="prelude-ty">Result</span><span class="op">&lt;</span>()<span class="op">&gt;</span> {
    <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">buffer</span> <span class="op">=</span> <span class="ident">File</span>::<span class="ident">create</span>(<span class="string">&quot;foo.txt&quot;</span>)<span class="question-mark">?</span>;

    <span class="kw">let</span> <span class="ident">reference</span> <span class="op">=</span> <span class="ident">buffer</span>.<span class="ident">by_ref</span>();

    <span class="comment">// we can use reference just like our original buffer</span>
    <span class="ident">reference</span>.<span class="ident">write_all</span>(<span class="string">b&quot;some bytes&quot;</span>)<span class="question-mark">?</span>;
    <span class="prelude-val">Ok</span>(())
}<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Ause%20std%3A%3Aio%3A%3AWrite%3B%0Ause%20std%3A%3Afs%3A%3AFile%3B%0A%0Afn%20main()%20-%3E%20std%3A%3Aio%3A%3AResult%3C()%3E%20%7B%0A%20%20%20%20let%20mut%20buffer%20%3D%20File%3A%3Acreate(%22foo.txt%22)%3F%3B%0A%0A%20%20%20%20let%20reference%20%3D%20buffer.by_ref()%3B%0A%0A%20%20%20%20%2F%2F%20we%20can%20use%20reference%20just%20like%20our%20original%20buffer%0A%20%20%20%20reference.write_all(b%22some%20bytes%22)%3F%3B%0A%20%20%20%20Ok(())%0A%7D">Run</a></pre>
</div></div>
        <h2 id='implementors' class='small-section-header'>
          Implementors<a href='#implementors' class='anchor'></a>
        </h2>
        <ul class='item-list' id='implementors-list'>
    <li><table class='table-display'><tbody><tr><td><code>impl Write for <a class="struct" href="../../std/fs/struct.File.html" title="struct std::fs::File">File</a></code><td><div class='out-of-band'><a class='srclink' href='../../src/std/fs.rs.html#617-622' title='goto source code'>[src]</a></div></td></tr></tbody></table></li>
<li><table class='table-display'><tbody><tr><td><code>impl&lt;'a&gt; Write for &amp;'a <a class="struct" href="../../std/fs/struct.File.html" title="struct std::fs::File">File</a></code><td><div class='out-of-band'><a class='srclink' href='../../src/std/fs.rs.html#641-646' title='goto source code'>[src]</a></div></td></tr></tbody></table></li>
<li><table class='table-display'><tbody><tr><td><code>impl&lt;W:&nbsp;<a class="trait" href="../../std/io/trait.Write.html" title="trait std::io::Write">Write</a>&gt; Write for <a class="struct" href="../../std/io/struct.BufWriter.html" title="struct std::io::BufWriter">BufWriter</a>&lt;W&gt;</code><td><div class='out-of-band'><a class='srclink' href='../../src/std/io/buffered.rs.html#584-601' title='goto source code'>[src]</a></div></td></tr></tbody></table></li>
<li><table class='table-display'><tbody><tr><td><code>impl&lt;W:&nbsp;<a class="trait" href="../../std/io/trait.Write.html" title="trait std::io::Write">Write</a>&gt; Write for <a class="struct" href="../../std/io/struct.LineWriter.html" title="struct std::io::LineWriter">LineWriter</a>&lt;W&gt;</code><td><div class='out-of-band'><a class='srclink' href='../../src/std/io/buffered.rs.html#896-938' title='goto source code'>[src]</a></div></td></tr></tbody></table></li>
<li><table class='table-display'><tbody><tr><td><code>impl&lt;'a&gt; Write for <a class="struct" href="../../std/io/struct.Cursor.html" title="struct std::io::Cursor">Cursor</a>&lt;<a class="primitive" href="../primitive.slice.html">&amp;'a mut [</a><a class="primitive" href="../primitive.u8.html">u8</a><a class="primitive" href="../primitive.slice.html">]</a>&gt;</code><td><div class='out-of-band'><a class='srclink' href='../../src/std/io/cursor.rs.html#307-313' title='goto source code'>[src]</a></div></td></tr></tbody></table></li>
<li><table class='table-display'><tbody><tr><td><code>impl&lt;'a&gt; Write for <a class="struct" href="../../std/io/struct.Cursor.html" title="struct std::io::Cursor">Cursor</a>&lt;&amp;'a mut <a class="struct" href="../../std/vec/struct.Vec.html" title="struct std::vec::Vec">Vec</a>&lt;<a class="primitive" href="../primitive.u8.html">u8</a>&gt;&gt;</code><td><div class='out-of-band'><a class='srclink' href='../../src/std/io/cursor.rs.html#316-321' title='goto source code'>[src]</a></div></td></tr></tbody></table></li>
<li><table class='table-display'><tbody><tr><td><code>impl Write for <a class="struct" href="../../std/io/struct.Cursor.html" title="struct std::io::Cursor">Cursor</a>&lt;<a class="struct" href="../../std/vec/struct.Vec.html" title="struct std::vec::Vec">Vec</a>&lt;<a class="primitive" href="../primitive.u8.html">u8</a>&gt;&gt;</code><td><div class='out-of-band'><a class='srclink' href='../../src/std/io/cursor.rs.html#324-329' title='goto source code'>[src]</a></div></td></tr></tbody></table></li>
<li><table class='table-display'><tbody><tr><td><code>impl Write for <a class="struct" href="../../std/io/struct.Cursor.html" title="struct std::io::Cursor">Cursor</a>&lt;<a class="struct" href="../../std/boxed/struct.Box.html" title="struct std::boxed::Box">Box</a>&lt;<a class="primitive" href="../primitive.slice.html">[</a><a class="primitive" href="../primitive.u8.html">u8</a><a class="primitive" href="../primitive.slice.html">]</a>&gt;&gt;</code><td><div class='out-of-band'><a class='srclink' href='../../src/std/io/cursor.rs.html#332-338' title='goto source code'>[src]</a></div></td></tr></tbody></table></li>
<li><table class='table-display'><tbody><tr><td><code>impl&lt;'a, W:&nbsp;<a class="trait" href="../../std/io/trait.Write.html" title="trait std::io::Write">Write</a> + ?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>&gt; Write for <a class="primitive" href="../primitive.reference.html">&amp;'a mut </a>W</code><td><div class='out-of-band'><a class='srclink' href='../../src/std/io/impls.rs.html#47-63' title='goto source code'>[src]</a></div></td></tr></tbody></table></li>
<li><table class='table-display'><tbody><tr><td><code>impl&lt;W:&nbsp;<a class="trait" href="../../std/io/trait.Write.html" title="trait std::io::Write">Write</a> + ?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>&gt; Write for <a class="struct" href="../../std/boxed/struct.Box.html" title="struct std::boxed::Box">Box</a>&lt;W&gt;</code><td><div class='out-of-band'><a class='srclink' href='../../src/std/io/impls.rs.html#116-132' title='goto source code'>[src]</a></div></td></tr></tbody></table></li>
<li><table class='table-display'><tbody><tr><td><code>impl&lt;'a&gt; Write for <a class="primitive" href="../primitive.slice.html">&amp;'a mut [</a><a class="primitive" href="../primitive.u8.html">u8</a><a class="primitive" href="../primitive.slice.html">]</a></code><td><div class='out-of-band'><a class='srclink' href='../../src/std/io/impls.rs.html#234-255' title='goto source code'>[src]</a></div></td></tr></tbody></table></li>
<li><table class='table-display'><tbody><tr><td><code>impl Write for <a class="struct" href="../../std/vec/struct.Vec.html" title="struct std::vec::Vec">Vec</a>&lt;<a class="primitive" href="../primitive.u8.html">u8</a>&gt;</code><td><div class='out-of-band'><a class='srclink' href='../../src/std/io/impls.rs.html#260-275' title='goto source code'>[src]</a></div></td></tr></tbody></table></li>
<li><table class='table-display'><tbody><tr><td><code>impl Write for <a class="struct" href="../../std/io/struct.Sink.html" title="struct std::io::Sink">Sink</a></code><td><div class='out-of-band'><a class='srclink' href='../../src/std/io/util.rs.html#200-205' title='goto source code'>[src]</a></div></td></tr></tbody></table></li>
<li><table class='table-display'><tbody><tr><td><code>impl Write for <a class="struct" href="../../std/io/struct.Stdout.html" title="struct std::io::Stdout">Stdout</a></code><td><div class='out-of-band'><a class='srclink' href='../../src/std/io/stdio.rs.html#448-461' title='goto source code'>[src]</a></div></td></tr></tbody></table></li>
<li><table class='table-display'><tbody><tr><td><code>impl&lt;'a&gt; Write for <a class="struct" href="../../std/io/struct.StdoutLock.html" title="struct std::io::StdoutLock">StdoutLock</a>&lt;'a&gt;</code><td><div class='out-of-band'><a class='srclink' href='../../src/std/io/stdio.rs.html#463-470' title='goto source code'>[src]</a></div></td></tr></tbody></table></li>
<li><table class='table-display'><tbody><tr><td><code>impl Write for <a class="struct" href="../../std/io/struct.Stderr.html" title="struct std::io::Stderr">Stderr</a></code><td><div class='out-of-band'><a class='srclink' href='../../src/std/io/stdio.rs.html#583-596' title='goto source code'>[src]</a></div></td></tr></tbody></table></li>
<li><table class='table-display'><tbody><tr><td><code>impl&lt;'a&gt; Write for <a class="struct" href="../../std/io/struct.StderrLock.html" title="struct std::io::StderrLock">StderrLock</a>&lt;'a&gt;</code><td><div class='out-of-band'><a class='srclink' href='../../src/std/io/stdio.rs.html#598-605' title='goto source code'>[src]</a></div></td></tr></tbody></table></li>
<li><table class='table-display'><tbody><tr><td><code>impl Write for <a class="struct" href="../../std/net/struct.TcpStream.html" title="struct std::net::TcpStream">TcpStream</a></code><td><div class='out-of-band'><a class='srclink' href='../../src/std/net/tcp.rs.html#588-591' title='goto source code'>[src]</a></div></td></tr></tbody></table></li>
<li><table class='table-display'><tbody><tr><td><code>impl&lt;'a&gt; Write for &amp;'a <a class="struct" href="../../std/net/struct.TcpStream.html" title="struct std::net::TcpStream">TcpStream</a></code><td><div class='out-of-band'><a class='srclink' href='../../src/std/net/tcp.rs.html#602-605' title='goto source code'>[src]</a></div></td></tr></tbody></table></li>
<li><table class='table-display'><tbody><tr><td><code>impl Write for <a class="struct" href="../../std/process/struct.ChildStdin.html" title="struct std::process::ChildStdin">ChildStdin</a></code><td><div class='out-of-band'><a class='srclink' href='../../src/std/process.rs.html#233-241' title='goto source code'>[src]</a></div></td></tr></tbody></table></li>
<li><table class='table-display'><tbody><tr><td><code>impl Write for <a class="struct" href="../../std/os/unix/net/struct.UnixStream.html" title="struct std::os::unix::net::UnixStream">UnixStream</a></code><td><div class='out-of-band'><a class='srclink' href='../../src/std/sys/unix/ext/net.rs.html#580-588' title='goto source code'>[src]</a></div></td></tr></tbody></table></li>
<li><table class='table-display'><tbody><tr><td><code>impl&lt;'a&gt; Write for &amp;'a <a class="struct" href="../../std/os/unix/net/struct.UnixStream.html" title="struct std::os::unix::net::UnixStream">UnixStream</a></code><td><div class='out-of-band'><a class='srclink' href='../../src/std/sys/unix/ext/net.rs.html#591-599' title='goto source code'>[src]</a></div></td></tr></tbody></table></li>
</ul><script type="text/javascript">window.inlined_types=new Set([]);</script><script type="text/javascript" async
                         src="../../implementors/std/io/trait.Write.js">
                 </script></section><section id="search" class="content hidden"></section><section class="footer"></section><aside id="help" class="hidden"><div><h1 class="hidden">Help</h1><div class="shortcuts"><h2>Keyboard Shortcuts</h2><dl><dt><kbd>?</kbd></dt><dd>Show this help dialog</dd><dt><kbd>S</kbd></dt><dd>Focus the search field</dd><dt><kbd>↑</kbd></dt><dd>Move up in search results</dd><dt><kbd>↓</kbd></dt><dd>Move down in search results</dd><dt><kbd>↹</kbd></dt><dd>Switch tab</dd><dt><kbd>&#9166;</kbd></dt><dd>Go to active search result</dd><dt><kbd>+</kbd></dt><dd>Expand all sections</dd><dt><kbd>-</kbd></dt><dd>Collapse all sections</dd></dl></div><div class="infos"><h2>Search Tricks</h2><p>Prefix searches with a type followed by a colon (e.g. <code>fn:</code>) to restrict the search to a given type.</p><p>Accepted types are: <code>fn</code>, <code>mod</code>, <code>struct</code>, <code>enum</code>, <code>trait</code>, <code>type</code>, <code>macro</code>, and <code>const</code>.</p><p>Search functions by type signature (e.g. <code>vec -> usize</code> or <code>* -> vec</code>)</p><p>Search multiple things at once by splitting your query with comma (e.g. <code>str,u8</code> or <code>String,struct:Vec,test</code>)</p></div></div></aside><script>window.rootPath = "../../";window.currentCrate = "std";</script><script src="../../aliases.js"></script><script src="../../main.js"></script><script defer src="../../search-index.js"></script></body></html>