Sophie

Sophie

distrib > Mageia > 6 > x86_64 > media > core-updates > by-pkgid > fdedb3cf2140df9b6d419a2338ab1caa > files > 4730

rust-doc-1.25.0-1.mga6.x86_64.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 `BufRead` trait in crate `std`.">
    <meta name="keywords" content="rust, rustlang, rust-lang, BufRead">

    <title>std::io::BufRead - 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="../../main.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 BufRead</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.fill_buf">fill_buf</a><a href="#tymethod.consume">consume</a></div><a class="sidebar-title" href="#provided-methods">Provided Methods</a><div class="sidebar-links"><a href="#method.read_until">read_until</a><a href="#method.read_line">read_line</a><a href="#method.split">split</a><a href="#method.lines">lines</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: 'BufRead', 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">
            </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=''>BufRead</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#1363-1628' title='goto source code'>[src]</a></span></h1>
<pre class='rust trait'>pub trait BufRead: <a class="trait" href="../../std/io/trait.Read.html" title="trait std::io::Read">Read</a> {
    fn <a href='#tymethod.fill_buf' class='fnname'>fill_buf</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.slice.html">&amp;[</a><a class="primitive" href="../primitive.u8.html">u8</a><a class="primitive" href="../primitive.slice.html">]</a>&gt;;
<div class='item-spacer'></div>    fn <a href='#tymethod.consume' class='fnname'>consume</a>(&amp;mut self, amt: <a class="primitive" href="../primitive.usize.html">usize</a>);

    fn <a href='#method.read_until' class='fnname'>read_until</a>(&amp;mut self, byte: <a class="primitive" href="../primitive.u8.html">u8</a>, 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_line' class='fnname'>read_line</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.split' class='fnname'>split</a>(self, byte: <a class="primitive" href="../primitive.u8.html">u8</a>) -&gt; <a class="struct" href="../../std/io/struct.Split.html" title="struct std::io::Split">Split</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.lines' class='fnname'>lines</a>(self) -&gt; <a class="struct" href="../../std/io/struct.Lines.html" title="struct std::io::Lines">Lines</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 class='docblock'><p>A <code>BufRead</code> is a type of <code>Read</code>er which has an internal buffer, allowing it
to perform extra ways of reading.</p>
<p>For example, reading line-by-line is inefficient without using a buffer, so
if you want to read by line, you'll need <code>BufRead</code>, which includes a
<a href="#method.read_line"><code>read_line</code></a> method as well as a <a href="#method.lines"><code>lines</code></a> iterator.</p>
<h1 id="examples" class="section-header"><a href="#examples">Examples</a></h1>
<p>A locked standard input implements <code>BufRead</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">let</span> <span class="ident">stdin</span> <span class="op">=</span> <span class="ident">io</span>::<span class="ident">stdin</span>();
<span class="kw">for</span> <span class="ident">line</span> <span class="kw">in</span> <span class="ident">stdin</span>.<span class="ident">lock</span>().<span class="ident">lines</span>() {
    <span class="macro">println</span><span class="macro">!</span>(<span class="string">&quot;{}&quot;</span>, <span class="ident">line</span>.<span class="ident">unwrap</span>());
}<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Ause%20std%3A%3Aio%3B%0Ause%20std%3A%3Aio%3A%3Aprelude%3A%3A*%3B%0A%0Alet%20stdin%20%3D%20io%3A%3Astdin()%3B%0Afor%20line%20in%20stdin.lock().lines()%20%7B%0A%20%20%20%20println!(%22%7B%7D%22%2C%20line.unwrap())%3B%0A%7D%0A%7D">Run</a></pre>
<p>If you have something that implements <a href="trait.Read.html"><code>Read</code></a>, you can use the <a href="struct.BufReader.html"><code>BufReader</code>
type</a> to turn it into a <code>BufRead</code>.</p>
<p>For example, <a href="../fs/struct.File.html"><code>File</code></a> implements <a href="trait.Read.html"><code>Read</code></a>, but not <code>BufRead</code>.
<a href="struct.BufReader.html"><code>BufReader</code></a> to the rescue!</p>

<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">io</span>::{<span class="self">self</span>, <span class="ident">BufReader</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">let</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="ident">f</span> <span class="op">=</span> <span class="ident">BufReader</span>::<span class="ident">new</span>(<span class="ident">f</span>);

<span class="kw">for</span> <span class="ident">line</span> <span class="kw">in</span> <span class="ident">f</span>.<span class="ident">lines</span>() {
    <span class="macro">println</span><span class="macro">!</span>(<span class="string">&quot;{}&quot;</span>, <span class="ident">line</span>.<span class="ident">unwrap</span>());
}
<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Ause%20std%3A%3Aio%3A%3A%7Bself%2C%20BufReader%7D%3B%0Ause%20std%3A%3Aio%3A%3Aprelude%3A%3A*%3B%0Ause%20std%3A%3Afs%3A%3AFile%3B%0A%0Afn%20foo()%20-%3E%20io%3A%3AResult%3C()%3E%20%7B%0Alet%20f%20%3D%20File%3A%3Aopen(%22foo.txt%22)%3F%3B%0Alet%20f%20%3D%20BufReader%3A%3Anew(f)%3B%0A%0Afor%20line%20in%20f.lines()%20%7B%0A%20%20%20%20println!(%22%7B%7D%22%2C%20line.unwrap())%3B%0A%7D%0A%0AOk(())%0A%7D%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.fill_buf' class='method'><span id='fill_buf.v' class='invisible'><code>fn <a href='#tymethod.fill_buf' class='fnname'>fill_buf</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.slice.html">&amp;[</a><a class="primitive" href="../primitive.u8.html">u8</a><a class="primitive" href="../primitive.slice.html">]</a>&gt;</code></span></h3><div class='docblock'><p>Fills the internal buffer of this object, returning the buffer contents.</p>
<p>This function is a lower-level call. It needs to be paired with the
<a href="#tymethod.consume"><code>consume</code></a> method to function properly. When calling this
method, none of the contents will be &quot;read&quot; in the sense that later
calling <code>read</code> may return the same contents. As such, <a href="#tymethod.consume"><code>consume</code></a> must
be called with the number of bytes that are consumed from this buffer to
ensure that the bytes are never returned twice.</p>
<p>An empty buffer returned indicates that the stream has reached EOF.</p>
<h1 id="errors" class="section-header"><a href="#errors">Errors</a></h1>
<p>This function will return an I/O error if the underlying reader was
read, but returned an error.</p>
<h1 id="examples-1" class="section-header"><a href="#examples-1">Examples</a></h1>
<p>A locked standard input implements <code>BufRead</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">let</span> <span class="ident">stdin</span> <span class="op">=</span> <span class="ident">io</span>::<span class="ident">stdin</span>();
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">stdin</span> <span class="op">=</span> <span class="ident">stdin</span>.<span class="ident">lock</span>();

<span class="comment">// we can&#39;t have two `&amp;mut` references to `stdin`, so use a block</span>
<span class="comment">// to end the borrow early.</span>
<span class="kw">let</span> <span class="ident">length</span> <span class="op">=</span> {
    <span class="kw">let</span> <span class="ident">buffer</span> <span class="op">=</span> <span class="ident">stdin</span>.<span class="ident">fill_buf</span>().<span class="ident">unwrap</span>();

    <span class="comment">// work with buffer</span>
    <span class="macro">println</span><span class="macro">!</span>(<span class="string">&quot;{:?}&quot;</span>, <span class="ident">buffer</span>);

    <span class="ident">buffer</span>.<span class="ident">len</span>()
};

<span class="comment">// ensure the bytes we worked with aren&#39;t returned again later</span>
<span class="ident">stdin</span>.<span class="ident">consume</span>(<span class="ident">length</span>);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Ause%20std%3A%3Aio%3B%0Ause%20std%3A%3Aio%3A%3Aprelude%3A%3A*%3B%0A%0Alet%20stdin%20%3D%20io%3A%3Astdin()%3B%0Alet%20mut%20stdin%20%3D%20stdin.lock()%3B%0A%0A%2F%2F%20we%20can't%20have%20two%20%60%26mut%60%20references%20to%20%60stdin%60%2C%20so%20use%20a%20block%0A%2F%2F%20to%20end%20the%20borrow%20early.%0Alet%20length%20%3D%20%7B%0A%20%20%20%20let%20buffer%20%3D%20stdin.fill_buf().unwrap()%3B%0A%0A%20%20%20%20%2F%2F%20work%20with%20buffer%0A%20%20%20%20println!(%22%7B%3A%3F%7D%22%2C%20buffer)%3B%0A%0A%20%20%20%20buffer.len()%0A%7D%3B%0A%0A%2F%2F%20ensure%20the%20bytes%20we%20worked%20with%20aren't%20returned%20again%20later%0Astdin.consume(length)%3B%0A%7D">Run</a></pre>
</div><h3 id='tymethod.consume' class='method'><span id='consume.v' class='invisible'><code>fn <a href='#tymethod.consume' class='fnname'>consume</a>(&amp;mut self, amt: <a class="primitive" href="../primitive.usize.html">usize</a>)</code></span></h3><div class='docblock'><p>Tells this buffer that <code>amt</code> bytes have been consumed from the buffer,
so they should no longer be returned in calls to <code>read</code>.</p>
<p>This function is a lower-level call. It needs to be paired with the
<a href="#tymethod.fill_buf"><code>fill_buf</code></a> method to function properly. This function does
not perform any I/O, it simply informs this object that some amount of
its buffer, returned from <a href="#tymethod.fill_buf"><code>fill_buf</code></a>, has been consumed and should
no longer be returned. As such, this function may do odd things if
<a href="#tymethod.fill_buf"><code>fill_buf</code></a> isn't called before calling it.</p>
<p>The <code>amt</code> must be <code>&lt;=</code> the number of bytes in the buffer returned by
<a href="#tymethod.fill_buf"><code>fill_buf</code></a>.</p>
<h1 id="examples-2" class="section-header"><a href="#examples-2">Examples</a></h1>
<p>Since <code>consume()</code> is meant to be used with <a href="#tymethod.fill_buf"><code>fill_buf</code></a>,
that method's example includes an example of <code>consume()</code>.</p>
</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.read_until' class='method'><span id='read_until.v' class='invisible'><code>fn <a href='#method.read_until' class='fnname'>read_until</a>(&amp;mut self, byte: <a class="primitive" href="../primitive.u8.html">u8</a>, 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 into <code>buf</code> until the delimiter <code>byte</code> or EOF is reached.</p>
<p>This function will read bytes from the underlying stream until the
delimiter or EOF is found. Once found, all bytes up to, and including,
the delimiter (if found) will be appended to <code>buf</code>.</p>
<p>If successful, this function will return the total number of bytes read.</p>
<p>An empty buffer returned indicates that the stream has reached EOF.</p>
<h1 id="errors-1" class="section-header"><a href="#errors-1">Errors</a></h1>
<p>This function will ignore all instances of <a href="enum.ErrorKind.html#variant.Interrupted"><code>ErrorKind::Interrupted</code></a> and
will otherwise return any errors returned by <a href="#tymethod.fill_buf"><code>fill_buf</code></a>.</p>
<p>If an I/O error is encountered then all bytes read so far will be
present in <code>buf</code> and its length will have been adjusted appropriately.</p>
<h1 id="examples-3" class="section-header"><a href="#examples-3">Examples</a></h1>
<p><a href="struct.Cursor.html"><code>std::io::Cursor</code></a> is a type that implements <code>BufRead</code>. In
this example, we use <a href="struct.Cursor.html"><code>Cursor</code></a> to read all the bytes in a byte slice
in hyphen delimited segments:</p>

<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">io</span>::{<span class="self">self</span>, <span class="ident">BufRead</span>};

<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">cursor</span> <span class="op">=</span> <span class="ident">io</span>::<span class="ident">Cursor</span>::<span class="ident">new</span>(<span class="string">b&quot;lorem-ipsum&quot;</span>);
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">buf</span> <span class="op">=</span> <span class="macro">vec</span><span class="macro">!</span>[];

<span class="comment">// cursor is at &#39;l&#39;</span>
<span class="kw">let</span> <span class="ident">num_bytes</span> <span class="op">=</span> <span class="ident">cursor</span>.<span class="ident">read_until</span>(<span class="string">b&#39;-&#39;</span>, <span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="ident">buf</span>)
    .<span class="ident">expect</span>(<span class="string">&quot;reading from cursor won&#39;t fail&quot;</span>);
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">num_bytes</span>, <span class="number">6</span>);
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">buf</span>, <span class="string">b&quot;lorem-&quot;</span>);
<span class="ident">buf</span>.<span class="ident">clear</span>();

<span class="comment">// cursor is at &#39;i&#39;</span>
<span class="kw">let</span> <span class="ident">num_bytes</span> <span class="op">=</span> <span class="ident">cursor</span>.<span class="ident">read_until</span>(<span class="string">b&#39;-&#39;</span>, <span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="ident">buf</span>)
    .<span class="ident">expect</span>(<span class="string">&quot;reading from cursor won&#39;t fail&quot;</span>);
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">num_bytes</span>, <span class="number">5</span>);
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">buf</span>, <span class="string">b&quot;ipsum&quot;</span>);
<span class="ident">buf</span>.<span class="ident">clear</span>();

<span class="comment">// cursor is at EOF</span>
<span class="kw">let</span> <span class="ident">num_bytes</span> <span class="op">=</span> <span class="ident">cursor</span>.<span class="ident">read_until</span>(<span class="string">b&#39;-&#39;</span>, <span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="ident">buf</span>)
    .<span class="ident">expect</span>(<span class="string">&quot;reading from cursor won&#39;t fail&quot;</span>);
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">num_bytes</span>, <span class="number">0</span>);
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">buf</span>, <span class="string">b&quot;&quot;</span>);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Ause%20std%3A%3Aio%3A%3A%7Bself%2C%20BufRead%7D%3B%0A%0Alet%20mut%20cursor%20%3D%20io%3A%3ACursor%3A%3Anew(b%22lorem-ipsum%22)%3B%0Alet%20mut%20buf%20%3D%20vec!%5B%5D%3B%0A%0A%2F%2F%20cursor%20is%20at%20'l'%0Alet%20num_bytes%20%3D%20cursor.read_until(b'-'%2C%20%26mut%20buf)%0A%20%20%20%20.expect(%22reading%20from%20cursor%20won't%20fail%22)%3B%0Aassert_eq!(num_bytes%2C%206)%3B%0Aassert_eq!(buf%2C%20b%22lorem-%22)%3B%0Abuf.clear()%3B%0A%0A%2F%2F%20cursor%20is%20at%20'i'%0Alet%20num_bytes%20%3D%20cursor.read_until(b'-'%2C%20%26mut%20buf)%0A%20%20%20%20.expect(%22reading%20from%20cursor%20won't%20fail%22)%3B%0Aassert_eq!(num_bytes%2C%205)%3B%0Aassert_eq!(buf%2C%20b%22ipsum%22)%3B%0Abuf.clear()%3B%0A%0A%2F%2F%20cursor%20is%20at%20EOF%0Alet%20num_bytes%20%3D%20cursor.read_until(b'-'%2C%20%26mut%20buf)%0A%20%20%20%20.expect(%22reading%20from%20cursor%20won't%20fail%22)%3B%0Aassert_eq!(num_bytes%2C%200)%3B%0Aassert_eq!(buf%2C%20b%22%22)%3B%0A%7D">Run</a></pre>
</div><h3 id='method.read_line' class='method'><span id='read_line.v' class='invisible'><code>fn <a href='#method.read_line' class='fnname'>read_line</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 a newline (the 0xA byte) is reached, and append
them to the provided buffer.</p>
<p>This function will read bytes from the underlying stream until the
newline delimiter (the 0xA byte) or EOF is found. Once found, all bytes
up to, and including, the delimiter (if found) will be appended to
<code>buf</code>.</p>
<p>If successful, this function will return the total number of bytes read.</p>
<p>An empty buffer returned indicates that the stream has reached EOF.</p>
<h1 id="errors-2" class="section-header"><a href="#errors-2">Errors</a></h1>
<p>This function has the same error semantics as [<code>read_until</code>] and will
also return an error if the read bytes are not valid UTF-8. If an I/O
error is encountered then <code>buf</code> may contain some bytes already read in
the event that all data read so far was valid UTF-8.</p>
<h1 id="examples-4" class="section-header"><a href="#examples-4">Examples</a></h1>
<p><a href="struct.Cursor.html"><code>std::io::Cursor</code></a> is a type that implements <code>BufRead</code>. In
this example, we use <a href="struct.Cursor.html"><code>Cursor</code></a> to read all the lines in a byte slice:</p>

<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">io</span>::{<span class="self">self</span>, <span class="ident">BufRead</span>};

<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">cursor</span> <span class="op">=</span> <span class="ident">io</span>::<span class="ident">Cursor</span>::<span class="ident">new</span>(<span class="string">b&quot;foo\nbar&quot;</span>);
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">buf</span> <span class="op">=</span> <span class="ident">String</span>::<span class="ident">new</span>();

<span class="comment">// cursor is at &#39;f&#39;</span>
<span class="kw">let</span> <span class="ident">num_bytes</span> <span class="op">=</span> <span class="ident">cursor</span>.<span class="ident">read_line</span>(<span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="ident">buf</span>)
    .<span class="ident">expect</span>(<span class="string">&quot;reading from cursor won&#39;t fail&quot;</span>);
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">num_bytes</span>, <span class="number">4</span>);
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">buf</span>, <span class="string">&quot;foo\n&quot;</span>);
<span class="ident">buf</span>.<span class="ident">clear</span>();

<span class="comment">// cursor is at &#39;b&#39;</span>
<span class="kw">let</span> <span class="ident">num_bytes</span> <span class="op">=</span> <span class="ident">cursor</span>.<span class="ident">read_line</span>(<span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="ident">buf</span>)
    .<span class="ident">expect</span>(<span class="string">&quot;reading from cursor won&#39;t fail&quot;</span>);
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">num_bytes</span>, <span class="number">3</span>);
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">buf</span>, <span class="string">&quot;bar&quot;</span>);
<span class="ident">buf</span>.<span class="ident">clear</span>();

<span class="comment">// cursor is at EOF</span>
<span class="kw">let</span> <span class="ident">num_bytes</span> <span class="op">=</span> <span class="ident">cursor</span>.<span class="ident">read_line</span>(<span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="ident">buf</span>)
    .<span class="ident">expect</span>(<span class="string">&quot;reading from cursor won&#39;t fail&quot;</span>);
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">num_bytes</span>, <span class="number">0</span>);
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">buf</span>, <span class="string">&quot;&quot;</span>);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Ause%20std%3A%3Aio%3A%3A%7Bself%2C%20BufRead%7D%3B%0A%0Alet%20mut%20cursor%20%3D%20io%3A%3ACursor%3A%3Anew(b%22foo%5Cnbar%22)%3B%0Alet%20mut%20buf%20%3D%20String%3A%3Anew()%3B%0A%0A%2F%2F%20cursor%20is%20at%20'f'%0Alet%20num_bytes%20%3D%20cursor.read_line(%26mut%20buf)%0A%20%20%20%20.expect(%22reading%20from%20cursor%20won't%20fail%22)%3B%0Aassert_eq!(num_bytes%2C%204)%3B%0Aassert_eq!(buf%2C%20%22foo%5Cn%22)%3B%0Abuf.clear()%3B%0A%0A%2F%2F%20cursor%20is%20at%20'b'%0Alet%20num_bytes%20%3D%20cursor.read_line(%26mut%20buf)%0A%20%20%20%20.expect(%22reading%20from%20cursor%20won't%20fail%22)%3B%0Aassert_eq!(num_bytes%2C%203)%3B%0Aassert_eq!(buf%2C%20%22bar%22)%3B%0Abuf.clear()%3B%0A%0A%2F%2F%20cursor%20is%20at%20EOF%0Alet%20num_bytes%20%3D%20cursor.read_line(%26mut%20buf)%0A%20%20%20%20.expect(%22reading%20from%20cursor%20won't%20fail%22)%3B%0Aassert_eq!(num_bytes%2C%200)%3B%0Aassert_eq!(buf%2C%20%22%22)%3B%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.Split.html" title="struct std::io::Split">Split</a>&lt;B&gt;</span></div><div class="content hidden"><h3 class="important">Important traits for <a class="struct" href="../../std/io/struct.Split.html" title="struct std::io::Split">Split</a>&lt;B&gt;</h3><code class="content"><span class="where fmt-newline">impl&lt;B:&nbsp;<a class="trait" href="../../std/io/trait.BufRead.html" title="trait std::io::BufRead">BufRead</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.Split.html" title="struct std::io::Split">Split</a>&lt;B&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="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;;</span></code></div></div><h3 id='method.split' class='method'><span id='split.v' class='invisible'><code>fn <a href='#method.split' class='fnname'>split</a>(self, byte: <a class="primitive" href="../primitive.u8.html">u8</a>) -&gt; <a class="struct" href="../../std/io/struct.Split.html" title="struct std::io::Split">Split</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>Returns an iterator over the contents of this reader split on the byte
<code>byte</code>.</p>
<p>The iterator returned from this function will return instances of
<a href="type.Result.html"><code>io::Result</code></a><code>&lt;</code><a href="../vec/struct.Vec.html"><code>Vec&lt;u8&gt;</code></a><code>&gt;</code>. Each vector returned will <em>not</em> have
the delimiter byte at the end.</p>
<p>This function will yield errors whenever <a href="#method.read_until"><code>read_until</code></a> would have
also yielded an error.</p>
<h1 id="examples-5" class="section-header"><a href="#examples-5">Examples</a></h1>
<p><a href="struct.Cursor.html"><code>std::io::Cursor</code></a> is a type that implements <code>BufRead</code>. In
this example, we use <a href="struct.Cursor.html"><code>Cursor</code></a> to iterate over all hyphen delimited
segments in a byte slice</p>

<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">io</span>::{<span class="self">self</span>, <span class="ident">BufRead</span>};

<span class="kw">let</span> <span class="ident">cursor</span> <span class="op">=</span> <span class="ident">io</span>::<span class="ident">Cursor</span>::<span class="ident">new</span>(<span class="string">b&quot;lorem-ipsum-dolor&quot;</span>);

<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">split_iter</span> <span class="op">=</span> <span class="ident">cursor</span>.<span class="ident">split</span>(<span class="string">b&#39;-&#39;</span>).<span class="ident">map</span>(<span class="op">|</span><span class="ident">l</span><span class="op">|</span> <span class="ident">l</span>.<span class="ident">unwrap</span>());
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">split_iter</span>.<span class="ident">next</span>(), <span class="prelude-val">Some</span>(<span class="string">b&quot;lorem&quot;</span>.<span class="ident">to_vec</span>()));
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">split_iter</span>.<span class="ident">next</span>(), <span class="prelude-val">Some</span>(<span class="string">b&quot;ipsum&quot;</span>.<span class="ident">to_vec</span>()));
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">split_iter</span>.<span class="ident">next</span>(), <span class="prelude-val">Some</span>(<span class="string">b&quot;dolor&quot;</span>.<span class="ident">to_vec</span>()));
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">split_iter</span>.<span class="ident">next</span>(), <span class="prelude-val">None</span>);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Ause%20std%3A%3Aio%3A%3A%7Bself%2C%20BufRead%7D%3B%0A%0Alet%20cursor%20%3D%20io%3A%3ACursor%3A%3Anew(b%22lorem-ipsum-dolor%22)%3B%0A%0Alet%20mut%20split_iter%20%3D%20cursor.split(b'-').map(%7Cl%7C%20l.unwrap())%3B%0Aassert_eq!(split_iter.next()%2C%20Some(b%22lorem%22.to_vec()))%3B%0Aassert_eq!(split_iter.next()%2C%20Some(b%22ipsum%22.to_vec()))%3B%0Aassert_eq!(split_iter.next()%2C%20Some(b%22dolor%22.to_vec()))%3B%0Aassert_eq!(split_iter.next()%2C%20None)%3B%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.Lines.html" title="struct std::io::Lines">Lines</a>&lt;B&gt;</span></div><div class="content hidden"><h3 class="important">Important traits for <a class="struct" href="../../std/io/struct.Lines.html" title="struct std::io::Lines">Lines</a>&lt;B&gt;</h3><code class="content"><span class="where fmt-newline">impl&lt;B:&nbsp;<a class="trait" href="../../std/io/trait.BufRead.html" title="trait std::io::BufRead">BufRead</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.Lines.html" title="struct std::io::Lines">Lines</a>&lt;B&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="struct" href="../../std/string/struct.String.html" title="struct std::string::String">String</a>&gt;;</span></code></div></div><h3 id='method.lines' class='method'><span id='lines.v' class='invisible'><code>fn <a href='#method.lines' class='fnname'>lines</a>(self) -&gt; <a class="struct" href="../../std/io/struct.Lines.html" title="struct std::io::Lines">Lines</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>Returns an iterator over the lines of this reader.</p>
<p>The iterator returned from this function will yield instances of
<a href="type.Result.html"><code>io::Result</code></a><code>&lt;</code><a href="../string/struct.String.html"><code>String</code></a><code>&gt;</code>. Each string returned will <em>not</em> have a newline
byte (the 0xA byte) or CRLF (0xD, 0xA bytes) at the end.</p>
<h1 id="examples-6" class="section-header"><a href="#examples-6">Examples</a></h1>
<p><a href="struct.Cursor.html"><code>std::io::Cursor</code></a> is a type that implements <code>BufRead</code>. In
this example, we use <a href="struct.Cursor.html"><code>Cursor</code></a> to iterate over all the lines in a byte
slice.</p>

<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">io</span>::{<span class="self">self</span>, <span class="ident">BufRead</span>};

<span class="kw">let</span> <span class="ident">cursor</span> <span class="op">=</span> <span class="ident">io</span>::<span class="ident">Cursor</span>::<span class="ident">new</span>(<span class="string">b&quot;lorem\nipsum\r\ndolor&quot;</span>);

<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">lines_iter</span> <span class="op">=</span> <span class="ident">cursor</span>.<span class="ident">lines</span>().<span class="ident">map</span>(<span class="op">|</span><span class="ident">l</span><span class="op">|</span> <span class="ident">l</span>.<span class="ident">unwrap</span>());
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">lines_iter</span>.<span class="ident">next</span>(), <span class="prelude-val">Some</span>(<span class="ident">String</span>::<span class="ident">from</span>(<span class="string">&quot;lorem&quot;</span>)));
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">lines_iter</span>.<span class="ident">next</span>(), <span class="prelude-val">Some</span>(<span class="ident">String</span>::<span class="ident">from</span>(<span class="string">&quot;ipsum&quot;</span>)));
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">lines_iter</span>.<span class="ident">next</span>(), <span class="prelude-val">Some</span>(<span class="ident">String</span>::<span class="ident">from</span>(<span class="string">&quot;dolor&quot;</span>)));
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">lines_iter</span>.<span class="ident">next</span>(), <span class="prelude-val">None</span>);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Ause%20std%3A%3Aio%3A%3A%7Bself%2C%20BufRead%7D%3B%0A%0Alet%20cursor%20%3D%20io%3A%3ACursor%3A%3Anew(b%22lorem%5Cnipsum%5Cr%5Cndolor%22)%3B%0A%0Alet%20mut%20lines_iter%20%3D%20cursor.lines().map(%7Cl%7C%20l.unwrap())%3B%0Aassert_eq!(lines_iter.next()%2C%20Some(String%3A%3Afrom(%22lorem%22)))%3B%0Aassert_eq!(lines_iter.next()%2C%20Some(String%3A%3Afrom(%22ipsum%22)))%3B%0Aassert_eq!(lines_iter.next()%2C%20Some(String%3A%3Afrom(%22dolor%22)))%3B%0Aassert_eq!(lines_iter.next()%2C%20None)%3B%0A%7D">Run</a></pre>
<h1 id="errors-3" class="section-header"><a href="#errors-3">Errors</a></h1>
<p>Each line of the iterator has the same error semantics as <a href="trait.BufRead.html#method.read_line"><code>BufRead::read_line</code></a>.</p>
</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><div class='out-of-band'><a class='srclink' href='../../src/std/io/buffered.rs.html#246-263' title='goto source code'>[src]</a></div><code>impl&lt;R:&nbsp;<a class="trait" href="../../std/io/trait.Read.html" title="trait std::io::Read">Read</a>&gt; BufRead for <a class="struct" href="../../std/io/struct.BufReader.html" title="struct std::io::BufReader">BufReader</a>&lt;R&gt;</code></li>
<li><div class='out-of-band'><a class='srclink' href='../../src/std/io/cursor.rs.html#247-253' title='goto source code'>[src]</a></div><code>impl&lt;T&gt; BufRead 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></li>
<li><div class='out-of-band'><a class='srclink' href='../../src/std/io/impls.rs.html#70-86' title='goto source code'>[src]</a></div><code>impl&lt;'a, B:&nbsp;<a class="trait" href="../../std/io/trait.BufRead.html" title="trait std::io::BufRead">BufRead</a> + ?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>&gt; BufRead for <a class="primitive" href="../primitive.reference.html">&amp;'a mut </a>B</code></li>
<li><div class='out-of-band'><a class='srclink' href='../../src/std/io/impls.rs.html#139-155' title='goto source code'>[src]</a></div><code>impl&lt;B:&nbsp;<a class="trait" href="../../std/io/trait.BufRead.html" title="trait std::io::BufRead">BufRead</a> + ?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>&gt; BufRead for <a class="struct" href="../../std/boxed/struct.Box.html" title="struct std::boxed::Box">Box</a>&lt;B&gt;</code></li>
<li><div class='out-of-band'><a class='srclink' href='../../src/std/io/impls.rs.html#220-226' title='goto source code'>[src]</a></div><code>impl&lt;'a&gt; BufRead 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></li>
<li><div class='out-of-band'><a class='srclink' href='../../src/std/io/util.rs.html#111-116' title='goto source code'>[src]</a></div><code>impl BufRead for <a class="struct" href="../../std/io/struct.Empty.html" title="struct std::io::Empty">Empty</a></code></li>
<li><div class='out-of-band'><a class='srclink' href='../../src/std/io/stdio.rs.html#318-321' title='goto source code'>[src]</a></div><code>impl&lt;'a&gt; BufRead for <a class="struct" href="../../std/io/struct.StdinLock.html" title="struct std::io::StdinLock">StdinLock</a>&lt;'a&gt;</code></li>
<li><div class='out-of-band'><a class='srclink' href='../../src/std/io/mod.rs.html#1751-1769' title='goto source code'>[src]</a></div><code>impl&lt;T:&nbsp;<a class="trait" href="../../std/io/trait.BufRead.html" title="trait std::io::BufRead">BufRead</a>, U:&nbsp;<a class="trait" href="../../std/io/trait.BufRead.html" title="trait std::io::BufRead">BufRead</a>&gt; BufRead for <a class="struct" href="../../std/io/struct.Chain.html" title="struct std::io::Chain">Chain</a>&lt;T, U&gt;</code></li>
<li><div class='out-of-band'><a class='srclink' href='../../src/std/io/mod.rs.html#1944-1962' title='goto source code'>[src]</a></div><code>impl&lt;T:&nbsp;<a class="trait" href="../../std/io/trait.BufRead.html" title="trait std::io::BufRead">BufRead</a>&gt; BufRead for <a class="struct" href="../../std/io/struct.Take.html" title="struct std::io::Take">Take</a>&lt;T&gt;</code></li>
</ul><script type="text/javascript" async
                         src="../../implementors/std/io/trait.BufRead.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>
            </div>
        </div>
    </aside>

    

    <script>
        window.rootPath = "../../";
        window.currentCrate = "std";
    </script>
    <script src="../../main.js"></script>
    <script defer src="../../search-index.js"></script>
</body>
</html>