Sophie

Sophie

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

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 `Read` trait in crate `std`."><meta name="keywords" content="rust, rustlang, rust-lang, Read"><title>std::io::Read - 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 Read</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.read">read</a></div><a class="sidebar-title" href="#provided-methods">Provided Methods</a><div class="sidebar-links"><a href="#method.initializer">initializer</a><a href="#method.read_to_end">read_to_end</a><a href="#method.read_to_string">read_to_string</a><a href="#method.read_exact">read_exact</a><a href="#method.by_ref">by_ref</a><a href="#method.bytes">bytes</a><a href="#method.chars">chars</a><a href="#method.chain">chain</a><a href="#method.take">take</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: 'Read', 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=''>Read</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#470-920' title='goto source code'>[src]</a></span></h1><div class="docblock type-decl"><pre class='rust trait'>pub trait Read {
    fn <a href='#tymethod.read' class='fnname'>read</a>(&amp;mut self, buf: <a class="primitive" href="../primitive.slice.html">&amp;mut [</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;;

    unsafe fn <a href='#method.initializer' class='fnname'>initializer</a>(&amp;self) -&gt; <a class="struct" href="../../std/io/struct.Initializer.html" title="struct std::io::Initializer">Initializer</a> { ... }
<div class='item-spacer'></div>    fn <a href='#method.read_to_end' class='fnname'>read_to_end</a>(&amp;mut self, buf: &amp;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; <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='#method.read_to_string' class='fnname'>read_to_string</a>(&amp;mut self, buf: &amp;mut <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</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='#method.read_exact' class='fnname'>read_exact</a>(&amp;mut self, buf: <a class="primitive" href="../primitive.slice.html">&amp;mut [</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.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>,
    { ... }
<div class='item-spacer'></div>    fn <a href='#method.bytes' class='fnname'>bytes</a>(self) -&gt; <a class="struct" href="../../std/io/struct.Bytes.html" title="struct std::io::Bytes">Bytes</a>&lt;Self&gt;<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>,
    { ... }
<div class='item-spacer'></div>    fn <a href='#method.chars' class='fnname'>chars</a>(self) -&gt; <a class="struct" href="../../std/io/struct.Chars.html" title="struct std::io::Chars">Chars</a>&lt;Self&gt;<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>,
    { ... }
<div class='item-spacer'></div>    fn <a href='#method.chain' class='fnname'>chain</a>&lt;R:&nbsp;<a class="trait" href="../../std/io/trait.Read.html" title="trait std::io::Read">Read</a>&gt;(self, next: R) -&gt; <a class="struct" href="../../std/io/struct.Chain.html" title="struct std::io::Chain">Chain</a>&lt;Self, R&gt;<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>,
    { ... }
<div class='item-spacer'></div>    fn <a href='#method.take' class='fnname'>take</a>(self, limit: <a class="primitive" href="../primitive.u64.html">u64</a>) -&gt; <a class="struct" href="../../std/io/struct.Take.html" title="struct std::io::Take">Take</a>&lt;Self&gt;<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>The <code>Read</code> trait allows for reading bytes from a source.</p>
<p>Implementors of the <code>Read</code> trait are called 'readers'.</p>
<p>Readers are defined by one required method, <a href="trait.Read.html#tymethod.read"><code>read()</code></a>. Each call to <a href="trait.Read.html#tymethod.read"><code>read()</code></a>
will attempt to pull bytes from this source into a provided buffer. A
number of other methods are implemented in terms of <a href="trait.Read.html#tymethod.read"><code>read()</code></a>, giving
implementors a number of ways to read bytes while only needing to implement
a single method.</p>
<p>Readers are intended to be composable with one another. Many implementors
throughout <a href="../../std/io/index.html"><code>std::io</code></a> take and provide types which implement the <code>Read</code>
trait.</p>
<p>Please note that each call to <a href="trait.Read.html#tymethod.read"><code>read()</code></a> may involve a system call, and
therefore, using something that implements <a href="trait.BufRead.html"><code>BufRead</code></a>, such as
<a href="struct.BufReader.html"><code>BufReader</code></a>, will be more efficient.</p>
<h1 id="examples" class="section-header"><a href="#examples">Examples</a></h1>
<p><a href="../fs/struct.File.html"><code>File</code></a>s implement <code>Read</code>:</p>

<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">io</span>;
<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">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">f</span> <span class="op">=</span> <span class="ident">File</span>::<span class="ident">open</span>(<span class="string">&quot;foo.txt&quot;</span>)<span class="question-mark">?</span>;
    <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">buffer</span> <span class="op">=</span> [<span class="number">0</span>; <span class="number">10</span>];

    <span class="comment">// read up to 10 bytes</span>
    <span class="ident">f</span>.<span class="ident">read</span>(<span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="ident">buffer</span>)<span class="question-mark">?</span>;

    <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">buffer</span> <span class="op">=</span> <span class="macro">vec</span><span class="macro">!</span>[<span class="number">0</span>; <span class="number">10</span>];
    <span class="comment">// read the whole file</span>
    <span class="ident">f</span>.<span class="ident">read_to_end</span>(<span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="ident">buffer</span>)<span class="question-mark">?</span>;

    <span class="comment">// read into a String, so that you don&#39;t need to do the conversion.</span>
    <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">buffer</span> <span class="op">=</span> <span class="ident">String</span>::<span class="ident">new</span>();
    <span class="ident">f</span>.<span class="ident">read_to_string</span>(<span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="ident">buffer</span>)<span class="question-mark">?</span>;

    <span class="comment">// and more! See the other methods for more details.</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%3B%0Ause%20std%3A%3Aio%3A%3Aprelude%3A%3A*%3B%0Ause%20std%3A%3Afs%3A%3AFile%3B%0A%0Afn%20main()%20-%3E%20io%3A%3AResult%3C()%3E%20%7B%0A%20%20%20%20let%20mut%20f%20%3D%20File%3A%3Aopen(%22foo.txt%22)%3F%3B%0A%20%20%20%20let%20mut%20buffer%20%3D%20%5B0%3B%2010%5D%3B%0A%0A%20%20%20%20%2F%2F%20read%20up%20to%2010%20bytes%0A%20%20%20%20f.read(%26mut%20buffer)%3F%3B%0A%0A%20%20%20%20let%20mut%20buffer%20%3D%20vec!%5B0%3B%2010%5D%3B%0A%20%20%20%20%2F%2F%20read%20the%20whole%20file%0A%20%20%20%20f.read_to_end(%26mut%20buffer)%3F%3B%0A%0A%20%20%20%20%2F%2F%20read%20into%20a%20String%2C%20so%20that%20you%20don't%20need%20to%20do%20the%20conversion.%0A%20%20%20%20let%20mut%20buffer%20%3D%20String%3A%3Anew()%3B%0A%20%20%20%20f.read_to_string(%26mut%20buffer)%3F%3B%0A%0A%20%20%20%20%2F%2F%20and%20more!%20See%20the%20other%20methods%20for%20more%20details.%0A%20%20%20%20Ok(())%0A%7D">Run</a></pre>
<p>Read from <a href="../../std/primitive.str.html"><code>&amp;str</code></a> because <a href="../../std/primitive.slice.html"><code>&amp;[u8]</code></a> implements <code>Read</code>:</p>

<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">fn</span> <span class="ident">main</span>() <span class="op">-&gt;</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">b</span> <span class="op">=</span> <span class="string">&quot;This string will be read&quot;</span>.<span class="ident">as_bytes</span>();
    <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">buffer</span> <span class="op">=</span> [<span class="number">0</span>; <span class="number">10</span>];

    <span class="comment">// read up to 10 bytes</span>
    <span class="ident">b</span>.<span class="ident">read</span>(<span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="ident">buffer</span>)<span class="question-mark">?</span>;

    <span class="comment">// etc... it works exactly as a File does!</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%3B%0Ause%20std%3A%3Aio%3A%3Aprelude%3A%3A*%3B%0A%0Afn%20main()%20-%3E%20io%3A%3AResult%3C()%3E%20%7B%0A%20%20%20%20let%20mut%20b%20%3D%20%22This%20string%20will%20be%20read%22.as_bytes()%3B%0A%20%20%20%20let%20mut%20buffer%20%3D%20%5B0%3B%2010%5D%3B%0A%0A%20%20%20%20%2F%2F%20read%20up%20to%2010%20bytes%0A%20%20%20%20b.read(%26mut%20buffer)%3F%3B%0A%0A%20%20%20%20%2F%2F%20etc...%20it%20works%20exactly%20as%20a%20File%20does!%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.read' class='method'><span id='read.v' class='invisible'><code>fn <a href='#tymethod.read' class='fnname'>read</a>(&amp;mut self, buf: <a class="primitive" href="../primitive.slice.html">&amp;mut [</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>Pull some bytes from this source into the specified buffer, returning
how many bytes were read.</p>
<p>This function does not provide any guarantees about whether it blocks
waiting for data, but if an object needs to block for a read but cannot
it will typically signal this via an <a href="../../std/result/enum.Result.html#variant.Err"><code>Err</code></a> return value.</p>
<p>If the return value of this method 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 nonzero <code>n</code> value indicates
that the buffer <code>buf</code> has been filled in with <code>n</code> bytes of data from this
source. If <code>n</code> is <code>0</code>, then it can indicate one of two scenarios:</p>
<ol>
<li>This reader has reached its &quot;end of file&quot; and will likely no longer
be able to produce bytes. Note that this does not mean that the
reader will <em>always</em> no longer be able to produce bytes.</li>
<li>The buffer specified was 0 bytes in length.</li>
</ol>
<p>No guarantees are provided about the contents of <code>buf</code> when this
function is called, implementations cannot rely on any property of the
contents of <code>buf</code> being true. It is recommended that implementations
only write data to <code>buf</code> instead of reading its contents.</p>
<h1 id="errors" class="section-header"><a href="#errors">Errors</a></h1>
<p>If this function encounters any form of I/O or other error, an error
variant will be returned. If an error is returned then it must be
guaranteed that no bytes were read.</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 read
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>
<p><a href="../fs/struct.File.html"><code>File</code></a>s implement <code>Read</code>:</p>

<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">io</span>;
<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">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">f</span> <span class="op">=</span> <span class="ident">File</span>::<span class="ident">open</span>(<span class="string">&quot;foo.txt&quot;</span>)<span class="question-mark">?</span>;
    <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">buffer</span> <span class="op">=</span> [<span class="number">0</span>; <span class="number">10</span>];

    <span class="comment">// read up to 10 bytes</span>
    <span class="ident">f</span>.<span class="ident">read</span>(<span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="ident">buffer</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%3B%0Ause%20std%3A%3Aio%3A%3Aprelude%3A%3A*%3B%0Ause%20std%3A%3Afs%3A%3AFile%3B%0A%0Afn%20main()%20-%3E%20io%3A%3AResult%3C()%3E%20%7B%0A%20%20%20%20let%20mut%20f%20%3D%20File%3A%3Aopen(%22foo.txt%22)%3F%3B%0A%20%20%20%20let%20mut%20buffer%20%3D%20%5B0%3B%2010%5D%3B%0A%0A%20%20%20%20%2F%2F%20read%20up%20to%2010%20bytes%0A%20%20%20%20f.read(%26mut%20buffer%5B..%5D)%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.initializer' class='method'><span id='initializer.v' class='invisible'><code>unsafe fn <a href='#method.initializer' class='fnname'>initializer</a>(&amp;self) -&gt; <a class="struct" href="../../std/io/struct.Initializer.html" title="struct std::io::Initializer">Initializer</a></code></span></h3><div class='stability'><div class='stab unstable'><span class=microscope>🔬</span> This is a nightly-only experimental API.  (<code>read_initializer </code><a href="https://github.com/rust-lang/rust/issues/42788">#42788</a>)</div></div><div class='docblock'><p>Determines if this <code>Read</code>er can work with buffers of uninitialized
memory.</p>
<p>The default implementation returns an initializer which will zero
buffers.</p>
<p>If a <code>Read</code>er guarantees that it can work properly with uninitialized
memory, it should call <a href="../../std/io/struct.Initializer.html#method.nop"><code>Initializer::nop()</code></a>. See the documentation for
<a href="../../std/io/struct.Initializer.html"><code>Initializer</code></a> for details.</p>
<p>The behavior of this method must be independent of the state of the
<code>Read</code>er - the method only takes <code>&amp;self</code> so that it can be used through
trait objects.</p>
<h1 id="safety" class="section-header"><a href="#safety">Safety</a></h1>
<p>This method is unsafe because a <code>Read</code>er could otherwise return a
non-zeroing <code>Initializer</code> from another <code>Read</code> type without an <code>unsafe</code>
block.</p>
</div><h3 id='method.read_to_end' class='method'><span id='read_to_end.v' class='invisible'><code>fn <a href='#method.read_to_end' class='fnname'>read_to_end</a>(&amp;mut self, buf: &amp;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; <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>Read all bytes until EOF in this source, placing them into <code>buf</code>.</p>
<p>All bytes read from this source will be appended to the specified buffer
<code>buf</code>. This function will continuously call <a href="trait.Read.html#tymethod.read"><code>read()</code></a> to append more data to
<code>buf</code> until <a href="trait.Read.html#tymethod.read"><code>read()</code></a> returns either <a href="../../std/result/enum.Result.html#variant.Ok"><code>Ok(0)</code></a> or an error of
non-<a href="../../std/io/enum.ErrorKind.html#variant.Interrupted"><code>ErrorKind::Interrupted</code></a> kind.</p>
<p>If successful, this function will return the total number of bytes read.</p>
<h1 id="errors-1" class="section-header"><a href="#errors-1">Errors</a></h1>
<p>If this function encounters an error of the kind
<a href="../../std/io/enum.ErrorKind.html#variant.Interrupted"><code>ErrorKind::Interrupted</code></a> then the error is ignored and the operation
will continue.</p>
<p>If any other read error is encountered then this function immediately
returns. Any bytes which have already been read will be appended to
<code>buf</code>.</p>
<h1 id="examples-2" class="section-header"><a href="#examples-2">Examples</a></h1>
<p><a href="../fs/struct.File.html"><code>File</code></a>s implement <code>Read</code>:</p>

<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">io</span>;
<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">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">f</span> <span class="op">=</span> <span class="ident">File</span>::<span class="ident">open</span>(<span class="string">&quot;foo.txt&quot;</span>)<span class="question-mark">?</span>;
    <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">buffer</span> <span class="op">=</span> <span class="ident">Vec</span>::<span class="ident">new</span>();

    <span class="comment">// read the whole file</span>
    <span class="ident">f</span>.<span class="ident">read_to_end</span>(<span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="ident">buffer</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%3B%0Ause%20std%3A%3Aio%3A%3Aprelude%3A%3A*%3B%0Ause%20std%3A%3Afs%3A%3AFile%3B%0A%0Afn%20main()%20-%3E%20io%3A%3AResult%3C()%3E%20%7B%0A%20%20%20%20let%20mut%20f%20%3D%20File%3A%3Aopen(%22foo.txt%22)%3F%3B%0A%20%20%20%20let%20mut%20buffer%20%3D%20Vec%3A%3Anew()%3B%0A%0A%20%20%20%20%2F%2F%20read%20the%20whole%20file%0A%20%20%20%20f.read_to_end(%26mut%20buffer)%3F%3B%0A%20%20%20%20Ok(())%0A%7D">Run</a></pre>
<p>(See also the <a href="../fs/fn.read.html"><code>std::fs::read</code></a> convenience function for reading from a
file.)</p>
</div><h3 id='method.read_to_string' class='method'><span id='read_to_string.v' class='invisible'><code>fn <a href='#method.read_to_string' class='fnname'>read_to_string</a>(&amp;mut self, buf: &amp;mut <a class="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</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>Read all bytes until EOF in this source, appending them to <code>buf</code>.</p>
<p>If successful, this function returns the number of bytes which were read
and appended to <code>buf</code>.</p>
<h1 id="errors-2" class="section-header"><a href="#errors-2">Errors</a></h1>
<p>If the data in this stream is <em>not</em> valid UTF-8 then an error is
returned and <code>buf</code> is unchanged.</p>
<p>See <a href="#method.read_to_end"><code>read_to_end</code></a> for other error semantics.</p>
<h1 id="examples-3" class="section-header"><a href="#examples-3">Examples</a></h1>
<p><a href="../fs/struct.File.html"><code>File</code></a>s implement <code>Read</code>:</p>

<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">io</span>;
<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">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">f</span> <span class="op">=</span> <span class="ident">File</span>::<span class="ident">open</span>(<span class="string">&quot;foo.txt&quot;</span>)<span class="question-mark">?</span>;
    <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">buffer</span> <span class="op">=</span> <span class="ident">String</span>::<span class="ident">new</span>();

    <span class="ident">f</span>.<span class="ident">read_to_string</span>(<span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="ident">buffer</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%3B%0Ause%20std%3A%3Aio%3A%3Aprelude%3A%3A*%3B%0Ause%20std%3A%3Afs%3A%3AFile%3B%0A%0Afn%20main()%20-%3E%20io%3A%3AResult%3C()%3E%20%7B%0A%20%20%20%20let%20mut%20f%20%3D%20File%3A%3Aopen(%22foo.txt%22)%3F%3B%0A%20%20%20%20let%20mut%20buffer%20%3D%20String%3A%3Anew()%3B%0A%0A%20%20%20%20f.read_to_string(%26mut%20buffer)%3F%3B%0A%20%20%20%20Ok(())%0A%7D">Run</a></pre>
<p>(See also the <a href="../fs/fn.read_to_string.html"><code>std::fs::read_to_string</code></a> convenience function for
reading from a file.)</p>
</div><h3 id='method.read_exact' class='method'><span id='read_exact.v' class='invisible'><code>fn <a href='#method.read_exact' class='fnname'>read_exact</a>(&amp;mut self, buf: <a class="primitive" href="../primitive.slice.html">&amp;mut [</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><div class='since' title='Stable since Rust version 1.6.0'>1.6.0</div></span></h3><div class='docblock'><p>Read the exact number of bytes required to fill <code>buf</code>.</p>
<p>This function reads as many bytes as necessary to completely fill the
specified buffer <code>buf</code>.</p>
<p>No guarantees are provided about the contents of <code>buf</code> when this
function is called, implementations cannot rely on any property of the
contents of <code>buf</code> being true. It is recommended that implementations
only write data to <code>buf</code> instead of reading its contents.</p>
<h1 id="errors-3" class="section-header"><a href="#errors-3">Errors</a></h1>
<p>If this function encounters an error of the kind
<a href="../../std/io/enum.ErrorKind.html#variant.Interrupted"><code>ErrorKind::Interrupted</code></a> then the error is ignored and the operation
will continue.</p>
<p>If this function encounters an &quot;end of file&quot; before completely filling
the buffer, it returns an error of the kind <a href="../../std/io/enum.ErrorKind.html#variant.UnexpectedEof"><code>ErrorKind::UnexpectedEof</code></a>.
The contents of <code>buf</code> are unspecified in this case.</p>
<p>If any other read error is encountered then this function immediately
returns. The contents of <code>buf</code> are unspecified in this case.</p>
<p>If this function returns an error, it is unspecified how many bytes it
has read, but it will never read more than would be necessary to
completely fill the buffer.</p>
<h1 id="examples-4" class="section-header"><a href="#examples-4">Examples</a></h1>
<p><a href="../fs/struct.File.html"><code>File</code></a>s implement <code>Read</code>:</p>

<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">io</span>;
<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">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">f</span> <span class="op">=</span> <span class="ident">File</span>::<span class="ident">open</span>(<span class="string">&quot;foo.txt&quot;</span>)<span class="question-mark">?</span>;
    <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">buffer</span> <span class="op">=</span> [<span class="number">0</span>; <span class="number">10</span>];

    <span class="comment">// read exactly 10 bytes</span>
    <span class="ident">f</span>.<span class="ident">read_exact</span>(<span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="ident">buffer</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%3B%0Ause%20std%3A%3Aio%3A%3Aprelude%3A%3A*%3B%0Ause%20std%3A%3Afs%3A%3AFile%3B%0A%0Afn%20main()%20-%3E%20io%3A%3AResult%3C()%3E%20%7B%0A%20%20%20%20let%20mut%20f%20%3D%20File%3A%3Aopen(%22foo.txt%22)%3F%3B%0A%20%20%20%20let%20mut%20buffer%20%3D%20%5B0%3B%2010%5D%3B%0A%0A%20%20%20%20%2F%2F%20read%20exactly%2010%20bytes%0A%20%20%20%20f.read_exact(%26mut%20buffer)%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>Read</code>.</p>
<p>The returned adaptor also implements <code>Read</code> and will simply borrow this
current reader.</p>
<h1 id="examples-5" class="section-header"><a href="#examples-5">Examples</a></h1>
<p><a href="../fs/struct.File.html"><code>File</code></a>s implement <code>Read</code>:</p>

<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">io</span>;
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">io</span>::<span class="ident">Read</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">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">f</span> <span class="op">=</span> <span class="ident">File</span>::<span class="ident">open</span>(<span class="string">&quot;foo.txt&quot;</span>)<span class="question-mark">?</span>;
    <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">buffer</span> <span class="op">=</span> <span class="ident">Vec</span>::<span class="ident">new</span>();
    <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">other_buffer</span> <span class="op">=</span> <span class="ident">Vec</span>::<span class="ident">new</span>();

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

        <span class="comment">// read at most 5 bytes</span>
        <span class="ident">reference</span>.<span class="ident">take</span>(<span class="number">5</span>).<span class="ident">read_to_end</span>(<span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="ident">buffer</span>)<span class="question-mark">?</span>;

    } <span class="comment">// drop our &amp;mut reference so we can use f again</span>

    <span class="comment">// original file still usable, read the rest</span>
    <span class="ident">f</span>.<span class="ident">read_to_end</span>(<span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="ident">other_buffer</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%3B%0Ause%20std%3A%3Aio%3A%3ARead%3B%0Ause%20std%3A%3Afs%3A%3AFile%3B%0A%0Afn%20main()%20-%3E%20io%3A%3AResult%3C()%3E%20%7B%0A%20%20%20%20let%20mut%20f%20%3D%20File%3A%3Aopen(%22foo.txt%22)%3F%3B%0A%20%20%20%20let%20mut%20buffer%20%3D%20Vec%3A%3Anew()%3B%0A%20%20%20%20let%20mut%20other_buffer%20%3D%20Vec%3A%3Anew()%3B%0A%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20let%20reference%20%3D%20f.by_ref()%3B%0A%0A%20%20%20%20%20%20%20%20%2F%2F%20read%20at%20most%205%20bytes%0A%20%20%20%20%20%20%20%20reference.take(5).read_to_end(%26mut%20buffer)%3F%3B%0A%0A%20%20%20%20%7D%20%2F%2F%20drop%20our%20%26mut%20reference%20so%20we%20can%20use%20f%20again%0A%0A%20%20%20%20%2F%2F%20original%20file%20still%20usable%2C%20read%20the%20rest%0A%20%20%20%20f.read_to_end(%26mut%20other_buffer)%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="struct" href="../../std/io/struct.Bytes.html" title="struct std::io::Bytes">Bytes</a>&lt;R&gt;</span></div><div class="content hidden"><h3 class="important">Important traits for <a class="struct" href="../../std/io/struct.Bytes.html" title="struct std::io::Bytes">Bytes</a>&lt;R&gt;</h3><code class="content"><span class="where fmt-newline">impl&lt;R:&nbsp;<a class="trait" href="../../std/io/trait.Read.html" title="trait std::io::Read">Read</a>&gt; <a class="trait" href="../../std/iter/trait.Iterator.html" title="trait std::iter::Iterator">Iterator</a> for <a class="struct" href="../../std/io/struct.Bytes.html" title="struct std::io::Bytes">Bytes</a>&lt;R&gt;</span><span class="where fmt-newline">    type <a href='../../std/iter/trait.Iterator.html#associatedtype.Item' class="type">Item</a> = <a class="type" href="../../std/io/type.Result.html" title="type std::io::Result">Result</a>&lt;<a class="primitive" href="../primitive.u8.html">u8</a>&gt;;</span></code></div></div><h3 id='method.bytes' class='method'><span id='bytes.v' class='invisible'><code>fn <a href='#method.bytes' class='fnname'>bytes</a>(self) -&gt; <a class="struct" href="../../std/io/struct.Bytes.html" title="struct std::io::Bytes">Bytes</a>&lt;Self&gt; <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>Transforms this <code>Read</code> instance to an <a href="../../std/iter/trait.Iterator.html"><code>Iterator</code></a> over its bytes.</p>
<p>The returned type implements <a href="../../std/iter/trait.Iterator.html"><code>Iterator</code></a> where the <code>Item</code> is
<a href="../../std/result/enum.Result.html"><code>Result</code></a><code>&lt;</code><a href="../../std/primitive.u8.html"><code>u8</code></a><code>,</code><a href="../../std/io/struct.Error.html"><code>io::Error</code></a><code>&gt;</code>.
The yielded item is <a href="../../std/result/enum.Result.html#variant.Ok"><code>Ok</code></a> if a byte was successfully read and <a href="../../std/result/enum.Result.html#variant.Err"><code>Err</code></a>
otherwise. EOF is mapped to returning <a href="../../std/option/enum.Option.html#variant.None"><code>None</code></a> from this iterator.</p>
<h1 id="examples-6" class="section-header"><a href="#examples-6">Examples</a></h1>
<p><a href="../fs/struct.File.html"><code>File</code></a>s implement <code>Read</code>:</p>

<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">io</span>;
<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">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">f</span> <span class="op">=</span> <span class="ident">File</span>::<span class="ident">open</span>(<span class="string">&quot;foo.txt&quot;</span>)<span class="question-mark">?</span>;

    <span class="kw">for</span> <span class="ident">byte</span> <span class="kw">in</span> <span class="ident">f</span>.<span class="ident">bytes</span>() {
        <span class="macro">println</span><span class="macro">!</span>(<span class="string">&quot;{}&quot;</span>, <span class="ident">byte</span>.<span class="ident">unwrap</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%3B%0Ause%20std%3A%3Aio%3A%3Aprelude%3A%3A*%3B%0Ause%20std%3A%3Afs%3A%3AFile%3B%0A%0Afn%20main()%20-%3E%20io%3A%3AResult%3C()%3E%20%7B%0A%20%20%20%20let%20mut%20f%20%3D%20File%3A%3Aopen(%22foo.txt%22)%3F%3B%0A%0A%20%20%20%20for%20byte%20in%20f.bytes()%20%7B%0A%20%20%20%20%20%20%20%20println!(%22%7B%7D%22%2C%20byte.unwrap())%3B%0A%20%20%20%20%7D%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="struct" href="../../std/io/struct.Chars.html" title="struct std::io::Chars">Chars</a>&lt;R&gt;</span></div><div class="content hidden"><h3 class="important">Important traits for <a class="struct" href="../../std/io/struct.Chars.html" title="struct std::io::Chars">Chars</a>&lt;R&gt;</h3><code class="content"><span class="where fmt-newline">impl&lt;R:&nbsp;<a class="trait" href="../../std/io/trait.Read.html" title="trait std::io::Read">Read</a>&gt; <a class="trait" href="../../std/iter/trait.Iterator.html" title="trait std::iter::Iterator">Iterator</a> for <a class="struct" href="../../std/io/struct.Chars.html" title="struct std::io::Chars">Chars</a>&lt;R&gt;</span><span class="where fmt-newline">    type <a href='../../std/iter/trait.Iterator.html#associatedtype.Item' class="type">Item</a> = <a class="enum" href="../../std/result/enum.Result.html" title="enum std::result::Result">Result</a>&lt;<a class="primitive" href="../primitive.char.html">char</a>, <a class="enum" href="../../std/io/enum.CharsError.html" title="enum std::io::CharsError">CharsError</a>&gt;;</span></code></div></div><h3 id='method.chars' class='method'><span id='chars.v' class='invisible'><code>fn <a href='#method.chars' class='fnname'>chars</a>(self) -&gt; <a class="struct" href="../../std/io/struct.Chars.html" title="struct std::io::Chars">Chars</a>&lt;Self&gt; <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='stability'><div class='stab deprecated'>Deprecated since 1.27.0<p>: Use str::from_utf8 instead:
https://doc.rust-lang.org/nightly/std/str/struct.Utf8Error.html#examples</p>
</div><div class='stab unstable'><details><summary><span class=microscope>🔬</span> This is a nightly-only experimental API.  (<code>io </code><a href="https://github.com/rust-lang/rust/issues/27802">#27802</a>)</summary><p>the semantics of a partial read/write of where errors happen is currently unclear and may change</p>
</details></div></div><div class='docblock'><p>Transforms this <code>Read</code> instance to an <a href="../../std/iter/trait.Iterator.html"><code>Iterator</code></a> over <a href="../../std/primitive.char.html"><code>char</code></a>s.</p>
<p>This adaptor will attempt to interpret this reader as a UTF-8 encoded
sequence of characters. The returned iterator will return <a href="../../std/option/enum.Option.html#variant.None"><code>None</code></a> once
EOF is reached for this reader. Otherwise each element yielded will be a
<a href="../../std/result/enum.Result.html"><code>Result</code></a><code>&lt;</code><a href="../../std/primitive.char.html"><code>char</code></a><code>, E&gt;</code> where <code>E</code> may contain information about what I/O error
occurred or where decoding failed.</p>
<p>Currently this adaptor will discard intermediate data read, and should
be avoided if this is not desired.</p>
<h1 id="examples-7" class="section-header"><a href="#examples-7">Examples</a></h1>
<p><a href="../fs/struct.File.html"><code>File</code></a>s implement <code>Read</code>:</p>

<pre class="rust rust-example-rendered">
<span class="attribute">#![<span class="ident">feature</span>(<span class="ident">io</span>)]</span>
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">io</span>;
<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">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">f</span> <span class="op">=</span> <span class="ident">File</span>::<span class="ident">open</span>(<span class="string">&quot;foo.txt&quot;</span>)<span class="question-mark">?</span>;

    <span class="kw">for</span> <span class="ident">c</span> <span class="kw">in</span> <span class="ident">f</span>.<span class="ident">chars</span>() {
        <span class="macro">println</span><span class="macro">!</span>(<span class="string">&quot;{}&quot;</span>, <span class="ident">c</span>.<span class="ident">unwrap</span>());
    }
    <span class="prelude-val">Ok</span>(())
}<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0A%23!%5Bfeature(io)%5D%0Ause%20std%3A%3Aio%3B%0Ause%20std%3A%3Aio%3A%3Aprelude%3A%3A*%3B%0Ause%20std%3A%3Afs%3A%3AFile%3B%0A%0Afn%20main()%20-%3E%20io%3A%3AResult%3C()%3E%20%7B%0A%20%20%20%20let%20mut%20f%20%3D%20File%3A%3Aopen(%22foo.txt%22)%3F%3B%0A%0A%20%20%20%20for%20c%20in%20f.chars()%20%7B%0A%20%20%20%20%20%20%20%20println!(%22%7B%7D%22%2C%20c.unwrap())%3B%0A%20%20%20%20%7D%0A%20%20%20%20Ok(())%0A%7D&amp;version=nightly">Run</a></pre>
</div><div class="important-traits"><div class='tooltip'>ⓘ<span class='tooltiptext'>Important traits for <a class="struct" href="../../std/io/struct.Chain.html" title="struct std::io::Chain">Chain</a>&lt;T, U&gt;</span></div><div class="content hidden"><h3 class="important">Important traits for <a class="struct" href="../../std/io/struct.Chain.html" title="struct std::io::Chain">Chain</a>&lt;T, U&gt;</h3><code class="content"><span class="where fmt-newline">impl&lt;T:&nbsp;<a class="trait" href="../../std/io/trait.Read.html" title="trait std::io::Read">Read</a>, U:&nbsp;<a class="trait" href="../../std/io/trait.Read.html" title="trait std::io::Read">Read</a>&gt; <a class="trait" href="../../std/io/trait.Read.html" title="trait std::io::Read">Read</a> for <a class="struct" href="../../std/io/struct.Chain.html" title="struct std::io::Chain">Chain</a>&lt;T, U&gt;</span></code></div></div><h3 id='method.chain' class='method'><span id='chain.v' class='invisible'><code>fn <a href='#method.chain' class='fnname'>chain</a>&lt;R:&nbsp;<a class="trait" href="../../std/io/trait.Read.html" title="trait std::io::Read">Read</a>&gt;(self, next: R) -&gt; <a class="struct" href="../../std/io/struct.Chain.html" title="struct std::io::Chain">Chain</a>&lt;Self, R&gt; <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 an adaptor which will chain this stream with another.</p>
<p>The returned <code>Read</code> instance will first read all bytes from this object
until EOF is encountered. Afterwards the output is equivalent to the
output of <code>next</code>.</p>
<h1 id="examples-8" class="section-header"><a href="#examples-8">Examples</a></h1>
<p><a href="../fs/struct.File.html"><code>File</code></a>s implement <code>Read</code>:</p>

<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">io</span>;
<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">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">f1</span> <span class="op">=</span> <span class="ident">File</span>::<span class="ident">open</span>(<span class="string">&quot;foo.txt&quot;</span>)<span class="question-mark">?</span>;
    <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">f2</span> <span class="op">=</span> <span class="ident">File</span>::<span class="ident">open</span>(<span class="string">&quot;bar.txt&quot;</span>)<span class="question-mark">?</span>;

    <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">handle</span> <span class="op">=</span> <span class="ident">f1</span>.<span class="ident">chain</span>(<span class="ident">f2</span>);
    <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">buffer</span> <span class="op">=</span> <span class="ident">String</span>::<span class="ident">new</span>();

    <span class="comment">// read the value into a String. We could use any Read method here,</span>
    <span class="comment">// this is just one example.</span>
    <span class="ident">handle</span>.<span class="ident">read_to_string</span>(<span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="ident">buffer</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%3B%0Ause%20std%3A%3Aio%3A%3Aprelude%3A%3A*%3B%0Ause%20std%3A%3Afs%3A%3AFile%3B%0A%0Afn%20main()%20-%3E%20io%3A%3AResult%3C()%3E%20%7B%0A%20%20%20%20let%20mut%20f1%20%3D%20File%3A%3Aopen(%22foo.txt%22)%3F%3B%0A%20%20%20%20let%20mut%20f2%20%3D%20File%3A%3Aopen(%22bar.txt%22)%3F%3B%0A%0A%20%20%20%20let%20mut%20handle%20%3D%20f1.chain(f2)%3B%0A%20%20%20%20let%20mut%20buffer%20%3D%20String%3A%3Anew()%3B%0A%0A%20%20%20%20%2F%2F%20read%20the%20value%20into%20a%20String.%20We%20could%20use%20any%20Read%20method%20here%2C%0A%20%20%20%20%2F%2F%20this%20is%20just%20one%20example.%0A%20%20%20%20handle.read_to_string(%26mut%20buffer)%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="struct" href="../../std/io/struct.Take.html" title="struct std::io::Take">Take</a>&lt;T&gt;</span></div><div class="content hidden"><h3 class="important">Important traits for <a class="struct" href="../../std/io/struct.Take.html" title="struct std::io::Take">Take</a>&lt;T&gt;</h3><code class="content"><span class="where fmt-newline">impl&lt;T:&nbsp;<a class="trait" href="../../std/io/trait.Read.html" title="trait std::io::Read">Read</a>&gt; <a class="trait" href="../../std/io/trait.Read.html" title="trait std::io::Read">Read</a> for <a class="struct" href="../../std/io/struct.Take.html" title="struct std::io::Take">Take</a>&lt;T&gt;</span></code></div></div><h3 id='method.take' class='method'><span id='take.v' class='invisible'><code>fn <a href='#method.take' class='fnname'>take</a>(self, limit: <a class="primitive" href="../primitive.u64.html">u64</a>) -&gt; <a class="struct" href="../../std/io/struct.Take.html" title="struct std::io::Take">Take</a>&lt;Self&gt; <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 an adaptor which will read at most <code>limit</code> bytes from it.</p>
<p>This function returns a new instance of <code>Read</code> which will read at most
<code>limit</code> bytes, after which it will always return EOF (<a href="../../std/result/enum.Result.html#variant.Ok"><code>Ok(0)</code></a>). Any
read errors will not count towards the number of bytes read and future
calls to <a href="trait.Read.html#tymethod.read"><code>read()</code></a> may succeed.</p>
<h1 id="examples-9" class="section-header"><a href="#examples-9">Examples</a></h1>
<p><a href="../fs/struct.File.html"><code>File</code></a>s implement <code>Read</code>:</p>

<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">io</span>;
<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">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">f</span> <span class="op">=</span> <span class="ident">File</span>::<span class="ident">open</span>(<span class="string">&quot;foo.txt&quot;</span>)<span class="question-mark">?</span>;
    <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">buffer</span> <span class="op">=</span> [<span class="number">0</span>; <span class="number">5</span>];

    <span class="comment">// read at most five bytes</span>
    <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">handle</span> <span class="op">=</span> <span class="ident">f</span>.<span class="ident">take</span>(<span class="number">5</span>);

    <span class="ident">handle</span>.<span class="ident">read</span>(<span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="ident">buffer</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%3B%0Ause%20std%3A%3Aio%3A%3Aprelude%3A%3A*%3B%0Ause%20std%3A%3Afs%3A%3AFile%3B%0A%0Afn%20main()%20-%3E%20io%3A%3AResult%3C()%3E%20%7B%0A%20%20%20%20let%20mut%20f%20%3D%20File%3A%3Aopen(%22foo.txt%22)%3F%3B%0A%20%20%20%20let%20mut%20buffer%20%3D%20%5B0%3B%205%5D%3B%0A%0A%20%20%20%20%2F%2F%20read%20at%20most%20five%20bytes%0A%20%20%20%20let%20mut%20handle%20%3D%20f.take(5)%3B%0A%0A%20%20%20%20handle.read(%26mut%20buffer)%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 Read 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#606-615' 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; Read 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#630-639' title='goto source code'>[src]</a></div></td></tr></tbody></table></li>
<li><table class='table-display'><tbody><tr><td><code>impl&lt;R:&nbsp;<a class="trait" href="../../std/io/trait.Read.html" title="trait std::io::Read">Read</a>&gt; Read for <a class="struct" href="../../std/io/struct.BufReader.html" title="struct std::io::BufReader">BufReader</a>&lt;R&gt;</code><td><div class='out-of-band'><a class='srclink' href='../../src/std/io/buffered.rs.html#258-278' title='goto source code'>[src]</a></div></td></tr></tbody></table></li>
<li><table class='table-display'><tbody><tr><td><code>impl&lt;T&gt; Read for <a class="struct" href="../../std/io/struct.Cursor.html" title="struct std::io::Cursor">Cursor</a>&lt;T&gt; <span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;T: <a class="trait" href="../../std/convert/trait.AsRef.html" title="trait std::convert::AsRef">AsRef</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;,&nbsp;</span></code><td><div class='out-of-band'><a class='srclink' href='../../src/std/io/cursor.rs.html#225-243' title='goto source code'>[src]</a></div></td></tr></tbody></table></li>
<li><table class='table-display'><tbody><tr><td><code>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; Read for <a class="primitive" href="../primitive.reference.html">&amp;'a mut </a>R</code><td><div class='out-of-band'><a class='srclink' href='../../src/std/io/impls.rs.html#20-45' title='goto source code'>[src]</a></div></td></tr></tbody></table></li>
<li><table class='table-display'><tbody><tr><td><code>impl&lt;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; Read for <a class="struct" href="../../std/boxed/struct.Box.html" title="struct std::boxed::Box">Box</a>&lt;R&gt;</code><td><div class='out-of-band'><a class='srclink' href='../../src/std/io/impls.rs.html#89-114' 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; Read for <a class="primitive" href="../primitive.slice.html">&amp;'a [</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#165-217' title='goto source code'>[src]</a></div></td></tr></tbody></table></li>
<li><table class='table-display'><tbody><tr><td><code>impl Read for <a class="struct" href="../../std/io/struct.Empty.html" title="struct std::io::Empty">Empty</a></code><td><div class='out-of-band'><a class='srclink' href='../../src/std/io/util.rs.html#100-108' title='goto source code'>[src]</a></div></td></tr></tbody></table></li>
<li><table class='table-display'><tbody><tr><td><code>impl Read for <a class="struct" href="../../std/io/struct.Repeat.html" title="struct std::io::Repeat">Repeat</a></code><td><div class='out-of-band'><a class='srclink' href='../../src/std/io/util.rs.html#151-164' title='goto source code'>[src]</a></div></td></tr></tbody></table></li>
<li><table class='table-display'><tbody><tr><td><code>impl Read for <a class="struct" href="../../std/io/struct.Stdin.html" title="struct std::io::Stdin">Stdin</a></code><td><div class='out-of-band'><a class='srclink' href='../../src/std/io/stdio.rs.html#287-304' 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; Read for <a class="struct" href="../../std/io/struct.StdinLock.html" title="struct std::io::StdinLock">StdinLock</a>&lt;'a&gt;</code><td><div class='out-of-band'><a class='srclink' href='../../src/std/io/stdio.rs.html#307-315' title='goto source code'>[src]</a></div></td></tr></tbody></table></li>
<li><table class='table-display'><tbody><tr><td><code>impl&lt;T:&nbsp;<a class="trait" href="../../std/io/trait.Read.html" title="trait std::io::Read">Read</a>, U:&nbsp;<a class="trait" href="../../std/io/trait.Read.html" title="trait std::io::Read">Read</a>&gt; Read for <a class="struct" href="../../std/io/struct.Chain.html" title="struct std::io::Chain">Chain</a>&lt;T, U&gt;</code><td><div class='out-of-band'><a class='srclink' href='../../src/std/io/mod.rs.html#1741-1760' title='goto source code'>[src]</a></div></td></tr></tbody></table></li>
<li><table class='table-display'><tbody><tr><td><code>impl&lt;T:&nbsp;<a class="trait" href="../../std/io/trait.Read.html" title="trait std::io::Read">Read</a>&gt; Read for <a class="struct" href="../../std/io/struct.Take.html" title="struct std::io::Take">Take</a>&lt;T&gt;</code><td><div class='out-of-band'><a class='srclink' href='../../src/std/io/mod.rs.html#1936-1952' title='goto source code'>[src]</a></div></td></tr></tbody></table></li>
<li><table class='table-display'><tbody><tr><td><code>impl Read 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#579-586' 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; Read 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#593-600' title='goto source code'>[src]</a></div></td></tr></tbody></table></li>
<li><table class='table-display'><tbody><tr><td><code>impl Read for <a class="struct" href="../../std/process/struct.ChildStdout.html" title="struct std::process::ChildStdout">ChildStdout</a></code><td><div class='out-of-band'><a class='srclink' href='../../src/std/process.rs.html#280-288' title='goto source code'>[src]</a></div></td></tr></tbody></table></li>
<li><table class='table-display'><tbody><tr><td><code>impl Read for <a class="struct" href="../../std/process/struct.ChildStderr.html" title="struct std::process::ChildStderr">ChildStderr</a></code><td><div class='out-of-band'><a class='srclink' href='../../src/std/process.rs.html#327-335' title='goto source code'>[src]</a></div></td></tr></tbody></table></li>
<li><table class='table-display'><tbody><tr><td><code>impl Read 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#556-565' 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; Read 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#568-577' 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.Read.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>