<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
    <channel>
      <title>Snoyberg - Michael Snoyman - rust-crash-course</title>
        <link>https://www.snoyman.com</link>
        <description>Michael Snoyman&#x27;s homepage and blog. Eclectic collection of programming (mostly Rust and Haskell) and lifting (weights, children, and monads).</description>
        <generator>Zola</generator>
        <language>en</language>
        <atom:link href="https://www.snoyman.com/series/rust-crash-course/rss.xml" rel="self" type="application/rss+xml"/>
        <lastBuildDate>Thu, 05 Dec 2019 00:00:00 +0000</lastBuildDate>
        <item>
            <title>Tokio 0.2 - Rust Crash Course lesson 9</title>
            <pubDate>Thu, 05 Dec 2019 00:00:00 +0000</pubDate>
            <link>https://www.snoyman.com/blog/2019/12/rust-crash-course-09-tokio-0-2/</link>
            <guid>https://www.snoyman.com/blog/2019/12/rust-crash-course-09-tokio-0-2/</guid>
            <description>&lt;p&gt;&lt;strong&gt;Heads up&lt;&#x2F;strong&gt; This blog post series has been updated and published as an eBook by FP Complete. I&#x27;d recommend reading that version instead of these posts. If you&#x27;re interested, please check out the &lt;a href=&quot;https:&#x2F;&#x2F;www.fpcomplete.com&#x2F;rust&#x2F;crash-course&#x2F;&quot;&gt;Rust Crash Course eBook&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;In the &lt;a href=&quot;&#x2F;blog&#x2F;2019&#x2F;12&#x2F;rust-crash-course-08-down-dirty-future&quot;&gt;previous lesson in the crash course&lt;&#x2F;a&gt;, we covered the new &lt;code&gt;async&#x2F;.await&lt;&#x2F;code&gt; syntax stabilized in Rust 1.39, and the &lt;code&gt;Future&lt;&#x2F;code&gt; trait which lives underneath it. This information greatly supercedes the now-defunct lesson 7 from last year, which covered the older &lt;code&gt;Future&lt;&#x2F;code&gt; approach.&lt;&#x2F;p&gt;
&lt;p&gt;Now it&#x27;s time to update the second half of lesson 7, and teach the hot-off-the-presses Tokio 0.2 release. For those not familiar with it, let me quote the project&#x27;s overview:&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;Tokio is an event-driven, non-blocking I&#x2F;O platform for writing asynchronous applications with the Rust programming language.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;If you want to write an efficient, concurrent network service in Rust, you&#x27;ll want to use something like Tokio. That&#x27;s not to say that this is the only use case for Tokio; you can do lots of great things with an event driven scheduler outside of network services. It&#x27;s also not to say that Tokio is the only solution; the &lt;a href=&quot;https:&#x2F;&#x2F;async.rs&#x2F;&quot;&gt;&lt;code&gt;async-std&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; library provides similar functionality.&lt;&#x2F;p&gt;
&lt;p&gt;However, network services are likely the most common domain agitating for a non-blocking I&#x2F;O system. And Tokio is the most popular and established of these systems today. So this combination is where we&#x27;re going to get started.&lt;&#x2F;p&gt;
&lt;p&gt;And as a side note, if you have some other topic you&#x27;d like me to cover around this, please &lt;a href=&quot;https:&#x2F;&#x2F;twitter.com&#x2F;snoyberg&quot;&gt;let me know on Twitter&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;em&gt;Exercise solutions will be included at the end of the blog post. Yes, I keep changing the rules, sue me.&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;p&gt;This post is part of a series based on &lt;a href=&quot;https:&#x2F;&#x2F;tech.fpcomplete.com&#x2F;rust&quot;&gt;teaching Rust at FP
Complete&lt;&#x2F;a&gt;. If you&#x27;re reading this post outside
of the blog, you can find links to all posts in the series &lt;a href=&quot;https:&#x2F;&#x2F;www.snoyman.com&#x2F;blog&#x2F;2018&#x2F;10&#x2F;introducing-rust-crash-course&quot;&gt;at the top of the
introduction
post&lt;&#x2F;a&gt;. You
can also &lt;a href=&quot;https:&#x2F;&#x2F;www.snoyman.com&#x2F;feed&#x2F;rust-crash-course&quot;&gt;subscribe to the RSS
feed&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;hello-tokio&quot;&gt;Hello Tokio!&lt;&#x2F;h2&gt;
&lt;p&gt;Let&#x27;s kick this off. Go ahead and create a new Rust project for experimenting:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-shell&quot; data-lang=&quot;shell&quot;&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;$ cargo new --bin usetokio
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If you want to make sure you&#x27;re using the same compiler version as me, set up your &lt;code&gt;rust-toolchain&lt;&#x2F;code&gt; correctly:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-shell&quot; data-lang=&quot;shell&quot;&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;$ echo 1.39.0 &amp;gt; rust-toolchain
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And then set up Tokio as a dependency. For simplicity, we&#x27;ll install all the bells and whistles. In your &lt;code&gt;Cargo.toml&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-toml&quot; data-lang=&quot;toml&quot;&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;dependencies&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;tokio &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= { &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;version &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;0.2&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;features &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;full&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;] }
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;strong&gt;PROTIP&lt;&#x2F;strong&gt; You can run &lt;code&gt;cargo build&lt;&#x2F;code&gt; now to kick off the download and build of crates while you keep reading...&lt;&#x2F;p&gt;
&lt;p&gt;And now we&#x27;re going to write an asynchronous hello world application. Type this into your &lt;code&gt;src&#x2F;main.rs&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::io;

#[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;tokio&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;]
async &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Result&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;(), std::io::Error&amp;gt; {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; stdout = io::stdout();
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; hello: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u8&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;] = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;b&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Hello, world!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#dc322f;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
    io::copy(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; hello, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; stdout).await&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Ok&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(())
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;strong&gt;NOTE&lt;&#x2F;strong&gt; I specifically said &amp;quot;type this in&amp;quot; instead of &amp;quot;copy and paste.&amp;quot; For getting comfortable with this stuff, I recommend manually typing in the code.&lt;&#x2F;p&gt;
&lt;p&gt;A lot of this should look familiar from our previous lesson. To recap:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Since we&#x27;ll be &lt;code&gt;await&lt;&#x2F;code&gt;ing something and generating a &lt;code&gt;Future&lt;&#x2F;code&gt;, our &lt;code&gt;main&lt;&#x2F;code&gt; function is &lt;code&gt;async&lt;&#x2F;code&gt;.&lt;&#x2F;li&gt;
&lt;li&gt;Since &lt;code&gt;main&lt;&#x2F;code&gt; is &lt;code&gt;async&lt;&#x2F;code&gt;, we need to use an executor to run it. That&#x27;s why we use the &lt;code&gt;#[tokio::main]&lt;&#x2F;code&gt; attribute.&lt;&#x2F;li&gt;
&lt;li&gt;Since performing I&#x2F;O can fail, we return a &lt;code&gt;Result&lt;&#x2F;code&gt;.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;The first really new thing since last lesson is this little bit of syntax:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;.await&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;?
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;I mentioned it last time, but now we&#x27;re seeing it in real life. This is just the combination of our two pieces of prior art: &lt;code&gt;.await&lt;&#x2F;code&gt; for chaining together &lt;code&gt;Future&lt;&#x2F;code&gt;s, and &lt;code&gt;?&lt;&#x2F;code&gt; for error handling. The fact that these work together so nicely is really awesome. I&#x27;ll probably mention this a few more times, because I love it that much.&lt;&#x2F;p&gt;
&lt;p&gt;The next thing to note is that we use &lt;code&gt;tokio::io::stdout()&lt;&#x2F;code&gt; to get access to some value that lets us interact with standard output. If you&#x27;re familiar with it, this looks really similar to &lt;code&gt;std::io::stdout()&lt;&#x2F;code&gt;. That&#x27;s by design: a large part of the &lt;code&gt;tokio&lt;&#x2F;code&gt; API is simply async-ifying things from &lt;code&gt;std&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;And finally, we can look at the actual &lt;code&gt;tokio::io::copy&lt;&#x2F;code&gt; call. As you may have guessed, and as stated in the &lt;a href=&quot;https:&#x2F;&#x2F;docs.rs&#x2F;tokio&#x2F;0.2.2&#x2F;tokio&#x2F;io&#x2F;fn.copy.html&quot;&gt;API docs&lt;&#x2F;a&gt;:&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;This is an asynchronous version of &lt;a href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;std&#x2F;io&#x2F;fn.copy.html&quot;&gt;&lt;code&gt;std::io::copy&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;However, instead of working with the &lt;code&gt;Read&lt;&#x2F;code&gt; and &lt;code&gt;Write&lt;&#x2F;code&gt; traits, this works with their async cousins: &lt;code&gt;AsyncRead&lt;&#x2F;code&gt; and &lt;code&gt;AsyncWrite&lt;&#x2F;code&gt;. A byte slice (&lt;code&gt;&amp;amp;[u8]&lt;&#x2F;code&gt;) is a valid &lt;code&gt;AsyncRead&lt;&#x2F;code&gt;, so we&#x27;re able to store our input there. And as you may have guessed, &lt;code&gt;Stdout&lt;&#x2F;code&gt; is an &lt;code&gt;AsyncWrite&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;EXERCISE 1&lt;&#x2F;strong&gt; Modify this application so that instead of printing &amp;quot;Hello, world!&amp;quot;, it copies the entire contents of standard input to standard output.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;NOTE&lt;&#x2F;strong&gt; You can simplify this code using &lt;code&gt;stdout.write_all&lt;&#x2F;code&gt; after &lt;code&gt;use&lt;&#x2F;code&gt;ing &lt;code&gt;tokio::io::AsyncWriteExt&lt;&#x2F;code&gt;, but we&#x27;ll stick to &lt;code&gt;tokio::io::copy&lt;&#x2F;code&gt;, since we&#x27;ll be using it throughout. But if you&#x27;re curious:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::io::{&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, AsyncWriteExt};

#[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;tokio&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;]
async &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Result&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;(), std::io::Error&amp;gt; {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; stdout = io::stdout();
    stdout.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;write_all&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;b&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Hello, world!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#dc322f;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;).await&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Ok&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(())
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;spawning-processes&quot;&gt;Spawning processes&lt;&#x2F;h2&gt;
&lt;p&gt;Tokio provides a &lt;code&gt;tokio::process&lt;&#x2F;code&gt; module which resembles the &lt;code&gt;std::process&lt;&#x2F;code&gt; module. We can use this to implement Hello World once again:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::process::Command;

#[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;tokio&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;]
async &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Result&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;(), std::io::Error&amp;gt; {
    Command::new(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;echo&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;arg&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Hello, world!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;spawn&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;.await&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Ok&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(())
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Notice how the &lt;code&gt;?&lt;&#x2F;code&gt; and &lt;code&gt;.await&lt;&#x2F;code&gt; bits can go in whatever order they are needed. You can read this line as:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Create a new &lt;code&gt;Command&lt;&#x2F;code&gt; to run &lt;code&gt;echo&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;Give it the argument &lt;code&gt;&amp;quot;Hello, world!&amp;quot;&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;Spawn this, which may fail&lt;&#x2F;li&gt;
&lt;li&gt;Using the first &lt;code&gt;?&lt;&#x2F;code&gt;: if it fails, return the error. Otherwise, return a &lt;code&gt;Future&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;Using the &lt;code&gt;.await&lt;&#x2F;code&gt;: wait until that &lt;code&gt;Future&lt;&#x2F;code&gt; completes, and capture its &lt;code&gt;Result&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;Using the second &lt;code&gt;?&lt;&#x2F;code&gt;: if that &lt;code&gt;Result&lt;&#x2F;code&gt; is &lt;code&gt;Err&lt;&#x2F;code&gt;, return that error.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;Pretty nice for a single line!&lt;&#x2F;p&gt;
&lt;p&gt;One of the great advantages of &lt;code&gt;async&lt;&#x2F;code&gt;&#x2F;&lt;code&gt;.await&lt;&#x2F;code&gt; versus the previous way of doing async with callbacks is how easily it works with looping.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;EXERCISE 2&lt;&#x2F;strong&gt; Extend this example so that it prints &lt;code&gt;Hello, world!&lt;&#x2F;code&gt; 10 times.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;take-a-break&quot;&gt;Take a break&lt;&#x2F;h2&gt;
&lt;p&gt;So far we&#x27;ve only really done a single bit of &lt;code&gt;.await&lt;&#x2F;code&gt;ing. But it&#x27;s easy enough to &lt;code&gt;.await&lt;&#x2F;code&gt; on multiple things. Let&#x27;s use &lt;a href=&quot;https:&#x2F;&#x2F;docs.rs&#x2F;tokio&#x2F;0.2.2&#x2F;tokio&#x2F;time&#x2F;fn.delay_for.html&quot;&gt;&lt;code&gt;delay_for&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; to pause for a bit.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::time;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::process::Command;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;std::time::Duration;

#[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;tokio&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;]
async &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Result&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;(), std::io::Error&amp;gt; {
    Command::new(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;date&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;spawn&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;.await&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
    time::delay_for(Duration::from_secs(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)).await;
    Command::new(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;date&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;spawn&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;.await&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
    time::delay_for(Duration::from_secs(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)).await;
    Command::new(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;date&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;spawn&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;.await&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Ok&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(())
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We can also use the &lt;code&gt;tokio::time::interval&lt;&#x2F;code&gt; function to create a stream of &amp;quot;ticks&amp;quot; for each time a certain amount of time has passed. For example, this program will keep calling &lt;code&gt;date&lt;&#x2F;code&gt; once per second until it is killed:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::time;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::process::Command;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;std::time::Duration;

#[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;tokio&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;]
async &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Result&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;(), std::io::Error&amp;gt; {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; interval = time::interval(Duration::from_secs(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;));
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;loop &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
        interval.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;tick&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;().await;
        Command::new(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;date&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;spawn&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;.await&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
    }
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;strong&gt;EXERCISE 3&lt;&#x2F;strong&gt; Why isn&#x27;t there a &lt;code&gt;Ok(())&lt;&#x2F;code&gt; after the &lt;code&gt;loop&lt;&#x2F;code&gt;?&lt;&#x2F;p&gt;
&lt;h2 id=&quot;time-to-spawn&quot;&gt;Time to spawn&lt;&#x2F;h2&gt;
&lt;p&gt;This is all well and good, but we&#x27;re not really taking advantage of asynchronous programming at all. Let&#x27;s fix that! We&#x27;ve seen two different interesting programs:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;Infinitely pausing 1 seconds and calling &lt;code&gt;date&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;Copying all input from &lt;code&gt;stdin&lt;&#x2F;code&gt; to &lt;code&gt;stdout&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;It&#x27;s time to introduce &lt;code&gt;spawn&lt;&#x2F;code&gt; so that we can combine these two into one program. First, let&#x27;s demonstrate a trivial usage of &lt;code&gt;spawn&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;std::time::Duration;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::process::Command;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::task;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::time;

#[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;tokio&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;]
async &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Result&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;(), std::io::Error&amp;gt; {
    task::spawn(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;dating&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()).await&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;??&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Ok&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(())
}

async &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;dating&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Result&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;(), std::io::Error&amp;gt; {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; interval = time::interval(Duration::from_secs(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;));
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;loop &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
        interval.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;tick&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;().await;
        Command::new(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;date&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;spawn&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;.await&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
    }
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You may be wondering: what&#x27;s up with that &lt;code&gt;??&lt;&#x2F;code&gt; operator? Is that some special super-error handler? No, it&#x27;s just the normal error handling &lt;code&gt;?&lt;&#x2F;code&gt; applied twice. Let&#x27;s look at some type signatures to help us out here:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;pub &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;spawn&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;T&amp;gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;task&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: T) -&amp;gt; JoinHandle&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;T::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;Output&amp;gt;;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;impl&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;T&amp;gt; Future &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;JoinHandle&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;T&amp;gt; {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;Output &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Result&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;T, JoinError&amp;gt;;
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Calling &lt;code&gt;spawn&lt;&#x2F;code&gt; gives us back a &lt;code&gt;JoinHandle&amp;lt;T::Output&amp;gt;&lt;&#x2F;code&gt;. In our case, the &lt;code&gt;Future&lt;&#x2F;code&gt; we provide as input is &lt;code&gt;dating()&lt;&#x2F;code&gt;, which has an output of type &lt;code&gt;Result&amp;lt;(), std::io::Error&amp;gt;&lt;&#x2F;code&gt;. So that means the type of &lt;code&gt;task::spawn(dating())&lt;&#x2F;code&gt; is &lt;code&gt;JoinHandle&amp;lt;Result&amp;lt;(), std::io::Error&amp;gt;&amp;gt;&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;We also see that &lt;code&gt;JoinHandle&lt;&#x2F;code&gt; implements &lt;code&gt;Future&lt;&#x2F;code&gt;. So when we apply &lt;code&gt;.await&lt;&#x2F;code&gt; to this value, we end up with whatever that &lt;code&gt;type Output = Result&amp;lt;T, JoinError&amp;gt;&lt;&#x2F;code&gt; thing is. Since we know that &lt;code&gt;T&lt;&#x2F;code&gt; is &lt;code&gt;Result&amp;lt;(), std::io::Error&amp;gt;&lt;&#x2F;code&gt;, this means we end up with &lt;code&gt;Result&amp;lt;Result&amp;lt;(), std::io::Error&amp;gt;, JoinError&amp;gt;&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;The first &lt;code&gt;?&lt;&#x2F;code&gt; deals with the outer &lt;code&gt;Result&lt;&#x2F;code&gt;, exiting with the &lt;code&gt;JoinError&lt;&#x2F;code&gt; on an &lt;code&gt;Err&lt;&#x2F;code&gt;, and giving us a &lt;code&gt;Result&amp;lt;(), std::io::Error&amp;gt;&lt;&#x2F;code&gt; value on &lt;code&gt;Ok&lt;&#x2F;code&gt;. The second &lt;code&gt;?&lt;&#x2F;code&gt; deals with the &lt;code&gt;std::io::Error&lt;&#x2F;code&gt;, giving us a &lt;code&gt;()&lt;&#x2F;code&gt; on &lt;code&gt;Ok&lt;&#x2F;code&gt;. Whew!&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;EXERCISE 4&lt;&#x2F;strong&gt; Now that we&#x27;ve seen &lt;code&gt;spawn&lt;&#x2F;code&gt;, you should modify the program so that it calls both &lt;code&gt;date&lt;&#x2F;code&gt; in a loop, and copies &lt;code&gt;stdin&lt;&#x2F;code&gt; to &lt;code&gt;stdout&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;synchronous-code&quot;&gt;Synchronous code&lt;&#x2F;h2&gt;
&lt;p&gt;You may not have the luxury of interacting exclusively with &lt;code&gt;async&lt;&#x2F;code&gt;-friendly code. Maybe you have some really nice library you want to leverage, but it performs blocking calls internally. Fortunately, Tokio&#x27;s got you covered with the &lt;a href=&quot;https:&#x2F;&#x2F;docs.rs&#x2F;tokio&#x2F;0.2.2&#x2F;tokio&#x2F;task&#x2F;fn.spawn_blocking.html&quot;&gt;&lt;code&gt;spawn_blocking&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; function. Since the docs are so perfect, let me quote them:&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;The &lt;code&gt;task::spawn_blocking&lt;&#x2F;code&gt; function is similar to the &lt;code&gt;task::spawn&lt;&#x2F;code&gt; function discussed in the previous section, but rather than spawning an &lt;code&gt;non-blocking&lt;&#x2F;code&gt; future on the Tokio runtime, it instead spawns a &lt;code&gt;blocking&lt;&#x2F;code&gt; function on a dedicated thread pool for blocking tasks.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;&lt;strong&gt;EXERCISE 5&lt;&#x2F;strong&gt; Rewrite the &lt;code&gt;dating()&lt;&#x2F;code&gt; function to use &lt;code&gt;spawn_blocking&lt;&#x2F;code&gt; and &lt;code&gt;std::thread::sleep&lt;&#x2F;code&gt; so that it calls &lt;code&gt;date&lt;&#x2F;code&gt; approximately once per second.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;let-s-network&quot;&gt;Let&#x27;s network!&lt;&#x2F;h2&gt;
&lt;p&gt;I could keep stepping through the other cools functions in the Tokio library. I encourage you to poke around at them yourself. But I promised some networking, and by golly, I&#x27;m gonna deliver!&lt;&#x2F;p&gt;
&lt;p&gt;I&#x27;m going to slightly extend the example from the &lt;a href=&quot;https:&#x2F;&#x2F;docs.rs&#x2F;tokio&#x2F;0.2.2&#x2F;tokio&#x2F;net&#x2F;struct.TcpListener.html#examples&quot;&gt;&lt;code&gt;TcpListener&lt;&#x2F;code&gt; docs&lt;&#x2F;a&gt; to (1) make it compile and (2) implement an echo server. This program has a pretty major flaw in it though, I recommend trying to find it.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::io;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::net::{TcpListener, TcpStream};

#[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;tokio&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;]
async &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() -&amp;gt; io::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Result&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;()&amp;gt; {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; listener = TcpListener::bind(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;127.0.0.1:8080&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;).await&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;

    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;loop &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(socket, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) = listener.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;accept&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;().await&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;echo&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(socket).await&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
    }
}

async &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;echo&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;socket&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: TcpStream) -&amp;gt; io::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Result&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;()&amp;gt; {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; recv, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; send) = io::split(socket);
    io::copy(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; recv, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; send).await&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Ok&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(())
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We use &lt;code&gt;TcpListener&lt;&#x2F;code&gt; to bind a socket. The binding itself is asynchronous, so we use &lt;code&gt;.await&lt;&#x2F;code&gt; to wait for the listening socket to be available. And we use &lt;code&gt;?&lt;&#x2F;code&gt; to deal with any errors while binding the listening socket.&lt;&#x2F;p&gt;
&lt;p&gt;Next, we loop forever. Inside the loop, we accept new connections, using &lt;code&gt;.await?&lt;&#x2F;code&gt; like before. We capture the &lt;code&gt;socket&lt;&#x2F;code&gt; (ignoring the address as the second part of the tuple). Then we call our &lt;code&gt;echo&lt;&#x2F;code&gt; function and &lt;code&gt;.await&lt;&#x2F;code&gt; it.&lt;&#x2F;p&gt;
&lt;p&gt;Within &lt;code&gt;echo&lt;&#x2F;code&gt;, we use &lt;code&gt;tokio::io::split&lt;&#x2F;code&gt; to split up our &lt;code&gt;TcpStream&lt;&#x2F;code&gt; into its constituent read and write halves, and then pass those into &lt;code&gt;tokio::io::copy&lt;&#x2F;code&gt;, as we&#x27;ve done before.&lt;&#x2F;p&gt;
&lt;p&gt;Awesome! Where&#x27;s the bug? Let me ask you a question: what &lt;em&gt;should&lt;&#x2F;em&gt; the behavior be if a second connection comes in while the first connection is still active? Ideally, it would be handled. However, our program has just one task. And that task &lt;code&gt;.await&lt;&#x2F;code&gt;s on each call to &lt;code&gt;echo&lt;&#x2F;code&gt;. So our second connection won&#x27;t be serviced until the first one closes.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;EXERCISE 6&lt;&#x2F;strong&gt; Modify the program above so that it handles concurrent connections correctly.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;tcp-client-and-ownership&quot;&gt;TCP client and ownership&lt;&#x2F;h2&gt;
&lt;p&gt;Let&#x27;s write a poor man&#x27;s HTTP client. It will establish a connection to a hard-coded server, copy all of &lt;code&gt;stdin&lt;&#x2F;code&gt; to the server, and then copy all data from the server to &lt;code&gt;stdout&lt;&#x2F;code&gt;. To use this, you&#x27;ll manually type in the HTTP request and then hit &lt;code&gt;Ctrl-D&lt;&#x2F;code&gt; for end-of-file.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::io;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::net::TcpStream;

#[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;tokio&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;]
async &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() -&amp;gt; io::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Result&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;()&amp;gt; {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; stream = TcpStream::connect(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;127.0.0.1:8080&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;).await&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; recv, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; send) = io::split(stream);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; stdin = io::stdin();
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; stdout = io::stdout();

    io::copy(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; stdin, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; send).await&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
    io::copy(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; recv, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; stdout).await&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;

    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Ok&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(())
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;That&#x27;s all well and good, but it&#x27;s limited. It only handles half-duplex protocols like HTTP, and doesn&#x27;t actually support keep-alive in any way. We&#x27;d like to use &lt;code&gt;spawn&lt;&#x2F;code&gt; to run the two &lt;code&gt;copy&lt;&#x2F;code&gt;s in different tasks. Seems easy enough:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; send = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;spawn&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(io::copy(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; stdin, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; send));
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; recv = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;spawn&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(io::copy(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; recv, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; stdout));

send.await&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;??&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
recv.await&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;??&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Unfortunately, this doesn&#x27;t compile. We get four nearly-identical error messages. Let&#x27;s look at the first:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;error[E0597]: `stdin` does not live long enough
  --&amp;gt; src&#x2F;main.rs:12:31
   |
12 |     let send = spawn(io::copy(&amp;amp;mut stdin, &amp;amp;mut send));
   |                      ---------^^^^^^^^^^------------
   |                      |        |
   |                      |        borrowed value does not live long enough
   |                      argument requires that `stdin` is borrowed for `&amp;#39;static`
...
19 | }
   | - `stdin` dropped here while still borrowed
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Here&#x27;s the issue: our &lt;code&gt;copy&lt;&#x2F;code&gt; &lt;code&gt;Future&lt;&#x2F;code&gt; does not &lt;em&gt;own&lt;&#x2F;em&gt; the &lt;code&gt;stdin&lt;&#x2F;code&gt; value (or the &lt;code&gt;send&lt;&#x2F;code&gt; value, for that matter). Instead, it has a (mutable) reference to it. That value remains in the &lt;code&gt;main&lt;&#x2F;code&gt; function&#x27;s &lt;code&gt;Future&lt;&#x2F;code&gt;. Ignoring error cases, we know that the &lt;code&gt;main&lt;&#x2F;code&gt; function will wait for &lt;code&gt;send&lt;&#x2F;code&gt; to complete (thanks to &lt;code&gt;send.await&lt;&#x2F;code&gt;), and therefore the lifetimes appear to be correct. However, Rust doesn&#x27;t recognize this lifetime information. (Also, and I haven&#x27;t thought this through completely, I&#x27;m fairly certain that &lt;code&gt;send&lt;&#x2F;code&gt; may be dropped earlier than the &lt;code&gt;Future&lt;&#x2F;code&gt; using it in the case of &lt;code&gt;panic&lt;&#x2F;code&gt;s.)&lt;&#x2F;p&gt;
&lt;p&gt;In order to fix this, we need to convince the compiler to make a &lt;code&gt;Future&lt;&#x2F;code&gt; that owns &lt;code&gt;stdin&lt;&#x2F;code&gt;. And the easiest way to do that here is to use an &lt;code&gt;async move&lt;&#x2F;code&gt; block.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;Exercise 7&lt;&#x2F;strong&gt; Make the code above compile using two &lt;code&gt;async move&lt;&#x2F;code&gt; blocks.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;playing-with-lines&quot;&gt;Playing with &lt;code&gt;lines&lt;&#x2F;code&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;This section will have a series of modifications to a program. I recommend you solve each challenge before looking at the solution. However, unlike the other exercises, I&#x27;m going to show the solutions inline since they build on each other.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s build an async program that counts the number of lines on standard input. You&#x27;ll want to use the &lt;a href=&quot;https:&#x2F;&#x2F;docs.rs&#x2F;tokio&#x2F;0.2.2&#x2F;tokio&#x2F;io&#x2F;trait.AsyncBufReadExt.html#method.lines&quot;&gt;&lt;code&gt;lines&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; method for this. Read the docs and try to figure out what &lt;code&gt;use&lt;&#x2F;code&gt;s and wrappers will be necessary to make the types line up.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::prelude::*;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::io::AsyncBufReadExt;

#[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;tokio&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;]
async &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Result&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;(), std::io::Error&amp;gt; {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; stdin = io::stdin();
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; stdin = io::BufReader::new(stdin);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; count = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; lines = stdin.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;lines&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;();
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;while &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) = lines.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;next_line&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;().await&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;? &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
        count += &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
    }
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Lines on stdin: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, count);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Ok&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(())
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;OK, bumping this up one more level. Instead of standard input, let&#x27;s take a list of file names as command line arguments, and count up the total number of lines in all the files. Initially, it&#x27;s OK to read the files one at a time. In other words: don&#x27;t bother calling &lt;code&gt;spawn&lt;&#x2F;code&gt;. Give it a shot, and then come back here:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::prelude::*;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::io::AsyncBufReadExt;

#[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;tokio&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;]
async &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Result&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;(), std::io::Error&amp;gt; {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; args = std::env::args();
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; _me = args.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;next&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;&#x2F;&#x2F; ignore command name
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; count = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;

    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; filename &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;in&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; args {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; file = tokio::fs::File::open(filename).await&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; file = io::BufReader::new(file);
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; lines = file.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;lines&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;();
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;while &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) = lines.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;next_line&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;().await&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;? &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
            count += &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
        }
    }

    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Total lines: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, count);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Ok&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(())
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;But now it&#x27;s time to make this properly asynchronous, and process the files in separate &lt;code&gt;spawn&lt;&#x2F;code&gt;ed tasks. In order to make this work, we need to spawn all of the tasks, and then &lt;code&gt;.await&lt;&#x2F;code&gt; each of them. I used a &lt;code&gt;Vec&lt;&#x2F;code&gt; of &lt;code&gt;Future&amp;lt;Output=Result&amp;lt;u32, std::io::Error&amp;gt;&amp;gt;&lt;&#x2F;code&gt;s for this. Give it a shot!&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::prelude::*;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::io::AsyncBufReadExt;

#[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;tokio&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;]
async &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Result&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;(), std::io::Error&amp;gt; {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; args = std::env::args();
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; _me = args.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;next&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;&#x2F;&#x2F; ignore command name
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; tasks = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;vec!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;[];

    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; filename &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;in&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; args {
        tasks.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;push&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(tokio::spawn(async {
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; file = tokio::fs::File::open(filename).await&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; file = io::BufReader::new(file);
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; lines = file.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;lines&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;();
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; count = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;while &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) = lines.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;next_line&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;().await&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;? &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
                count += &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
            }
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Ok&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(count) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;as Result&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, std::io::Error&amp;gt;
        }));
    }

    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; count = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; task &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;in&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; tasks {
        count += task.await&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;??&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
    }

    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Total lines: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, count);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Ok&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(())
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And finally in this progression: let&#x27;s change how we handle the &lt;code&gt;count&lt;&#x2F;code&gt;. Instead of &lt;code&gt;.await&lt;&#x2F;code&gt;ing the count in the second &lt;code&gt;for&lt;&#x2F;code&gt; loop, let&#x27;s have each individual task update a shared mutable variable. You should use an &lt;code&gt;Arc&amp;lt;Mutex&amp;lt;u32&amp;gt;&amp;gt;&lt;&#x2F;code&gt; for that. You&#x27;ll still need to keep a &lt;code&gt;Vec&lt;&#x2F;code&gt; of the tasks though to ensure you wait for all files to be read.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::prelude::*;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::io::AsyncBufReadExt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;std::sync::Arc;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;&#x2F;&#x2F; avoid thread blocking by using Tokio&amp;#39;s mutex
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::sync::Mutex;

#[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;tokio&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;]
async &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Result&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;(), std::io::Error&amp;gt; {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; args = std::env::args();
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; _me = args.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;next&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;&#x2F;&#x2F; ignore command name
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; tasks = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;vec!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;[];
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; count = Arc::new(Mutex::new(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;));

    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; filename &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;in&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; args {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; count = count.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;clone&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;();
        tasks.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;push&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(tokio::spawn(async &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;move &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; file = tokio::fs::File::open(filename).await&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; file = io::BufReader::new(file);
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; lines = file.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;lines&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;();
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; local_count = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;while &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) = lines.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;next_line&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;().await&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;? &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
                local_count += &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
            }

            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; count = count.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;lock&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;().await;
            *count += local_count;
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Ok&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(()) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;as Result&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;(), std::io::Error&amp;gt;
        }));
    }

    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; task &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;in&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; tasks {
        task.await&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;??&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
    }

    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; count = count.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;lock&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;().await;
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Total lines: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, *count);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Ok&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(())
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;localset-and-send&quot;&gt;LocalSet and &lt;code&gt;!Send&lt;&#x2F;code&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;Thanks to &lt;a href=&quot;https:&#x2F;&#x2F;twitter.com&#x2F;xudesheng&#x2F;status&#x2F;1201382514415325185?s=20&quot;&gt;@xudehseng&lt;&#x2F;a&gt; for the inspiration on this section.&lt;&#x2F;p&gt;
&lt;p&gt;OK, did that last exercise seem a bit contrived? It was! In my opinion, the previous approach of &lt;code&gt;.await&lt;&#x2F;code&gt;ing the counts and summing in the &lt;code&gt;main&lt;&#x2F;code&gt; function itself was superior. However, I wanted to teach you something else.&lt;&#x2F;p&gt;
&lt;p&gt;What happens if you replace the &lt;code&gt;Arc&amp;lt;Mutex&amp;lt;u32&amp;gt;&amp;gt;&lt;&#x2F;code&gt; with a &lt;code&gt;Rc&amp;lt;RefCell&amp;lt;u32&amp;gt;&amp;gt;&lt;&#x2F;code&gt;? With this code:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::prelude::*;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::io::AsyncBufReadExt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;std::rc::Rc;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;std::cell::RefCell;

#[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;tokio&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;]
async &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Result&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;(), std::io::Error&amp;gt; {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; args = std::env::args();
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; _me = args.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;next&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;&#x2F;&#x2F; ignore command name
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; tasks = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;vec!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;[];
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; count = Rc::new(RefCell::new(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;));

    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; filename &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;in&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; args {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; count = count.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;clone&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;();
        tasks.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;push&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(tokio::spawn(async {
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; file = tokio::fs::File::open(filename).await&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; file = io::BufReader::new(file);
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; lines = file.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;lines&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;();
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; local_count = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;while &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) = lines.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;next_line&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;().await&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;? &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
                local_count += &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
            }

            *count.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;borrow_mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() += local_count;
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Ok&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(()) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;as Result&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;(), std::io::Error&amp;gt;
        }));
    }

    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; task &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;in&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; tasks {
        task.await&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;??&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
    }

    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Total lines: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, count.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;borrow&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;());
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Ok&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(())
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You get an error:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;error[E0277]: `std::rc::Rc&amp;lt;std::cell::RefCell&amp;lt;u32&amp;gt;&amp;gt;` cannot be shared between threads safely
  --&amp;gt; src&#x2F;main.rs:15:20
   |
15 |         tasks.push(tokio::spawn(async {
   |                    ^^^^^^^^^^^^ `std::rc::Rc&amp;lt;std::cell::RefCell&amp;lt;u32&amp;gt;&amp;gt;` cannot be shared between threads safely
   |
  ::: &#x2F;Users&#x2F;michael&#x2F;.cargo&#x2F;registry&#x2F;src&#x2F;github.com-1ecc6299db9ec823&#x2F;tokio-0.2.2&#x2F;src&#x2F;task&#x2F;spawn.rs:49:17
   |
49 |     T: Future + Send + &amp;#39;static,
   |                 ---- required by this bound in `tokio::task::spawn::spawn`
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Tasks can be scheduled to multiple different threads. Therefore, your &lt;code&gt;Future&lt;&#x2F;code&gt; must be &lt;code&gt;Send&lt;&#x2F;code&gt;. And &lt;code&gt;Rc&amp;lt;RefCell&amp;lt;u32&amp;gt;&amp;gt;&lt;&#x2F;code&gt; is definitely &lt;code&gt;!Send&lt;&#x2F;code&gt;. However, in our use case, using multiple OS threads is unlikely to speed up our program; we&#x27;re going to be doing lots of blocking I&#x2F;O. It would be nice if we could insist on spawning all our tasks on the same OS thread and avoid the need for &lt;code&gt;Send&lt;&#x2F;code&gt;. And sure enough, Tokio provides such a function: &lt;code&gt;tokio::task::spawn_local&lt;&#x2F;code&gt;. Using it (and adding back in &lt;code&gt;async move&lt;&#x2F;code&gt; instead of &lt;code&gt;async&lt;&#x2F;code&gt;), our program compiles, but breaks at runtime:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;thread &amp;#39;main&amp;#39; panicked at &amp;#39;`spawn_local` called from outside of a local::LocalSet!&amp;#39;, src&#x2F;libcore&#x2F;option.rs:1190:5
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Uh-oh! Now I&#x27;m personally not a big fan of this detect-it-at-runtime stuff, but the concept is simple enough: if you want to spawn onto the current thread, you need to set up your runtime to support that. And the way we do that is with &lt;a href=&quot;https:&#x2F;&#x2F;docs.rs&#x2F;tokio&#x2F;0.2.2&#x2F;tokio&#x2F;task&#x2F;struct.LocalSet.html&quot;&gt;&lt;code&gt;LocalSet&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;. In order to use this, you&#x27;ll need to ditch the &lt;code&gt;#[tokio::main]&lt;&#x2F;code&gt; attribute.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;EXERCISE 8&lt;&#x2F;strong&gt; Follow the documentation for &lt;code&gt;LocalSet&lt;&#x2F;code&gt; to make the program above work with &lt;code&gt;Rc&amp;lt;RefCell&amp;lt;u32&amp;gt;&amp;gt;&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;&#x2F;h2&gt;
&lt;p&gt;That lesson felt short. Definitely compared to the previous Tokio lesson which seemed to go on forever. I think this is a testament to how easy to use the new &lt;code&gt;async&#x2F;&lt;&#x2F;code&gt;.await` syntax is.&lt;&#x2F;p&gt;
&lt;p&gt;There&#x27;s obviously a lot more that can be covered in asynchronous programming, but hopefully this establishes the largest foundations you need to understand to work with the &lt;code&gt;async&lt;&#x2F;code&gt;&#x2F;&lt;code&gt;.await&lt;&#x2F;code&gt; syntax and the Tokio library itself.&lt;&#x2F;p&gt;
&lt;p&gt;If we have future lessons, I believe they&#x27;ll cover additional libraries like Hyper as they move over to Tokio 0.2, as well as specific use cases people raise. If you want something covered, mention it to me on Twitter or in the comments below.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;solutions&quot;&gt;Solutions&lt;&#x2F;h2&gt;
&lt;h3 id=&quot;solution-1&quot;&gt;Solution 1&lt;&#x2F;h3&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::io;

#[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;tokio&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;]
async &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Result&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;(), std::io::Error&amp;gt; {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; stdin = io::stdin();
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; stdout = io::stdout();
    io::copy(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; stdin, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; stdout).await&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Ok&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(())
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;solution-2&quot;&gt;Solution 2&lt;&#x2F;h3&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::process::Command;

#[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;tokio&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;]
async &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Result&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;(), std::io::Error&amp;gt; {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for _ in &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;..&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;10 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
        Command::new(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;echo&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;arg&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Hello, world!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;spawn&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;.await&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
    }
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Ok&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(())
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;solution-3&quot;&gt;Solution 3&lt;&#x2F;h3&gt;
&lt;p&gt;Since the &lt;code&gt;loop&lt;&#x2F;code&gt; will either run forever or be short circuited by an error, any code following &lt;code&gt;loop&lt;&#x2F;code&gt; will never actually be called. Therefore, code placed there will generate a warning.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;solution-4&quot;&gt;Solution 4&lt;&#x2F;h3&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;std::time::Duration;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::process::Command;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::{io, task, time};

#[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;tokio&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;]
async &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Result&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;(), std::io::Error&amp;gt; {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; dating = task::spawn(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;dating&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;());
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; copying = task::spawn(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;copying&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;());

    dating.await&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;??&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
    copying.await&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;??&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;

    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Ok&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(())
}

async &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;dating&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Result&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;(), std::io::Error&amp;gt; {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; interval = time::interval(Duration::from_secs(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;));
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;loop &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
        interval.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;tick&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;().await;
        Command::new(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;date&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;spawn&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;.await&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
    }
}

async &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;copying&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Result&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;(), std::io::Error&amp;gt; {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; stdin = io::stdin();
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; stdout = io::stdout();
    io::copy(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; stdin, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; stdout).await&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Ok&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(())
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;solution-5&quot;&gt;Solution 5&lt;&#x2F;h3&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;async &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;dating&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Result&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;(), std::io::Error&amp;gt; {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;loop &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
        task::spawn_blocking(|| { std::thread::sleep(Duration::from_secs(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)) }).await&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
        Command::new(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;date&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;spawn&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;.await&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
    }
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;solution-6&quot;&gt;Solution 6&lt;&#x2F;h3&gt;
&lt;p&gt;The simplest tweak is to wrap the &lt;code&gt;echo&lt;&#x2F;code&gt; call with &lt;code&gt;tokio::spawn&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;loop &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(socket, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) = listener.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;accept&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;().await&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
    tokio::spawn(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;echo&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(socket));
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;There is a downside to this worth noting, however: we&#x27;re ignoring the errors produced by the spawned tasks. Likely the best behavior in this case is to handle the errors inside the spawned task:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;#[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;tokio&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;]
async &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() -&amp;gt; io::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Result&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;()&amp;gt; {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; listener = TcpListener::bind(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;127.0.0.1:8080&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;).await&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;

    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; counter = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;loop &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(socket, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) = listener.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;accept&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;().await&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Accepted connection #&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, counter);
        tokio::spawn(async &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;move &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;match echo&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(socket).await {
                &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Ok&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(()) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt; println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Connection #&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt; completed successfully&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, counter),
                &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Err&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(e) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt; println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Connection #&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt; errored: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{:?}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, counter, e),
            }
        });
        counter += &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
    }
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;exericse-7&quot;&gt;Exericse 7&lt;&#x2F;h3&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::io;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::spawn;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::net::TcpStream;

#[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;tokio&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;]
async &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() -&amp;gt; io::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Result&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;()&amp;gt; {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; stream = TcpStream::connect(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;127.0.0.1:8080&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;).await&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; recv, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; send) = io::split(stream);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; stdin = io::stdin();
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; stdout = io::stdout();

    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; send = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;spawn&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(async &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;move &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
        io::copy(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; stdin, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; send).await
    });
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; recv = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;spawn&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(async &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;move &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
        io::copy(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; recv, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; stdout).await
    });

    send.await&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;??&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
    recv.await&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;??&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;

    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Ok&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(())
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;solution-8&quot;&gt;Solution 8&lt;&#x2F;h2&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::prelude::*;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::io::AsyncBufReadExt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;std::rc::Rc;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;std::cell::RefCell;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Result&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;(), std::io::Error&amp;gt; {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; rt = tokio::runtime::Runtime::new()&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; local = tokio::task::LocalSet::new();
    local.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;block_on&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; rt, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;main_inner&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;())
}

async &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main_inner&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Result&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;(), std::io::Error&amp;gt; {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; args = std::env::args();
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; _me = args.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;next&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;&#x2F;&#x2F; ignore command name
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; tasks = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;vec!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;[];
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; count = Rc::new(RefCell::new(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;));

    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; filename &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;in&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; args {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; count = count.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;clone&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;();
        tasks.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;push&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(tokio::task::spawn_local(async &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;move &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; file = tokio::fs::File::open(filename).await&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; file = io::BufReader::new(file);
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; lines = file.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;lines&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;();
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; local_count = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;while &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) = lines.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;next_line&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;().await&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;? &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
                local_count += &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
            }

            *count.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;borrow_mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() += local_count;
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Ok&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(()) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;as Result&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;(), std::io::Error&amp;gt;
        }));
    }

    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; task &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;in&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; tasks {
        task.await&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;??&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
    }

    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Total lines: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, count.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;borrow&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;());
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Ok&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(())
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;</description>
        </item>
        <item>
            <title>Down and dirty with Future - Rust Crash Course lesson 8</title>
            <pubDate>Mon, 02 Dec 2019 00:00:00 +0000</pubDate>
            <link>https://www.snoyman.com/blog/2019/12/rust-crash-course-08-down-dirty-future/</link>
            <guid>https://www.snoyman.com/blog/2019/12/rust-crash-course-08-down-dirty-future/</guid>
            <description>&lt;p&gt;&lt;strong&gt;Heads up&lt;&#x2F;strong&gt; This blog post series has been updated and published as an eBook by FP Complete. I&#x27;d recommend reading that version instead of these posts. If you&#x27;re interested, please check out the &lt;a href=&quot;https:&#x2F;&#x2F;www.fpcomplete.com&#x2F;rust&#x2F;crash-course&#x2F;&quot;&gt;Rust Crash Course eBook&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;It&#x27;s about a year since I wrote the &lt;a href=&quot;https:&#x2F;&#x2F;www.snoyman.com&#x2F;blog&#x2F;2018&#x2F;12&#x2F;rust-crash-course-07-async-futures-tokio&quot;&gt;last installment&lt;&#x2F;a&gt; in the Rust Crash Course series. That last post was a doozy, diving into async, futures, and tokio. All in one post. That was a bit sadistic, and I&#x27;m a bit proud of myself on that front.&lt;&#x2F;p&gt;
&lt;p&gt;Much has happened since then, however. Importantly: the &lt;code&gt;Future&lt;&#x2F;code&gt; trait has moved into the standard library itself and absorbed a few modifications. And then to tie that up in a nicer bow, there&#x27;s a new &lt;code&gt;async&#x2F;.await&lt;&#x2F;code&gt; syntax. It&#x27;s hard for me to overstate just how big a quality of life difference this is when writing asynchronous code in Rust.&lt;&#x2F;p&gt;
&lt;p&gt;I recently &lt;a href=&quot;https:&#x2F;&#x2F;tech.fpcomplete.com&#x2F;rust&#x2F;pid1&quot;&gt;wrote an article on the FP Complete tech site&lt;&#x2F;a&gt; that demonstrates the &lt;code&gt;Future&lt;&#x2F;code&gt; and &lt;code&gt;async&#x2F;.await&lt;&#x2F;code&gt; stuff in practice. But here, I want to give a more thorough analysis of what&#x27;s going on under the surface. Unlike lesson 7, I&#x27;m going to skip the motivation for why we want to write asynchronous code, and break this up into more digestible chunks. Like lesson 7, I&#x27;m going to include the exercise solutions inline, instead of a separate post.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;NOTE&lt;&#x2F;strong&gt; I&#x27;m going to use the &lt;code&gt;async-std&lt;&#x2F;code&gt; library in this example instead of &lt;code&gt;tokio&lt;&#x2F;code&gt;. My only real reason for this is that I started using &lt;code&gt;async-std&lt;&#x2F;code&gt; before &lt;code&gt;tokio&lt;&#x2F;code&gt; released support for the new &lt;code&gt;async&#x2F;.await&lt;&#x2F;code&gt; syntax. I&#x27;m not ready to weigh in on, in general, which of the libraries I prefer.&lt;&#x2F;p&gt;
&lt;p&gt;You should start a Cargo project to play along. Try &lt;code&gt;cargo new --bin sleepus-interruptus&lt;&#x2F;code&gt;. If you want to ensure you&#x27;re on the same compiler version, add a &lt;code&gt;rust-toolchain&lt;&#x2F;code&gt; file with the string &lt;code&gt;1.39.0&lt;&#x2F;code&gt; in it. Run &lt;code&gt;cargo run&lt;&#x2F;code&gt; to make sure you&#x27;re all good to go.&lt;&#x2F;p&gt;
&lt;p&gt;This post is part of a series based on &lt;a href=&quot;https:&#x2F;&#x2F;tech.fpcomplete.com&#x2F;rust&quot;&gt;teaching Rust at FP
Complete&lt;&#x2F;a&gt;. If you&#x27;re reading this post outside
of the blog, you can find links to all posts in the series &lt;a href=&quot;https:&#x2F;&#x2F;www.snoyman.com&#x2F;blog&#x2F;2018&#x2F;10&#x2F;introducing-rust-crash-course&quot;&gt;at the top of the
introduction
post&lt;&#x2F;a&gt;. You
can also &lt;a href=&quot;https:&#x2F;&#x2F;www.snoyman.com&#x2F;feed&#x2F;rust-crash-course&quot;&gt;subscribe to the RSS
feed&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;sleepus-interruptus&quot;&gt;Sleepus Interruptus&lt;&#x2F;h2&gt;
&lt;p&gt;I want to write a program which will print the message &lt;code&gt;Sleepus&lt;&#x2F;code&gt; 10 times, with a delay of 0.5 seconds. And it should print the message &lt;code&gt;Interruptus&lt;&#x2F;code&gt; 5 times, with a delay of 1 second. This is some fairly easy Rust code:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;std::thread::{sleep};
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;std::time::Duration;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;sleepus&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; i &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;..&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;10 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Sleepus &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, i);
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;sleep&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(Duration::from_millis(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;500&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;));
    }
}

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;interruptus&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; i &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;..&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;5 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Interruptus &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, i);
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;sleep&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(Duration::from_millis(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1000&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;));
    }
}

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;sleepus&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;();
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;interruptus&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;();
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;However, as my clever naming implies, this isn&#x27;t my real goal. This program runs the two operations &lt;em&gt;synchronously&lt;&#x2F;em&gt;, first printing &lt;code&gt;Sleepus&lt;&#x2F;code&gt;, then &lt;code&gt;Interruptus&lt;&#x2F;code&gt;. Instead, we would want to have these two sets of statements printed in an interleaved way. That way, the interruptus actually does some interrupting.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;EXERCISE&lt;&#x2F;strong&gt; Use the &lt;code&gt;std::thread::spawn&lt;&#x2F;code&gt; function to spawn an operating system thread to make these printed statements interleave.&lt;&#x2F;p&gt;
&lt;p&gt;There are two basic approaches to this. One—maybe the more obvious—is to spawn a separate thread for each function, and then wait for each of them to complete:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;std::thread::{sleep, spawn};

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; sleepus = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;spawn&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(sleepus);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; interruptus = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;spawn&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(interruptus);

    sleepus.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;join&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;().&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;();
    interruptus.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;join&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;().&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;();
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Two things to notice:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;We call &lt;code&gt;spawn&lt;&#x2F;code&gt; with &lt;code&gt;spawn(sleepus)&lt;&#x2F;code&gt;, &lt;em&gt;not&lt;&#x2F;em&gt; &lt;code&gt;spawn(sleepus())&lt;&#x2F;code&gt;. The former passes in the function &lt;code&gt;sleepus&lt;&#x2F;code&gt; to &lt;code&gt;spawn&lt;&#x2F;code&gt; to be run. The latter would immediately run &lt;code&gt;sleepus()&lt;&#x2F;code&gt; and pass its result to &lt;code&gt;spawn&lt;&#x2F;code&gt;, which is not what we want.&lt;&#x2F;li&gt;
&lt;li&gt;I use &lt;code&gt;join()&lt;&#x2F;code&gt; in the main function&#x2F;thread to wait for the child thread to end. And I use &lt;code&gt;unwrap&lt;&#x2F;code&gt; to deal with any errors that may occur, because I&#x27;m being lazy.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;Another approach would be to spawn one helper thread instead, and call one of the functions in the main thread:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; sleepus = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;spawn&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(sleepus);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;interruptus&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;();

    sleepus.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;join&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;().&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;();
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This is more efficient (less time spawning threads and less memory used for holding them), and doesn&#x27;t really have a downside. I&#x27;d recommend going this way.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;QUESTION&lt;&#x2F;strong&gt; What would be the behavior of this program if we didn&#x27;t call &lt;code&gt;join&lt;&#x2F;code&gt; in the two-spawn version? What if we didn&#x27;t call &lt;code&gt;join&lt;&#x2F;code&gt; in the one-spawn version?&lt;&#x2F;p&gt;
&lt;p&gt;But this isn&#x27;t an asynchronous approach to the problem at all! We have two threads being handled by the operating system which are both acting synchronously and making blocking calls to &lt;code&gt;sleep&lt;&#x2F;code&gt;. Let&#x27;s build up a bit of intuition towards how we could have our two tasks (printing &lt;code&gt;Sleepus&lt;&#x2F;code&gt; and printing &lt;code&gt;Interruptus&lt;&#x2F;code&gt;) behave more cooperatively in a single thread.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;introducing-async&quot;&gt;Introducing &lt;code&gt;async&lt;&#x2F;code&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;We&#x27;re going to start at the highest level of abstraction, and work our way down to understand the details. Let&#x27;s rewrite our application in an &lt;code&gt;async&lt;&#x2F;code&gt; style. Add the following to your &lt;code&gt;Cargo.toml&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-toml&quot; data-lang=&quot;toml&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;async-std &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= { &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;version &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;1.2.0&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;features &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;attributes&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;] }
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And now we can rewrite our application as:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;async_std::task::{sleep, spawn};
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;std::time::Duration;

async &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;sleepus&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; i &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;..&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;10 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Sleepus &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, i);
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;sleep&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(Duration::from_millis(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;500&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)).await;
    }
}

async &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;interruptus&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; i &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;..&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;5 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Interruptus &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, i);
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;sleep&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(Duration::from_millis(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1000&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)).await;
    }
}

#[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;async_std&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;]
async &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; sleepus = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;spawn&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;sleepus&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;());
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;interruptus&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;().await;

    sleepus.await;
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Let&#x27;s hit the changes from top to bottom:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Instead of getting &lt;code&gt;sleep&lt;&#x2F;code&gt; and &lt;code&gt;spawn&lt;&#x2F;code&gt; from &lt;code&gt;std::thread&lt;&#x2F;code&gt;, we&#x27;re getting them from &lt;code&gt;async_std::task&lt;&#x2F;code&gt;. That probably makes sense.&lt;&#x2F;li&gt;
&lt;li&gt;Both &lt;code&gt;sleepus&lt;&#x2F;code&gt; and &lt;code&gt;interruptus&lt;&#x2F;code&gt; now say &lt;code&gt;async&lt;&#x2F;code&gt; in front of &lt;code&gt;fn&lt;&#x2F;code&gt;.&lt;&#x2F;li&gt;
&lt;li&gt;After the calls to &lt;code&gt;sleep&lt;&#x2F;code&gt;, we have a &lt;code&gt;.await&lt;&#x2F;code&gt;. Note that this is &lt;em&gt;not&lt;&#x2F;em&gt; a &lt;code&gt;.await()&lt;&#x2F;code&gt; method call, but instead a new syntax.&lt;&#x2F;li&gt;
&lt;li&gt;We have a new attribute &lt;code&gt;#[async_std::main]&lt;&#x2F;code&gt; on the &lt;code&gt;main&lt;&#x2F;code&gt; function.&lt;&#x2F;li&gt;
&lt;li&gt;The &lt;code&gt;main&lt;&#x2F;code&gt; function also has &lt;code&gt;async&lt;&#x2F;code&gt; before &lt;code&gt;fn&lt;&#x2F;code&gt;.&lt;&#x2F;li&gt;
&lt;li&gt;Instead of &lt;code&gt;spawn(sleepus)&lt;&#x2F;code&gt;, passing in the function itself, we&#x27;re now calling &lt;code&gt;spawn(sleepus())&lt;&#x2F;code&gt;, immediately running the function and passing its result to &lt;code&gt;spawn&lt;&#x2F;code&gt;.&lt;&#x2F;li&gt;
&lt;li&gt;The call to &lt;code&gt;interruptus()&lt;&#x2F;code&gt; is now followed by &lt;code&gt;.await&lt;&#x2F;code&gt;.&lt;&#x2F;li&gt;
&lt;li&gt;Instead of &lt;code&gt;join()&lt;&#x2F;code&gt;ing on the &lt;code&gt;sleepus&lt;&#x2F;code&gt; &lt;code&gt;JoinHandle&lt;&#x2F;code&gt;, we use the &lt;code&gt;.await&lt;&#x2F;code&gt; syntax.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;&lt;strong&gt;EXERCISE&lt;&#x2F;strong&gt; Run this code on your own machine and make sure everything compiles and runs as expected. Then try undoing some of the changes listed above and see what generates a compiler error, and what generates incorrect runtime behavior.&lt;&#x2F;p&gt;
&lt;p&gt;That may look like a large list of changes. But in reality, our code is almost identical structural to the previous version, which is a real testament to the &lt;code&gt;async&#x2F;.await&lt;&#x2F;code&gt; syntax. And now everything works under the surface the way we want: a single operating system thread making non-blocking calls.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s analyze what each of these changes actually means.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;async-functions&quot;&gt;&lt;code&gt;async&lt;&#x2F;code&gt; functions&lt;&#x2F;h2&gt;
&lt;p&gt;Adding &lt;code&gt;async&lt;&#x2F;code&gt; to the beginning of a function definition does three things:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;It allows you to use &lt;code&gt;.await&lt;&#x2F;code&gt; syntax inside. We&#x27;ll get to the meaning of that in a bit.&lt;&#x2F;li&gt;
&lt;li&gt;It modified the return type of the function. &lt;code&gt;async fn foo() -&amp;gt; Bar&lt;&#x2F;code&gt; actually returns &lt;code&gt;impl std::future::Future&amp;lt;Output=Bar&amp;gt;&lt;&#x2F;code&gt;.&lt;&#x2F;li&gt;
&lt;li&gt;Automatically wraps up the result value in a new &lt;code&gt;Future&lt;&#x2F;code&gt;. We&#x27;ll demonstrate that better later.&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;Let&#x27;s unpack that second point a bit. There&#x27;s a trait called &lt;code&gt;Future&lt;&#x2F;code&gt; defined in the standard library. It has an associated type &lt;code&gt;Output&lt;&#x2F;code&gt;. What this trait means is: I promise that, when I complete, I will give you a value of type &lt;code&gt;Output&lt;&#x2F;code&gt;. You could imagine, for instance, an asynchronous HTTP client that looks something like:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;impl &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;HttpRequest &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;perform&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) -&amp;gt; impl Future&amp;lt;Output=HttpResponse&amp;gt; { &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;... &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;There will be some non-blocking I&#x2F;O that needs to occur to make that request. We don&#x27;t want to block the calling thread while those things happen. But we do want to somehow eventually get the resulting response.&lt;&#x2F;p&gt;
&lt;p&gt;We&#x27;ll play around with &lt;code&gt;Future&lt;&#x2F;code&gt; values more directly later. For now, we&#x27;ll continue sticking with the high-level &lt;code&gt;async&#x2F;.await&lt;&#x2F;code&gt; syntax.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;EXERCISE&lt;&#x2F;strong&gt; Rewrite the signature of &lt;code&gt;sleepus&lt;&#x2F;code&gt; to not use the &lt;code&gt;async&lt;&#x2F;code&gt; keyword by modifying its result type. Note that the code will not compile when you get the type right. Pay attention to the error message you get.&lt;&#x2F;p&gt;
&lt;p&gt;The result type of &lt;code&gt;async fn sleepus()&lt;&#x2F;code&gt; is the implied unit value &lt;code&gt;()&lt;&#x2F;code&gt;. Therefore, the &lt;code&gt;Output&lt;&#x2F;code&gt; of our &lt;code&gt;Future&lt;&#x2F;code&gt; should be unit. This means we need to write our signature as:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;sleepus&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() -&amp;gt; impl std::future::Future&amp;lt;Output=()&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;However, with only that change in place, we get the following error messages:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;error[E0728]: `await` is only allowed inside `async` functions and blocks
 --&amp;gt; src&#x2F;main.rs:7:9
  |
4 | fn sleepus() -&amp;gt; impl std::future::Future&amp;lt;Output=()&amp;gt; {
  |    ------- this is not `async`
...
7 |         sleep(Duration::from_millis(500)).await;
  |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ only allowed inside `async` functions and blocks

error[E0277]: the trait bound `(): std::future::Future` is not satisfied
 --&amp;gt; src&#x2F;main.rs:4:17
  |
4 | fn sleepus() -&amp;gt; impl std::future::Future&amp;lt;Output=()&amp;gt; {
  |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::future::Future` is not implemented for `()`
  |
  = note: the return type of a function must have a statically known size
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The first message is pretty direct: you can only use the &lt;code&gt;.await&lt;&#x2F;code&gt; syntax inside an &lt;code&gt;async&lt;&#x2F;code&gt; function or block. We haven&#x27;t seen an &lt;code&gt;async&lt;&#x2F;code&gt; block yet, but it&#x27;s exactly what it sounds like:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;async {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;&#x2F;&#x2F; async noises intensify
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The second error message is a result of the first: the &lt;code&gt;async&lt;&#x2F;code&gt; keyword causes the return type to be an &lt;code&gt;impl Future&lt;&#x2F;code&gt;. Without that keyword, our &lt;code&gt;for&lt;&#x2F;code&gt; loop evaluates to &lt;code&gt;()&lt;&#x2F;code&gt;, which isn&#x27;t an &lt;code&gt;impl Future&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;EXERCISE&lt;&#x2F;strong&gt; Fix the compiler errors by introducing an &lt;code&gt;async&lt;&#x2F;code&gt; block inside the &lt;code&gt;sleepus&lt;&#x2F;code&gt; function. Do &lt;em&gt;not&lt;&#x2F;em&gt; add &lt;code&gt;async&lt;&#x2F;code&gt; to the function signature, keep using &lt;code&gt;impl Future&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Wrapping the entire function body with an &lt;code&gt;async&lt;&#x2F;code&gt; block solves the problem:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;sleepus&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() -&amp;gt; impl std::future::Future&amp;lt;Output=()&amp;gt; {
    async {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; i &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;..&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;10 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Sleepus &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, i);
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;sleep&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(Duration::from_millis(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;500&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)).await;
        }
    }
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;await-a-minute&quot;&gt;&lt;code&gt;.await&lt;&#x2F;code&gt; a minute&lt;&#x2F;h2&gt;
&lt;p&gt;Maybe we don&#x27;t need all this &lt;code&gt;async&#x2F;.await&lt;&#x2F;code&gt; garbage though. What if we remove the calls to &lt;code&gt;.await&lt;&#x2F;code&gt; usage in &lt;code&gt;sleepus&lt;&#x2F;code&gt;? Perhaps surprisingly, it compiles, though it does give us an ominous warning:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;warning: unused implementer of `std::future::Future` that must be used
 --&amp;gt; src&#x2F;main.rs:8:13
  |
8 |             sleep(Duration::from_millis(500));
  |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_must_use)]` on by default
  = note: futures do nothing unless you `.await` or poll them
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We&#x27;re generating a &lt;code&gt;Future&lt;&#x2F;code&gt; value but not using it. And sure enough, if you look at the output of our program, you can see what the compiler means:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;Interruptus 1
Sleepus 1
Sleepus 2
Sleepus 3
Sleepus 4
Sleepus 5
Sleepus 6
Sleepus 7
Sleepus 8
Sleepus 9
Sleepus 10
Interruptus 2
Interruptus 3
Interruptus 4
Interruptus 5
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;All of our &lt;code&gt;Sleepus&lt;&#x2F;code&gt; messages print without delay. Intriguing! The issue is that the call to &lt;code&gt;sleep&lt;&#x2F;code&gt; no longer actually puts our current thread to sleep. Instead, it generates a value which implements &lt;code&gt;Future&lt;&#x2F;code&gt;. And when that promise is eventually fulfilled, we know that the delay has occurred. But in our case, we&#x27;re simply ignoring the &lt;code&gt;Future&lt;&#x2F;code&gt;, and therefore never actually delaying.&lt;&#x2F;p&gt;
&lt;p&gt;To understand what the &lt;code&gt;.await&lt;&#x2F;code&gt; syntax is doing, we&#x27;re going to implement our function with much more direct usage of the &lt;code&gt;Future&lt;&#x2F;code&gt; values. Let&#x27;s start by getting rid of the &lt;code&gt;async&lt;&#x2F;code&gt; block.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;dropping-async-block&quot;&gt;Dropping &lt;code&gt;async&lt;&#x2F;code&gt; block&lt;&#x2F;h2&gt;
&lt;p&gt;If we drop the &lt;code&gt;async&lt;&#x2F;code&gt; block, we end up with this code:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;sleepus&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() -&amp;gt; impl std::future::Future&amp;lt;Output=()&amp;gt; {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; i &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;..&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;10 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Sleepus &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, i);
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;sleep&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(Duration::from_millis(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;500&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;));
    }
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This gives us an error message we saw before:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;error[E0277]: the trait bound `(): std::future::Future` is not satisfied
 --&amp;gt; src&#x2F;main.rs:4:17
  |
4 | fn sleepus() -&amp;gt; impl std::future::Future&amp;lt;Output=()&amp;gt; {
  |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::future::Future` is not implemented for `()`
  |
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This makes sense: the &lt;code&gt;for&lt;&#x2F;code&gt; loop evaluates to &lt;code&gt;()&lt;&#x2F;code&gt;, and unit does not implement &lt;code&gt;Future&lt;&#x2F;code&gt;. One way to fix this is to add an expression after the &lt;code&gt;for&lt;&#x2F;code&gt; loop that evaluates to something that implements &lt;code&gt;Future&lt;&#x2F;code&gt;. And we already know one such thing: &lt;code&gt;sleep&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;EXERCISE&lt;&#x2F;strong&gt; Tweak the &lt;code&gt;sleepus&lt;&#x2F;code&gt; function so that it compiles.&lt;&#x2F;p&gt;
&lt;p&gt;One implementation is:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;sleepus&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() -&amp;gt; impl std::future::Future&amp;lt;Output=()&amp;gt; {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; i &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;..&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;10 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Sleepus &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, i);
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;sleep&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(Duration::from_millis(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;500&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;));
    }
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;sleep&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(Duration::from_millis(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We still get a warning about the unused &lt;code&gt;Future&lt;&#x2F;code&gt; value inside the &lt;code&gt;for&lt;&#x2F;code&gt; loop, but not the one afterwards: that one is getting returned from the function. But of course, sleeping for 0 milliseconds is just a wordy way to do nothing. It would be nice if there was a &amp;quot;dummy&amp;quot; &lt;code&gt;Future&lt;&#x2F;code&gt; that more explicitly did nothing. And fortunately, &lt;a href=&quot;https:&#x2F;&#x2F;docs.rs&#x2F;async-std&#x2F;1.2.0&#x2F;async_std&#x2F;future&#x2F;fn.ready.html&quot;&gt;there is&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;EXERCISE&lt;&#x2F;strong&gt; Replace the &lt;code&gt;sleep&lt;&#x2F;code&gt; call after the &lt;code&gt;for&lt;&#x2F;code&gt; loop with a call to &lt;code&gt;ready&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;sleepus&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() -&amp;gt; impl std::future::Future&amp;lt;Output=()&amp;gt; {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; i &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;..&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;10 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Sleepus &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, i);
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;sleep&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(Duration::from_millis(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;500&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;));
    }
    async_std::future::ready(())
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;implement-our-own-future&quot;&gt;Implement our own &lt;code&gt;Future&lt;&#x2F;code&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;To unpeel this onion a bit more, let&#x27;s make our life harder, and &lt;em&gt;not&lt;&#x2F;em&gt; use the &lt;code&gt;ready&lt;&#x2F;code&gt; function. Instead, we&#x27;re going to define our own &lt;code&gt;struct&lt;&#x2F;code&gt; which implements &lt;code&gt;Future&lt;&#x2F;code&gt;. I&#x27;m going to call it &lt;code&gt;DoNothing&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;std::future::Future;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;struct &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;DoNothing&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;sleepus&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() -&amp;gt; impl Future&amp;lt;Output=()&amp;gt; {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; i &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;..&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;10 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Sleepus &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, i);
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;sleep&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(Duration::from_millis(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;500&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;));
    }
    DoNothing
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;strong&gt;EXERCISE&lt;&#x2F;strong&gt; This code won&#x27;t compile. Without looking below or asking the compiler, what do you think it&#x27;s going to complain about?&lt;&#x2F;p&gt;
&lt;p&gt;The problem here is that &lt;code&gt;DoNothing&lt;&#x2F;code&gt; does not provide a &lt;code&gt;Future&lt;&#x2F;code&gt; implementation. We&#x27;re going to do some Compiler Driven Development and let &lt;code&gt;rustc&lt;&#x2F;code&gt; tell us how to fix our program. Our first error message is:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;the trait bound `DoNothing: std::future::Future` is not satisfied
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;So let&#x27;s add in a trait implementation:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;impl &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;Future &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;DoNothing &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Which fails with:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;error[E0046]: not all trait items implemented, missing: `Output`, `poll`
 --&amp;gt; src&#x2F;main.rs:7:1
  |
7 | impl Future for DoNothing {
  | ^^^^^^^^^^^^^^^^^^^^^^^^^ missing `Output`, `poll` in implementation
  |
  = note: `Output` from trait: `type Output;`
  = note: `poll` from trait: `fn(std::pin::Pin&amp;lt;&amp;amp;mut Self&amp;gt;, &amp;amp;mut std::task::Context&amp;lt;&amp;#39;_&amp;gt;) -&amp;gt; std::task::Poll&amp;lt;&amp;lt;Self as std::future::Future&amp;gt;::Output&amp;gt;`
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We don&#x27;t really know about the &lt;code&gt;Pin&amp;lt;&amp;amp;mut Self&amp;gt;&lt;&#x2F;code&gt; or &lt;code&gt;Context&lt;&#x2F;code&gt; thing yet, but we do know about &lt;code&gt;Output&lt;&#x2F;code&gt;. And since we were previously returning a &lt;code&gt;()&lt;&#x2F;code&gt; from our &lt;code&gt;ready&lt;&#x2F;code&gt; call, let&#x27;s do the same thing here.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;std::pin::Pin;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;std::task::{Context, Poll};

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;impl &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;Future &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;DoNothing &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;Output &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= ();

    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;poll&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: Pin&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;ctx&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; Context) -&amp;gt; Poll&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Self::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;Output&amp;gt; {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;unimplemented!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()
    }
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Woohoo, that compiles! Of course, it fails at runtime due to the &lt;code&gt;unimplemented!()&lt;&#x2F;code&gt; call:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;thread &amp;#39;async-std&#x2F;executor&amp;#39; panicked at &amp;#39;not yet implemented&amp;#39;, src&#x2F;main.rs:13:9
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now let&#x27;s try to implement &lt;code&gt;poll&lt;&#x2F;code&gt;. We need to return a value of type &lt;code&gt;Poll&amp;lt;Self::Output&amp;gt;&lt;&#x2F;code&gt;, or &lt;code&gt;Poll&amp;lt;()&amp;gt;&lt;&#x2F;code&gt;. Let&#x27;s look at the &lt;a href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;std&#x2F;task&#x2F;enum.Poll.html&quot;&gt;definition of &lt;code&gt;Poll&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;pub &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;enum &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;Poll&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;T&amp;gt; {
    Ready(T),
    Pending,
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Using some basic deduction, we can see that &lt;code&gt;Ready&lt;&#x2F;code&gt; means &amp;quot;our &lt;code&gt;Future&lt;&#x2F;code&gt; is complete, and here&#x27;s the output&amp;quot; while &lt;code&gt;Pending&lt;&#x2F;code&gt; means &amp;quot;it&#x27;s not done yet.&amp;quot; Given that our &lt;code&gt;DoNothing&lt;&#x2F;code&gt; wants to return the output of &lt;code&gt;()&lt;&#x2F;code&gt; immediately, we can just use the &lt;code&gt;Ready&lt;&#x2F;code&gt; variant here.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;EXERCISE&lt;&#x2F;strong&gt; Implement a working version of &lt;code&gt;poll&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;poll&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: Pin&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;_ctx&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; Context) -&amp;gt; Poll&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Self::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;Output&amp;gt; {
    Poll::Ready(())
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Congratulations, you&#x27;ve just implemented your first &lt;code&gt;Future&lt;&#x2F;code&gt; struct!&lt;&#x2F;p&gt;
&lt;h2 id=&quot;the-third-async-difference&quot;&gt;The third &lt;code&gt;async&lt;&#x2F;code&gt; difference&lt;&#x2F;h2&gt;
&lt;p&gt;Remember above we said that making a function &lt;code&gt;async&lt;&#x2F;code&gt; does a third thing:&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;Automatically wraps up the result value in a new &lt;code&gt;Future&lt;&#x2F;code&gt;. We&#x27;ll demonstrate that better later.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;Now is later. Let&#x27;s demonstrate that better.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s simplify the definition of &lt;code&gt;sleepus&lt;&#x2F;code&gt; to:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;sleepus&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() -&amp;gt; impl Future&amp;lt;Output=()&amp;gt; {
    DoNothing
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The compiles and runs just fine. Let&#x27;s try switching back to the &lt;code&gt;async&lt;&#x2F;code&gt; way of writing the signature:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;async &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;sleepus&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    DoNothing
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This now gives us an error:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;error[E0271]: type mismatch resolving `&amp;lt;impl std::future::Future as std::future::Future&amp;gt;::Output == ()`
  --&amp;gt; src&#x2F;main.rs:17:20
   |
17 | async fn sleepus() {
   |                    ^ expected struct `DoNothing`, found ()
   |
   = note: expected type `DoNothing`
              found type `()`
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You see, when you have an &lt;code&gt;async&lt;&#x2F;code&gt; function or block, the result is automatically wrapped up in a &lt;code&gt;Future&lt;&#x2F;code&gt;. So instead of returning a &lt;code&gt;DoNothing&lt;&#x2F;code&gt;, we&#x27;re returning a &lt;code&gt;impl Future&amp;lt;Output=DoNothing&amp;gt;&lt;&#x2F;code&gt;. And our type wants &lt;code&gt;Output=()&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;EXERCISE&lt;&#x2F;strong&gt; Try to guess what you need to add to this function to make it compile.&lt;&#x2F;p&gt;
&lt;p&gt;Working around this is pretty easy: you simply append &lt;code&gt;.await&lt;&#x2F;code&gt; to &lt;code&gt;DoNothing&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;async &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;sleepus&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    DoNothing.await
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This gives us a little more intuition for what &lt;code&gt;.await&lt;&#x2F;code&gt; is doing: it&#x27;s extracting the &lt;code&gt;()&lt;&#x2F;code&gt; &lt;code&gt;Output&lt;&#x2F;code&gt; from the &lt;code&gt;DoNothing&lt;&#x2F;code&gt; &lt;code&gt;Future&lt;&#x2F;code&gt;... somehow. However, we still don&#x27;t really know how it&#x27;s achieving that. Let&#x27;s build up a more complicated &lt;code&gt;Future&lt;&#x2F;code&gt; to get closer.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;sleepprint&quot;&gt;SleepPrint&lt;&#x2F;h2&gt;
&lt;p&gt;We&#x27;re going to build a new &lt;code&gt;Future&lt;&#x2F;code&gt; implementation which:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Sleeps for a certain amount of time&lt;&#x2F;li&gt;
&lt;li&gt;Then prints a message&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;This is going to involve using &lt;a href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;std&#x2F;pin&#x2F;index.html&quot;&gt;pinned pointers&lt;&#x2F;a&gt;. I&#x27;m not going to describe those here. The specifics of what&#x27;s happening with the pinning isn&#x27;t terribly enlightening to the topic of &lt;code&gt;Future&lt;&#x2F;code&gt;s. If you want to let your eyes glaze over at that part of the code, you won&#x27;t be missing much.&lt;&#x2F;p&gt;
&lt;p&gt;Our implementation strategy for &lt;code&gt;SleepPrint&lt;&#x2F;code&gt; will be to wrap an existing &lt;code&gt;sleep&lt;&#x2F;code&gt; &lt;code&gt;Future&lt;&#x2F;code&gt; with our own implementation of &lt;code&gt;Future&lt;&#x2F;code&gt;. Since we don&#x27;t know the exact type of the result of a &lt;code&gt;sleep&lt;&#x2F;code&gt; call (it&#x27;s just an &lt;code&gt;impl Future&lt;&#x2F;code&gt;), we&#x27;ll use a parameter:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;struct &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;SleepPrint&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;Fut&amp;gt; {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;sleep&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: Fut,
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And we can call this in our &lt;code&gt;sleepus&lt;&#x2F;code&gt; function with:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;sleepus&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() -&amp;gt; impl Future&amp;lt;Output=()&amp;gt; {
    SleepPrint {
        sleep: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;sleep&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(Duration::from_millis(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;3000&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)),
    }
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Of course, we now get a compiler error about a missing &lt;code&gt;Future&lt;&#x2F;code&gt; implementation. So let&#x27;s work on that. Our &lt;code&gt;impl&lt;&#x2F;code&gt; starts with:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;impl&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;Fut: Future&amp;lt;Output=()&amp;gt;&amp;gt; Future &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;SleepPrint&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;Fut&amp;gt; {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;...
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This says that &lt;code&gt;SleepPrint&lt;&#x2F;code&gt; is a &lt;code&gt;Future&lt;&#x2F;code&gt; if the &lt;code&gt;sleep&lt;&#x2F;code&gt; value it contains is a &lt;code&gt;Future&lt;&#x2F;code&gt; with an &lt;code&gt;Output&lt;&#x2F;code&gt; of type &lt;code&gt;()&lt;&#x2F;code&gt;. Which, of course, is true in the case of the &lt;code&gt;sleep&lt;&#x2F;code&gt; function, so we&#x27;re good. We need to define &lt;code&gt;Output&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;Output &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= ();
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And then we need a &lt;code&gt;poll&lt;&#x2F;code&gt; function:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;poll&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: Pin&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;ctx&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; Context) -&amp;gt; Poll&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Self::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;Output&amp;gt; {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;...
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The next bit is the eyes-glazing part around pinned pointers. We need to &lt;em&gt;project&lt;&#x2F;em&gt; the &lt;code&gt;Pin&amp;lt;&amp;amp;mut Self&amp;gt;&lt;&#x2F;code&gt; into a &lt;code&gt;Pin&amp;lt;&amp;amp;mut Fut&amp;gt;&lt;&#x2F;code&gt; so that we can work on the underlying sleep &lt;code&gt;Future&lt;&#x2F;code&gt;. We could use a &lt;a href=&quot;https:&#x2F;&#x2F;crates.io&#x2F;crates&#x2F;pin-project-lite&quot;&gt;helper crate&lt;&#x2F;a&gt; to make this a bit prettier, but we&#x27;ll just do some &lt;code&gt;unsafe&lt;&#x2F;code&gt; mapping:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; sleep: Pin&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; Fut&amp;gt; = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;unsafe &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;map_unchecked_mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;s&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; s.sleep) };
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Alright, now the important bit. We&#x27;ve got our underlying &lt;code&gt;Future&lt;&#x2F;code&gt;, and we need to do something with it. The only thing we &lt;em&gt;can&lt;&#x2F;em&gt; do with it is call &lt;code&gt;poll&lt;&#x2F;code&gt;. &lt;code&gt;poll&lt;&#x2F;code&gt; requires a &lt;code&gt;&amp;amp;mut Context&lt;&#x2F;code&gt;, which fortunately we&#x27;ve been provided. That &lt;code&gt;Context&lt;&#x2F;code&gt; contains information about the currently running task, so it can be woken up (via a &lt;code&gt;Waker&lt;&#x2F;code&gt;) when the task is ready.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;NOTE&lt;&#x2F;strong&gt; We&#x27;re not going to get deeper into how &lt;code&gt;Waker&lt;&#x2F;code&gt; works in this post. If you want a real life example of how to call &lt;code&gt;Waker&lt;&#x2F;code&gt; yourself, I recommend reading my &lt;a href=&quot;https:&#x2F;&#x2F;tech.fpcomplete.com&#x2F;rust&#x2F;pid1&quot;&gt;pid1 in Rust&lt;&#x2F;a&gt; post.&lt;&#x2F;p&gt;
&lt;p&gt;For now, let&#x27;s do the only thing we can reasonably do:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;match&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; sleep.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;poll&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(ctx) {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;...
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We&#x27;ve got two possibilities. If &lt;code&gt;poll&lt;&#x2F;code&gt; returns a &lt;code&gt;Pending&lt;&#x2F;code&gt;, it means that the &lt;code&gt;sleep&lt;&#x2F;code&gt; hasn&#x27;t completed yet. In that case, we want our &lt;code&gt;Future&lt;&#x2F;code&gt; to also indicate that it&#x27;s not done. To make that work, we just propagate the &lt;code&gt;Pending&lt;&#x2F;code&gt; value:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;Poll::Pending &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;Poll::Pending,
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;However, if the &lt;code&gt;sleep&lt;&#x2F;code&gt; is already complete, we&#x27;ll receive a &lt;code&gt;Ready(())&lt;&#x2F;code&gt; variant. In that case, it&#x27;s finally time to print our message and then propagate the &lt;code&gt;Ready&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;Poll::Ready(()) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Inside SleepPrint&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
    Poll::Ready(())
},
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And just like that, we&#x27;ve built a more complex &lt;code&gt;Future&lt;&#x2F;code&gt; from a simpler one. But that was pretty ad-hoc.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;twofutures&quot;&gt;TwoFutures&lt;&#x2F;h2&gt;
&lt;p&gt;&lt;code&gt;SleepPrint&lt;&#x2F;code&gt; is pretty ad-hoc: it hard codes a specific action to run after the &lt;code&gt;sleep&lt;&#x2F;code&gt; &lt;code&gt;Future&lt;&#x2F;code&gt; completes. Let&#x27;s up our game, and sequence the actions of two different &lt;code&gt;Future&lt;&#x2F;code&gt;s. We&#x27;re going to define a new &lt;code&gt;struct&lt;&#x2F;code&gt; that has three fields:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;The first &lt;code&gt;Future&lt;&#x2F;code&gt; to run&lt;&#x2F;li&gt;
&lt;li&gt;The second &lt;code&gt;Future&lt;&#x2F;code&gt; to run&lt;&#x2F;li&gt;
&lt;li&gt;A &lt;code&gt;bool&lt;&#x2F;code&gt; to tell us if we&#x27;ve finished running the first &lt;code&gt;Future&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;Since the &lt;code&gt;Pin&lt;&#x2F;code&gt; stuff is going to get a bit more complicated, it&#x27;s time to reach for that helper crate to ease our implementation and avoid &lt;code&gt;unsafe&lt;&#x2F;code&gt; blocks ourself. So add the following to your &lt;code&gt;Cargo.toml&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-toml&quot; data-lang=&quot;toml&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;pin-project-lite &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;0.1.1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And now we can define a &lt;code&gt;TwoFutures&lt;&#x2F;code&gt; struct that allows us to project the first and second &lt;code&gt;Future&lt;&#x2F;code&gt;s into pinned pointers:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;pin_project_lite::pin_project;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;pin_project! &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;struct &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;TwoFutures&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;Fut1, Fut2&amp;gt; {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;first_done&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;bool&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;,
        #[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;pin&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;]
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;first&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: Fut1,
        #[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;pin&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;]
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;second&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: Fut2,
    }
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Using this in &lt;code&gt;sleepus&lt;&#x2F;code&gt; is easy enough:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;sleepus&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() -&amp;gt; impl Future&amp;lt;Output=()&amp;gt; {
    TwoFutures {
        first_done: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;false&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;,
        first: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;sleep&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(Duration::from_millis(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;3000&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)),
        second: async { &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Hello TwoFutures&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;); },
    }
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now we just need to define our &lt;code&gt;Future&lt;&#x2F;code&gt; implementation. Easy, right? We want to make sure both &lt;code&gt;Fut1&lt;&#x2F;code&gt; and &lt;code&gt;Fut2&lt;&#x2F;code&gt; are &lt;code&gt;Future&lt;&#x2F;code&gt;s. And our &lt;code&gt;Output&lt;&#x2F;code&gt; will be the output from the &lt;code&gt;Fut2&lt;&#x2F;code&gt;. (You could also return both the first and second output if you wanted.) To make all that work:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;impl&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;Fut1: Future, Fut2: Future&amp;gt; Future &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;TwoFutures&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;Fut1, Fut2&amp;gt; {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;Output &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= Fut2::Output;

    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;poll&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: Pin&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;ctx&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; Context) -&amp;gt; Poll&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Self::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;Output&amp;gt; {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;...
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In order to work with the pinned pointer, we&#x27;re going to get a new value, &lt;code&gt;this&lt;&#x2F;code&gt;, which projects all of the pointers:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; this = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;project&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;();
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;With that out of the way, we can interact with our three fields directly in &lt;code&gt;this&lt;&#x2F;code&gt;. The first thing we do is check if the first &lt;code&gt;Future&lt;&#x2F;code&gt; has already completed. If not, we&#x27;re going to poll it. If the poll is &lt;code&gt;Ready&lt;&#x2F;code&gt;, then we&#x27;ll ignore the output and indicate that the first &lt;code&gt;Future&lt;&#x2F;code&gt; is done:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;if !&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;*this.first_done {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;Poll::Ready(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) = this.first.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;poll&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(ctx) {
        *this.first_done = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;true&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
    }
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Next, if the first &lt;code&gt;Future&lt;&#x2F;code&gt; is done, we want to poll the second. And if the first &lt;code&gt;Future&lt;&#x2F;code&gt; is &lt;em&gt;not&lt;&#x2F;em&gt; done, then we say that we&#x27;re pending:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;*this.first_done {
    this.second.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;poll&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(ctx)
} &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;else &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
    Poll::Pending
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And just like that, we&#x27;ve composed two &lt;code&gt;Future&lt;&#x2F;code&gt;s together into a bigger, grander, brighter &lt;code&gt;Future&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;EXERCISE&lt;&#x2F;strong&gt; Get rid of the usage of an &lt;code&gt;async&lt;&#x2F;code&gt; block in &lt;code&gt;second&lt;&#x2F;code&gt;. Let the compiler errors guide you.&lt;&#x2F;p&gt;
&lt;p&gt;The error message you get says that &lt;code&gt;()&lt;&#x2F;code&gt; is not a &lt;code&gt;Future&lt;&#x2F;code&gt;. Instead, you need to return a &lt;code&gt;Future&lt;&#x2F;code&gt; value after the call to &lt;code&gt;println!&lt;&#x2F;code&gt;. We can use our handy &lt;code&gt;async_std::future::ready&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;second: {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Hello TwoFutures&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
    async_std::future::ready(())
},
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;andthen&quot;&gt;AndThen&lt;&#x2F;h2&gt;
&lt;p&gt;Sticking together two arbitrary &lt;code&gt;Future&lt;&#x2F;code&gt;s like this is nice. But it&#x27;s even nicer to have the second &lt;code&gt;Future&lt;&#x2F;code&gt;s depend on the result of the first &lt;code&gt;Future&lt;&#x2F;code&gt;. To do this, we&#x27;d want a function like &lt;code&gt;and_then&lt;&#x2F;code&gt;. (Monads FTW to my Haskell buddies.) I&#x27;m not going to bore you with the gory details of an implementation here, but feel free to &lt;a href=&quot;https:&#x2F;&#x2F;gist.github.com&#x2F;snoyberg&#x2F;7eeb5e330d9b5db9806d82c83c9d3e56&quot;&gt;read the Gist if you&#x27;re interested&lt;&#x2F;a&gt;. Assuming you have this method available, we can begin to write the &lt;code&gt;sleepus&lt;&#x2F;code&gt; function ourselves as:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;sleepus&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() -&amp;gt; impl Future&amp;lt;Output = ()&amp;gt; {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Sleepus 1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;sleep&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(Duration::from_millis(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;500&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;and_then&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|()| {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Sleepus 2&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;sleep&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(Duration::from_millis(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;500&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;and_then&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|()| {
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Sleepus 3&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;sleep&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(Duration::from_millis(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;500&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;and_then&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|()| {
                &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Sleepus 4&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
                async_std::future::ready(())
            })
        })
    })
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And before Rust 1.39 and the &lt;code&gt;async&#x2F;.await&lt;&#x2F;code&gt; syntax, this is basically how async code worked. This is far from perfect. Besides the obvious right-stepping of the code, it&#x27;s not actually a loop. You &lt;em&gt;could&lt;&#x2F;em&gt; recursively call &lt;code&gt;sleepus&lt;&#x2F;code&gt;, except that creates an infinite type which the compiler isn&#x27;t too fond of.&lt;&#x2F;p&gt;
&lt;p&gt;But fortunately, we&#x27;ve now finally established enough background to easily explain what the &lt;code&gt;.await&lt;&#x2F;code&gt; syntax is doing: exactly what &lt;code&gt;and_then&lt;&#x2F;code&gt; is doing, but without the fuss!&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;EXERCISE&lt;&#x2F;strong&gt; Rewrite the &lt;code&gt;sleepus&lt;&#x2F;code&gt; function above to use &lt;code&gt;.await&lt;&#x2F;code&gt; instead of &lt;code&gt;and_then&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;The rewrite is really easy. The body of the function becomes the non-right-stepping, super flat:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Sleepus 1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;sleep&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(Duration::from_millis(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;500&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)).await;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Sleepus 2&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;sleep&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(Duration::from_millis(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;500&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)).await;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Sleepus 3&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;sleep&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(Duration::from_millis(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;500&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)).await;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Sleepus 4&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And then we also need to change the signature of our function to use &lt;code&gt;async&lt;&#x2F;code&gt;, or wrap everything in an &lt;code&gt;async&lt;&#x2F;code&gt; block. Your call.&lt;&#x2F;p&gt;
&lt;p&gt;Besides the obvious readability improvements here, there are some massive usability improvements with &lt;code&gt;.await&lt;&#x2F;code&gt; as well. One that sticks out here is how easily it ties in with loops. This was a real pain with the older &lt;code&gt;futures&lt;&#x2F;code&gt; stuff. Also, chaining together multiple &lt;code&gt;await&lt;&#x2F;code&gt; calls is really easy, e.g.:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; body = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;make_http_request&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;().await.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;get_body&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;().await;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And not only that, but it plays in with the &lt;code&gt;?&lt;&#x2F;code&gt; operator for error handling perfectly. The above example would more likely be:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; body = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;make_http_request&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;().await&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;get_body&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;().await&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;main-attribute&quot;&gt;&lt;code&gt;main&lt;&#x2F;code&gt; attribute&lt;&#x2F;h2&gt;
&lt;p&gt;One final mystery remains. What exactly is going on with that weird attribute on &lt;code&gt;main&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;#[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;async_std&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;]
async &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;...
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Our &lt;code&gt;sleepus&lt;&#x2F;code&gt; and &lt;code&gt;interruptus&lt;&#x2F;code&gt; functions do not actually do anything. They return &lt;code&gt;Future&lt;&#x2F;code&gt;s which provide instructions on how to do work. Something has to actually perform those actions. The thing that runs those actions is an &lt;strong&gt;executor&lt;&#x2F;strong&gt;. The &lt;code&gt;async-std&lt;&#x2F;code&gt; library provides an executor, as does &lt;code&gt;tokio&lt;&#x2F;code&gt;. In order to run any &lt;code&gt;Future&lt;&#x2F;code&gt;, you need an executor.&lt;&#x2F;p&gt;
&lt;p&gt;The attribute above automatically wraps the &lt;code&gt;main&lt;&#x2F;code&gt; function with &lt;code&gt;async-std&lt;&#x2F;code&gt;&#x27;s executor. The attribute approach, however, is totally optional. Instead, you can use &lt;code&gt;async_std::task::block_on&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;EXERCISE&lt;&#x2F;strong&gt; Rewrite &lt;code&gt;main&lt;&#x2F;code&gt; to not use the attribute. You&#x27;ll need to rewrite it from &lt;code&gt;async fn main&lt;&#x2F;code&gt; to &lt;code&gt;fn main&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Since we use &lt;code&gt;.await&lt;&#x2F;code&gt; inside the body of &lt;code&gt;main&lt;&#x2F;code&gt;, we get an error when we simply remove the &lt;code&gt;async&lt;&#x2F;code&gt; qualifier. Therefore, we need to use an &lt;code&gt;async&lt;&#x2F;code&gt; block inside &lt;code&gt;main&lt;&#x2F;code&gt; (or define a separate helper &lt;code&gt;async&lt;&#x2F;code&gt; function). Putting it all together:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    async_std::task::block_on(async {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; sleepus = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;spawn&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;sleepus&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;());
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;interruptus&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;().await;

        sleepus.await;
    })
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Each executor is capable of managing multiple tasks. Each task is working on producing the output of a single &lt;code&gt;Future&lt;&#x2F;code&gt;. And just like with threads, you can &lt;code&gt;spawn&lt;&#x2F;code&gt; additional tasks to get concurrent running. Which is exactly how we achieve the interleaving we wanted!&lt;&#x2F;p&gt;
&lt;h2 id=&quot;cooperative-concurrency&quot;&gt;Cooperative concurrency&lt;&#x2F;h2&gt;
&lt;p&gt;One word of warning. &lt;code&gt;Future&lt;&#x2F;code&gt;s and &lt;code&gt;async&lt;&#x2F;code&gt;&#x2F;&lt;code&gt;.await&lt;&#x2F;code&gt; implement a form of cooperative concurrency. By contrast, operating system threads provide preemptive concurrency. The important different is that in cooperative concurrency, you have to cooperate. If one of your tasks causes a delay, such as by using &lt;code&gt;std::thread::sleep&lt;&#x2F;code&gt; or by performing significant CPU computation, it will not be interrupted.&lt;&#x2F;p&gt;
&lt;p&gt;The upshot of this is that you should ensure you do not perform blocking calls inside your tasks. And if you have a CPU-intensive task to perform, it&#x27;s probably worth spawning an OS thread for it, or at least ensuring your executor will not starve your other tasks.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;&#x2F;h2&gt;
&lt;p&gt;I don&#x27;t think the behavior under the surface of &lt;code&gt;.await&lt;&#x2F;code&gt; is too big a reveal, but I think it&#x27;s useful to understand exactly what&#x27;s happening here. In particular, understanding the difference between a value of &lt;code&gt;Future&lt;&#x2F;code&gt; and actually chaining together the outputs of &lt;code&gt;Future&lt;&#x2F;code&gt; values is core to using &lt;code&gt;async&#x2F;.await&lt;&#x2F;code&gt; correctly. Fortunately, the compiler errors and warnings do a great job of guiding you in the right direction.&lt;&#x2F;p&gt;
&lt;p&gt;In the next lesson, we can start using our newfound knowledge of &lt;code&gt;Future&lt;&#x2F;code&gt; and the &lt;code&gt;async&#x2F;.await&lt;&#x2F;code&gt; syntax to build some asynchronous applications. We&#x27;ll be diving into writing some async I&#x2F;O, including networking code, using Tokio 0.2.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;exercises&quot;&gt;Exercises&lt;&#x2F;h2&gt;
&lt;p&gt;Here are some take-home exercises to play with. You can base them on &lt;a href=&quot;https:&#x2F;&#x2F;gist.github.com&#x2F;snoyberg&#x2F;f5fea804f2b6fb69ae6d1f75c8004fc5&quot;&gt;the code in this Gist&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;Modify the &lt;code&gt;main&lt;&#x2F;code&gt; function to call &lt;code&gt;spawn&lt;&#x2F;code&gt; twice instead of just once.&lt;&#x2F;li&gt;
&lt;li&gt;Modify the &lt;code&gt;main&lt;&#x2F;code&gt; function to not call &lt;code&gt;spawn&lt;&#x2F;code&gt; at all. Instead, use &lt;a href=&quot;https:&#x2F;&#x2F;docs.rs&#x2F;async-std&#x2F;1.2.0&#x2F;async_std&#x2F;future&#x2F;trait.Future.html#method.join&quot;&gt;&lt;code&gt;join&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;. You&#x27;ll need to add a &lt;code&gt;use async_std::prelude::*;&lt;&#x2F;code&gt; and add the &lt;code&gt;&amp;quot;unstable&amp;quot;&lt;&#x2F;code&gt; feature to the &lt;code&gt;async-std&lt;&#x2F;code&gt; dependency in &lt;code&gt;Cargo.toml&lt;&#x2F;code&gt;.&lt;&#x2F;li&gt;
&lt;li&gt;Modify the &lt;code&gt;main&lt;&#x2F;code&gt; function to get the non-interleaved behavior, where the program prints &lt;code&gt;Sleepus&lt;&#x2F;code&gt; multiple times before &lt;code&gt;Interruptus&lt;&#x2F;code&gt;.&lt;&#x2F;li&gt;
&lt;li&gt;We&#x27;re still performing blocking I&#x2F;O with &lt;code&gt;println!&lt;&#x2F;code&gt;. Turn on the &lt;code&gt;&amp;quot;unstable&amp;quot;&lt;&#x2F;code&gt; feature again, and try using &lt;code&gt;async_std::println&lt;&#x2F;code&gt;. You&#x27;ll get an ugly error message until you get rid of &lt;code&gt;spawn&lt;&#x2F;code&gt;. Try to understand why that happens.&lt;&#x2F;li&gt;
&lt;li&gt;Write a function &lt;code&gt;foo&lt;&#x2F;code&gt; such that the following assertion passes: &lt;code&gt;assert_eq!(42, async_std::task::block_on(async { foo().await.await }));&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
</description>
        </item>
        <item>
            <title>Async, futures, and tokio - Rust Crash Course lesson 7</title>
            <pubDate>Mon, 03 Dec 2018 00:00:00 +0000</pubDate>
            <link>https://www.snoyman.com/blog/2018/12/rust-crash-course-07-async-futures-tokio/</link>
            <guid>https://www.snoyman.com/blog/2018/12/rust-crash-course-07-async-futures-tokio/</guid>
            <description>&lt;p&gt;&lt;strong&gt;Heads up&lt;&#x2F;strong&gt; This blog post series has been updated and published as an eBook by FP Complete. I&#x27;d recommend reading that version instead of these posts. If you&#x27;re interested, please check out the &lt;a href=&quot;https:&#x2F;&#x2F;www.fpcomplete.com&#x2F;rust&#x2F;crash-course&#x2F;&quot;&gt;Rust Crash Course eBook&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;NOTE&lt;&#x2F;strong&gt; With the addition of async&#x2F;await syntax in Rust 1.39 (November 2019), everything related to async code in Rust is getting an overhaul. As such, this lesson is now pretty deeply out of date. It&#x27;s still useful for understanding the deeper principles, but I hope to write up an updated tutorial in the future covering the new approach.&lt;&#x2F;p&gt;
&lt;p&gt;Unlike languages like Haskell, Erlang, and Go, Rust does not have a
runtime system providing green threads and asynchronous I&#x2F;O. However,
for many real world use cases, async I&#x2F;O is strongly desired, if not a
hard requirement. The de facto standard library for handling this in
Rust is tokio.&lt;&#x2F;p&gt;
&lt;p&gt;This post is part of a series based on &lt;a href=&quot;https:&#x2F;&#x2F;www.fpcomplete.com&#x2F;rust&quot;&gt;teaching Rust at FP
Complete&lt;&#x2F;a&gt;. If you&#x27;re reading this post outside
of the blog, you can find links to all posts in the series &lt;a href=&quot;https:&#x2F;&#x2F;www.snoyman.com&#x2F;blog&#x2F;2018&#x2F;10&#x2F;introducing-rust-crash-course&quot;&gt;at the top of the
introduction
post&lt;&#x2F;a&gt;. You
can also &lt;a href=&quot;https:&#x2F;&#x2F;www.snoyman.com&#x2F;feed&#x2F;rust-crash-course&quot;&gt;subscribe to the RSS
feed&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;This lesson in the crash course is going to be a bit different from
others, since:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;There&#x27;s a lot of interconnected material to cover which can&#x27;t as
easily be separated out&lt;&#x2F;li&gt;
&lt;li&gt;It&#x27;s more important to understand the motivation behind the design
of these libraries than in many other cases&lt;&#x2F;li&gt;
&lt;li&gt;I believe the material may be useful for people who haven&#x27;t been
following the rest of the crash course&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;Therefore, some different rules will apply:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;I&#x27;m defining up front the knowledge you&#x27;ll need of Rust to
understand this lesson, namely:
&lt;ul&gt;
&lt;li&gt;All the basics of syntax&lt;&#x2F;li&gt;
&lt;li&gt;Traits and associated types&lt;&#x2F;li&gt;
&lt;li&gt;Iterators&lt;&#x2F;li&gt;
&lt;li&gt;Closures&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;Instead of providing the exercise solutions in a later post, I&#x27;ll be
providing them immediately, since the material is so cumulative. I
still &lt;em&gt;strongly recommend&lt;&#x2F;em&gt; spending significant time and effort
trying to solve the exercises yourself before looking at the
solutions. It&#x27;s harder and more time consuming, but ultimately
worthwhile.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;Also, this lesson is &lt;em&gt;much longer and more involved&lt;&#x2F;em&gt; than previous
lessons. You should plan on it taking more time to complete than
others. I considered breaking this up into multiple lessons, but
decided to keep all of the content together. Instead, I&#x27;ll be taking a
break from weekly lessons after this one for a bit.&lt;&#x2F;p&gt;
&lt;p&gt;Consider that the intro to the intro. Now the real intro!&lt;&#x2F;p&gt;
&lt;h2 id=&quot;why-async&quot;&gt;Why async?&lt;&#x2F;h2&gt;
&lt;p&gt;I&#x27;m going to assume that readers are already familiar with async I&#x2F;O
and its motivations in general. If you&#x27;re not, it&#x27;s worth reading up a
bit on &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;C10k_problem&quot;&gt;the C10k problem&lt;&#x2F;a&gt;,
where many of us started thinking hard about async I&#x2F;O. You may also
be interested in reading a post I wrote &lt;a href=&quot;https:&#x2F;&#x2F;www.fpcomplete.com&#x2F;blog&#x2F;2017&#x2F;01&#x2F;green-threads-are-like-garbage-collection&quot;&gt;about green
threads&lt;&#x2F;a&gt;,
a language runtime-based solution to the same problem.&lt;&#x2F;p&gt;
&lt;p&gt;At the end of the day, the goals of Rust&#x27;s approach to async I&#x2F;O are:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Minimize system resources for handling a large number of concurrent
I&#x2F;O tasks&lt;&#x2F;li&gt;
&lt;li&gt;Provide a zero-cost abstraction on top of the async I&#x2F;O mechanisms
provided by operating systems&lt;&#x2F;li&gt;
&lt;li&gt;Do it at a library level, instead of introducing a runtime to Rust&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h2 id=&quot;a-sample-problem&quot;&gt;A sample problem&lt;&#x2F;h2&gt;
&lt;p&gt;It&#x27;s difficult to dive in to the wonderful world of tokio. You need to
learn about futures and streams, tasks and executors, async I&#x2F;O system
calls and the &lt;code&gt;Async&lt;&#x2F;code&gt; type, etc. To try and decouple this learning
experience, we&#x27;re going to start with a simplified problem. This
problem mostly, but not perfectly, models async I&#x2F;O in the real world,
and will demonstrate many of the design concerns. It will also let us
play just a bit more with concurrent programming before diving into
futures and tokio.&lt;&#x2F;p&gt;
&lt;p&gt;We&#x27;re going to run a separate thread. This thread will have access to
two atomic values:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;An &lt;code&gt;AtomicBool&lt;&#x2F;code&gt; to tell us whether we want this side thread to keep
running&lt;&#x2F;li&gt;
&lt;li&gt;An &lt;code&gt;AtomicUsize&lt;&#x2F;code&gt; counter&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;We haven&#x27;t covered atomic types yet, but they&#x27;re exactly what they
sound like: variables that can be safely accessed from multiple
threads. Since learning about them isn&#x27;t the point of this lesson,
I&#x27;ll defer further questions about the usage of these types to &lt;a href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;std&#x2F;sync&#x2F;atomic&#x2F;index.html&quot;&gt;their
API
documentation&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Our separate thread is going to run in a loop. As long as the
&lt;code&gt;AtomicBool&lt;&#x2F;code&gt; is &lt;code&gt;true&lt;&#x2F;code&gt;, it will:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Sleep for a given number of milliseconds&lt;&#x2F;li&gt;
&lt;li&gt;Print a message to the console&lt;&#x2F;li&gt;
&lt;li&gt;Increment the &lt;code&gt;AtomicUsize&lt;&#x2F;code&gt; counter&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;The series of examples we&#x27;ll be looking at will then try different
approaches to observing the changes in the counter.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;interval&quot;&gt;Interval&lt;&#x2F;h2&gt;
&lt;p&gt;We&#x27;re going to call this thing an &lt;code&gt;Interval&lt;&#x2F;code&gt;, since it somewhat
represents a &lt;code&gt;setInterval&lt;&#x2F;code&gt; call in Javascript.  Go ahead and start a
new project:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;$ cargo new interval --bin
$ cd interval
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We&#x27;re going to put the code for our &lt;code&gt;Interval&lt;&#x2F;code&gt; into a separate
module. First, put the following code into &lt;code&gt;src&#x2F;interval.rs&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;std::sync::Arc;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;std::thread::{sleep, spawn};
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;std::time::Duration;

#[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;derive&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(Clone)]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;pub &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;struct &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;Interval &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;counter&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: Arc&amp;lt;AtomicUsize&amp;gt;,
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;still_running&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: Arc&amp;lt;AtomicBool&amp;gt;,
}

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;impl &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;Drop &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;Interval &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;drop&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Interval thread shutting down&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;.still_running.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;store&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;false&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, Ordering::SeqCst);
    }
}

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;impl &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;Interval &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;pub &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;from_millis&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;millis&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u64&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) -&amp;gt; Interval {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; duration = Duration::from_millis(millis);

        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; counter = Arc::new(AtomicUsize::new(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;));
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; counter_clone = counter.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;clone&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;();

        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; still_running = Arc::new(AtomicBool::new(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;true&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;));
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; still_running_clone = still_running.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;clone&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;();

        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;spawn&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;move &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;|| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Interval thread launched&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;while&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; still_running_clone.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;load&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(Ordering::SeqCst) {
                &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;sleep&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(duration);
                &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; old = counter_clone.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;fetch_add&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, Ordering::SeqCst);
                &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Interval thread still alive, value was: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, old);
            }
        });

        Interval {
            counter,
            still_running,
        }
    }

    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;pub &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;get_counter&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;usize &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;.counter.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;load&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(Ordering::SeqCst)
    }
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Next, let&#x27;s provide a minimal &lt;code&gt;src&#x2F;main.rs&lt;&#x2F;code&gt; which uses this &lt;code&gt;Interval&lt;&#x2F;code&gt;
data type:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;mod &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;interval&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::interval::Interval;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; interval = Interval::from_millis(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;500&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;&#x2F;&#x2F; half a second
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; duration = std::time::Duration::from_millis(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;2000&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;&#x2F;&#x2F; 2 seconds
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; i &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;..&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;11 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Iteration number &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;, counter is &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, i, interval.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;get_counter&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;());
        std::thread::sleep(duration);
    }
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You should see something like the following:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;Iteration number 1, counter is 0
Interval thread launched
Interval thread still alive, value was: 0
Interval thread still alive, value was: 1
Interval thread still alive, value was: 2
Iteration number 2, counter is 3
Interval thread still alive, value was: 3
Interval thread still alive, value was: 4
...
Interval thread still alive, value was: 33
Interval thread still alive, value was: 34
Iteration number 10, counter is 35
Interval thread still alive, value was: 35
Interval thread still alive, value was: 36
Interval thread still alive, value was: 37
Interval thread still alive, value was: 38
Interval thread shutting down
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Hurrah, we have some concurrent communication.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;problems-with-this-approach&quot;&gt;Problems with this approach&lt;&#x2F;h2&gt;
&lt;p&gt;The first thing that jumps out as a problem is that we&#x27;re missing some updates in the main thread. Notice how the counter jumps from 0 to 3. This is obviously a problem with the interval set in the main thread: we&#x27;re delaying for 2 seconds instead of half a second. Let&#x27;s instead delay for a tenth of a second (100ms) in the main thread, and check if the value has changed since last time.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;NOTE&lt;&#x2F;strong&gt; It&#x27;s still possible we&#x27;ll miss some updates this way, since
&lt;code&gt;sleep&lt;&#x2F;code&gt; guarantees a thread will sleep &lt;em&gt;at least&lt;&#x2F;em&gt; a given amount of
time, but may sleep longer. However, by having such a large
difference, we&#x27;re fairly certain we&#x27;ll catch all of the updates.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; interval = Interval::from_millis(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;500&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;&#x2F;&#x2F; half a second
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; duration = std::time::Duration::from_millis(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;100&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;&#x2F;&#x2F; 0.1 seconds
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; last = interval.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;get_counter&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;();
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; i &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;..&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;51 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; curr = interval.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;get_counter&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;();

        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; curr != last {
            last = curr;
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Iteration number &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;, counter is &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, i, curr);
        }

        std::thread::sleep(duration);
    }
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;I had to increase the number of iterations to 50, because so many of
our main thread iterations end up showing no change in the
counter. Here&#x27;s an example of running this on my machine:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;Interval thread launched
Interval thread still alive, value was: 0
Iteration number 6, counter is 1
Interval thread still alive, value was: 1
Iteration number 11, counter is 2
Interval thread still alive, value was: 2
Iteration number 16, counter is 3
Interval thread still alive, value was: 3
Iteration number 21, counter is 4
Interval thread still alive, value was: 4
Iteration number 26, counter is 5
Interval thread still alive, value was: 5
Iteration number 31, counter is 6
Interval thread still alive, value was: 6
Iteration number 36, counter is 7
Interval thread still alive, value was: 7
Iteration number 41, counter is 8
Interval thread still alive, value was: 8
Iteration number 46, counter is 9
Interval thread still alive, value was: 9
Interval thread shutting down
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We didn&#x27;t lose any counter updates here, but from the bumps in
interval thread numbers, we can see that we&#x27;re wasting a lot of time
in the main thread checking numbers that aren&#x27;t changing.&lt;&#x2F;p&gt;
&lt;p&gt;Another problem that&#x27;s less obvious is that we&#x27;re dedicating an entire
OS thread to this sleep-and-check iteration. In our simple program,
that&#x27;s not a big deal. But imagine we decided we wanted to have 50
different similar tasks going on. It would require 49 extra threads,
most of which would sit around &lt;code&gt;sleep&lt;&#x2F;code&gt;ing the majority of the
time. That&#x27;s highly wasteful. We should be able to do better.&lt;&#x2F;p&gt;
&lt;p&gt;Finally, and perhaps least important for the moment, this is all
rather ad hoc. It seems like a common need to be able to abstract over
&amp;quot;this thing will produce a value in the future.&amp;quot; Even though this
seems like the least important problem, we&#x27;ll start by solving it
first.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;the-future-trait&quot;&gt;The Future trait&lt;&#x2F;h2&gt;
&lt;p&gt;Who&#x27;d have thought that our meandering would naturally lead to one of
the topics mentioned in this lesson&#x27;s title! You may have noticed a
pattern that developed in the &lt;code&gt;main&lt;&#x2F;code&gt; thread&#x27;s loop:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Check if a new value is available&lt;&#x2F;li&gt;
&lt;li&gt;Use it if it is available&lt;&#x2F;li&gt;
&lt;li&gt;Skip if it isn&#x27;t available&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;That&#x27;s exactly what the &lt;code&gt;Future&lt;&#x2F;code&gt; trait allows us to do, with one
addition: it also allows for error handling. We&#x27;re not going to worry
about that for now, since my code doesn&#x27;t have any errors :).&lt;&#x2F;p&gt;
&lt;p&gt;We&#x27;ll start by adding the &lt;code&gt;futures&lt;&#x2F;code&gt; crate as a dependency. In
&lt;code&gt;Cargo.toml&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-toml&quot; data-lang=&quot;toml&quot;&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;dependencies&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;futures &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;0.1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Next, let&#x27;s add a new module to provide a struct that will provide a
&lt;code&gt;Future&lt;&#x2F;code&gt; implementation. Behold, &lt;code&gt;src&#x2F;future.rs&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extern crate&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; futures;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use super&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::interval::Interval;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;futures::prelude::*;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;pub &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;struct &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;IntervalFuture &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;interval&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: Interval,
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;last&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;usize&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;,
}

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;impl &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;IntervalFuture &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;pub &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;interval&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: Interval) -&amp;gt; IntervalFuture {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; last = interval.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;get_counter&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;();
        IntervalFuture { interval, last }
    }
}

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;impl &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;Future &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;IntervalFuture &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;Item &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;usize&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;Error &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= ();

    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;poll&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) -&amp;gt; Poll&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Self::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;Item, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Self::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;Error&amp;gt; {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; curr = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;.interval.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;get_counter&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;();
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; curr == &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;.last {
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Ok&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(Async::NotReady)
        } &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;else &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;.last = curr;
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Ok&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(Async::Ready(curr))
        }
    }
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We&#x27;re going to own an &lt;code&gt;Interval&lt;&#x2F;code&gt; and the last value we provided, just
like our &lt;code&gt;main&lt;&#x2F;code&gt; loop used to. The &lt;code&gt;new&lt;&#x2F;code&gt; method is fairly
straightforward. For the &lt;code&gt;impl Future&lt;&#x2F;code&gt;, we need to define three
things:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;The thing which will be returned by this type when ready. In our
case, it&#x27;s the counter value, which is a &lt;code&gt;usize&lt;&#x2F;code&gt;.&lt;&#x2F;li&gt;
&lt;li&gt;The type of errors that can occur. We don&#x27;t have any errors, so we
use &lt;code&gt;()&lt;&#x2F;code&gt;. (The Haskeller in me screams that we should use
&lt;code&gt;Void&lt;&#x2F;code&gt;. Soon enough, we&#x27;ll be able to use
&lt;a href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;std&#x2F;primitive.never.html&quot;&gt;&lt;code&gt;never&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;.)&lt;&#x2F;li&gt;
&lt;li&gt;A function &lt;code&gt;poll&lt;&#x2F;code&gt;, which returns a &lt;code&gt;Result&lt;&#x2F;code&gt;. In the error case, this
will be our &lt;code&gt;Self::Error&lt;&#x2F;code&gt;. In the success case, this is an &lt;code&gt;Async&lt;&#x2F;code&gt;
enum type. As we can see in our method body, this is either a
&lt;code&gt;Ready&lt;&#x2F;code&gt; variant with the value, or &lt;code&gt;NotReady&lt;&#x2F;code&gt;.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;The logic in our function is the same as before, so I won&#x27;t comment on
implementation. Back in our &lt;code&gt;src&#x2F;main.rs&lt;&#x2F;code&gt;, instead of playing around
with the &lt;code&gt;curr&lt;&#x2F;code&gt;&#x2F;&lt;code&gt;last&lt;&#x2F;code&gt; logic, we can just pattern match on the result
of &lt;code&gt;poll()&lt;&#x2F;code&gt;ing:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extern crate&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; futures;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;mod &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;future&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;mod &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;interval&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::interval::Interval;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::future::IntervalFuture;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;futures::prelude::*;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; interval = Interval::from_millis(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;500&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;&#x2F;&#x2F; half a second
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; interval_future = IntervalFuture::new(interval);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; duration = std::time::Duration::from_millis(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;100&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;&#x2F;&#x2F; 0.1 seconds

    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; i &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;..&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;51 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;match&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; interval_future.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;poll&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Ok&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(Async::Ready(curr)) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
                &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Iteration number &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;, counter is &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, i, curr);
            }
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Ok&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(Async::NotReady) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(),
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Err&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(()) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt; unreachable!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(),
        }

        std::thread::sleep(duration);
    }
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Arguably a minor improvement on the previous code, though nothing
major. But congratulations, you&#x27;re now officially using the &lt;code&gt;futures&lt;&#x2F;code&gt;
crate!&lt;&#x2F;p&gt;
&lt;h3 id=&quot;the-poll-type-definition&quot;&gt;The Poll type definition&lt;&#x2F;h3&gt;
&lt;p&gt;Just a minor helper to mention. The type &lt;code&gt;Result&amp;lt;Async&amp;lt;Self::Item&amp;gt;, Self::Error&amp;gt;&lt;&#x2F;code&gt; may look a bit unwieldy to you. If so, you&#x27;ll be happy
to learn about the &lt;code&gt;Poll&lt;&#x2F;code&gt; type definition, which lets you replace the
above with &lt;code&gt;Poll&amp;lt;Self::Item, Self::Error&amp;gt;&lt;&#x2F;code&gt;. Not a big deal, but
important to recognize as you&#x27;re reading other code.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;the-tokio-executor&quot;&gt;The tokio executor&lt;&#x2F;h2&gt;
&lt;p&gt;Right now, we&#x27;re running our own executor in our &lt;code&gt;main&lt;&#x2F;code&gt; function:
we&#x27;re manually looping, delaying, etc. Besides tedium, we&#x27;ve already
mentioned some downsides to this above:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;We need a single thread per task we wish to perform&lt;&#x2F;li&gt;
&lt;li&gt;We need to implement some kind of guess-and-check thread sleeping&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;It&#x27;s time to pull out the big guns, and tokio this thing. We&#x27;ll end up
losing some functionality first, and then we&#x27;ll build it back.&lt;&#x2F;p&gt;
&lt;p&gt;First, add &lt;code&gt;tokio = &amp;quot;0.1&amp;quot;&lt;&#x2F;code&gt; to your &lt;code&gt;Cargo.toml&lt;&#x2F;code&gt;. Now, let&#x27;s try using
the tokio executor by calling &lt;code&gt;tokio::run&lt;&#x2F;code&gt; on a &lt;code&gt;Future&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extern crate&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; futures;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extern crate&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; tokio;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;mod &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;future&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;mod &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;interval&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::interval::Interval;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::future::IntervalFuture;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::prelude::*;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; interval = Interval::from_millis(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;500&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;&#x2F;&#x2F; half a second
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; interval_future = IntervalFuture::new(interval);

    tokio::run(interval_future)
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This fails with a compilation error:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;error[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;E0271&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;]: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;mismatch&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; resolving `&amp;lt;future::IntervalFuture &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;as &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;futures::Future&amp;gt;::Item == ()`
  --&amp;gt; src&#x2F;main.rs:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;15&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;5
   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;|
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;15 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;|     &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::run(interval_future)
   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;|     ^^^^^^^^^^&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; expected &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;usize&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, found ()
   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;|
   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= note: expected &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;type&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; `&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;usize&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;`
              found &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;type&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; `()`
   = note: required by `tokio::run`
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The &lt;code&gt;tokio::run&lt;&#x2F;code&gt; function expects a &lt;code&gt;Future&lt;&#x2F;code&gt; where the &lt;code&gt;Item&lt;&#x2F;code&gt; is &lt;code&gt;()&lt;&#x2F;code&gt;,
but ours is &lt;code&gt;usize&lt;&#x2F;code&gt;. This kind of makes sense anyway: don&#x27;t we want to
write some code to actually do something when we get a value?&lt;&#x2F;p&gt;
&lt;p&gt;We&#x27;re going to fix this, first the overly painful way, and then the
pleasant way. That will also help you appreciate why lesson 5 spent so
much time on closures.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;define-an-adapter-future&quot;&gt;Define an adapter Future&lt;&#x2F;h3&gt;
&lt;p&gt;Remember how you can define &lt;code&gt;Iterator&lt;&#x2F;code&gt;s that consume other &lt;code&gt;Iterator&lt;&#x2F;code&gt;s
and compose more powerful streams? Well, you can do the same thing
with &lt;code&gt;Future&lt;&#x2F;code&gt;s. Let&#x27;s define a new type that will:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Wrap around an &lt;code&gt;IntervalFuture&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;Print the new value whenever it&#x27;s ready&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;We&#x27;ll put this in &lt;code&gt;src&#x2F;main.rs&lt;&#x2F;code&gt; for now, it won&#x27;t last long anyway.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extern crate&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; futures;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extern crate&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; tokio;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;mod &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;future&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;mod &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;interval&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::interval::Interval;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::future::IntervalFuture;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::prelude::*;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;struct &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;IntervalPrinter&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(IntervalFuture);

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;impl &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;Future &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;IntervalPrinter &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;Item &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= ();
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;Error &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= ();
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;poll&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) -&amp;gt; Poll&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Self::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;Item, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Self::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;Error&amp;gt; {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;match &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;0.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;poll&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Ok&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(Async::Ready(curr)) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
                &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Counter is: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, curr);
                &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Ok&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(Async::Ready(()))
            }
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Ok&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(Async::NotReady) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt; Ok&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(Async::NotReady),
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Err&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(e) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt; Err&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(e),
        }
    }
}

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; interval = Interval::from_millis(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;500&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;&#x2F;&#x2F; half a second
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; interval_future = IntervalFuture::new(interval);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; interval_printer = IntervalPrinter(interval_future);

    tokio::run(interval_printer)
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Compile the code, but don&#x27;t run it yet. This is relatively
straight-forward given all of the types and traits we&#x27;ve seen, but
it&#x27;s obviously tedious. Let&#x27;s start off with a minor simplification.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;the-try-ready-macro&quot;&gt;The try_ready macro&lt;&#x2F;h3&gt;
&lt;p&gt;The body of poll spends a lot of lines of code to:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Pattern match&lt;&#x2F;li&gt;
&lt;li&gt;If it&#x27;s &lt;code&gt;NotReady&lt;&#x2F;code&gt;, return &lt;code&gt;NotReady&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;If it&#x27;s an &lt;code&gt;Err&lt;&#x2F;code&gt;, return the &lt;code&gt;Err&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;This is a repetitive pattern, and is pretty similar to the error
handling we saw previously in lesson 3. The futures crate provides a
macro, &lt;code&gt;try_ready!&lt;&#x2F;code&gt;, to deal with this annoyance. Add the following
above the &lt;code&gt;extern crate futures;&lt;&#x2F;code&gt; in &lt;code&gt;src&#x2F;main.rs&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;#[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;macro_use&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And then your implementation of &lt;code&gt;poll&lt;&#x2F;code&gt; can be simplified to:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; curr = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;try_ready!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;0.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;poll&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;());
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Counter is: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, curr);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Ok&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(Async::Ready(()))
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Nice! Compile, and again, don&#x27;t run. (Getting curious yet why I keep
saying that? We&#x27;ll find out soon, just one more pitstop first.)&lt;&#x2F;p&gt;
&lt;h3 id=&quot;i-need-some-closure&quot;&gt;I need some closure&lt;&#x2F;h3&gt;
&lt;p&gt;It&#x27;s amazing I&#x27;ve made it to lesson 7 in this crash course without
making that pun. Obviously, defining an entire struct and &lt;code&gt;Future&lt;&#x2F;code&gt;
implementation is a bit overkill to just print a line. Fortunately,
the authors of the &lt;code&gt;futures&lt;&#x2F;code&gt; crate noticed this too. There are a
number of combinators built into the &lt;code&gt;Future&lt;&#x2F;code&gt; trait that make it easy
to chain things together. We&#x27;re going to use the &lt;code&gt;and_then&lt;&#x2F;code&gt; method:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extern crate&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; futures;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extern crate&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; tokio;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;mod &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;future&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;mod &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;interval&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::interval::Interval;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::future::IntervalFuture;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::prelude::*;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; interval = Interval::from_millis(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;500&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;&#x2F;&#x2F; half a second
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; interval_future = IntervalFuture::new(interval);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; interval_printer = interval_future.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;and_then&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;curr&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Counter is: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, curr);
        futures::future::ok(())
    });

    tokio::run(interval_printer)
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;That&#x27;s much nicer! If you&#x27;re anything like me though, the
&lt;code&gt;futures::future::ok(())&lt;&#x2F;code&gt; is bothering you. What purpose does it serve
there? This is a vital part of the design of futures, which we&#x27;ll be
taking advantage of quite a bit going forward. It allows us to create
chains of actions to run as each bit of async I&#x2F;O completes. For now,
we don&#x27;t want to do anything else after we print the first value from
the counter, so we just return &lt;code&gt;futures::future::ok(())&lt;&#x2F;code&gt;, which means
&amp;quot;don&#x27;t do anything, and return the item &lt;code&gt;()&lt;&#x2F;code&gt;&amp;quot;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;Exercise 1&lt;&#x2F;strong&gt; There&#x27;s another method, &lt;code&gt;.map&lt;&#x2F;code&gt;, which is actually a
better choice for us here than &lt;code&gt;.and_then&lt;&#x2F;code&gt;. Try rewriting the code
above to use &lt;code&gt;.map&lt;&#x2F;code&gt;. Note: no solution provided to this one.&lt;&#x2F;p&gt;
&lt;p&gt;Out of curiosity, what&#x27;s the type of &lt;code&gt;interval_printer&lt;&#x2F;code&gt;? Let&#x27;s use the
dumb trick from before of giving it the wrong type. Insert something
silly like &lt;code&gt;let interval_printer: bool = ...&lt;&#x2F;code&gt; and try compiling,
you&#x27;ll get some type like:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;futures::AndThen&amp;lt;
  future::IntervalFuture,
  futures::FutureResult&amp;lt;(), ()&amp;gt;,
  [closure@src&#x2F;main.rs:14:59: 17:6]
&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If this is starting to look a bit like &lt;code&gt;Iterator&lt;&#x2F;code&gt; types, that&#x27;s by
design. Just like &lt;code&gt;Iterator&lt;&#x2F;code&gt;s, &lt;code&gt;Future&lt;&#x2F;code&gt;s capture a large amount of
information in the types themselves about what they&#x27;ll do. This allows
&lt;code&gt;Future&lt;&#x2F;code&gt;s to compile down to highly efficient code, living up to the
Rust mantra of zero-cost abstractions.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;finally-run-it&quot;&gt;Finally, run it!&lt;&#x2F;h3&gt;
&lt;p&gt;Is the suspense killing you? Alright, your moment has arrived. What
happens when you run &lt;code&gt;cargo run&lt;&#x2F;code&gt;?&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;$ cargo run
Interval thread launched
Interval thread shutting down
Interval thread still alive, value was: 0
...
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And that&#x27;s it. It hangs. It doesn&#x27;t print out the message we
painstakingly added with &lt;code&gt;and_then&lt;&#x2F;code&gt;. I&#x27;m a complete failure, my life
is in ruins.&lt;&#x2F;p&gt;
&lt;p&gt;After an hour to myself to contemplate my life and a few shots of
whiskey, things started to get clear. In our original implementation,
we had a really wasteful loop in the main function. It kept checking
if the counter had changed. We did that with our &lt;code&gt;Future&lt;&#x2F;code&gt;
implementation too at first, sleeping and then checking again for a
&lt;code&gt;NotReady&lt;&#x2F;code&gt;. But tokio probably &lt;em&gt;isn&#x27;t&lt;&#x2F;em&gt; doing that, right? (The answer
is yes, right.)&lt;&#x2F;p&gt;
&lt;p&gt;Instead of doing the silly wasteful thing, the &lt;code&gt;futures&lt;&#x2F;code&gt; crate is far
smarter. It has a mechanism to:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;determine which task is trying to get access to the data provided by
this future, and then&lt;&#x2F;li&gt;
&lt;li&gt;notify that task that new data is available&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;The
&lt;a href=&quot;https:&#x2F;&#x2F;docs.rs&#x2F;futures&#x2F;0.1&#x2F;futures&#x2F;task&#x2F;fn.current.html&quot;&gt;&lt;code&gt;futures::task::current()&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;
function gives us the current task being run, as a
&lt;a href=&quot;https:&#x2F;&#x2F;docs.rs&#x2F;futures&#x2F;0.1&#x2F;futures&#x2F;task&#x2F;struct.Task.html&quot;&gt;&lt;code&gt;Task&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;
struct. That struct has a method,
&lt;a href=&quot;https:&#x2F;&#x2F;docs.rs&#x2F;futures&#x2F;0.1&#x2F;futures&#x2F;task&#x2F;struct.Task.html#method.notify&quot;&gt;&lt;code&gt;notify&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;,
to let the task know that more data is available.&lt;&#x2F;p&gt;
&lt;p&gt;In our program, we have the logic split between &lt;code&gt;Interval&lt;&#x2F;code&gt; and
&lt;code&gt;IntervalFuture&lt;&#x2F;code&gt;. &lt;code&gt;IntervalFuture&lt;&#x2F;code&gt; will need to be responsible for
calling the &lt;code&gt;current()&lt;&#x2F;code&gt; function (give some thought as to why that&#x27;s
the case). The changes needed to make this work are:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Add a new field to &lt;code&gt;Interval&lt;&#x2F;code&gt; to hold an &lt;code&gt;Arc&amp;lt;Mutex&amp;lt;Option&amp;lt;Task&amp;gt;&amp;gt;&amp;gt;&lt;&#x2F;code&gt;
(yes, that&#x27;s a mouthful), and initialize correctly.&lt;&#x2F;li&gt;
&lt;li&gt;Each time we call &lt;code&gt;fetch_add&lt;&#x2F;code&gt; and update the counter, also call
&lt;code&gt;notify()&lt;&#x2F;code&gt; on that task, if it&#x27;s there.&lt;&#x2F;li&gt;
&lt;li&gt;Provide a method &lt;code&gt;set_task&lt;&#x2F;code&gt; to set the &lt;code&gt;Task&lt;&#x2F;code&gt; on an &lt;code&gt;Interval&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;When returning &lt;code&gt;NotReady&lt;&#x2F;code&gt; in &lt;code&gt;IntervalFuture&lt;&#x2F;code&gt;, call &lt;code&gt;set_task&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;&lt;strong&gt;Exercise 2&lt;&#x2F;strong&gt; Take a stab at implementing these four changes before
looking at the solution below. A hint on some features of Rust we
haven&#x27;t covered yet: you&#x27;ll end up wanting to pattern match on the
&lt;code&gt;Option&lt;&#x2F;code&gt; held inside the &lt;code&gt;Mutex&lt;&#x2F;code&gt;. You&#x27;ll want to pattern match by
reference, which will require some code that looks like &lt;code&gt;Some(ref task) =&amp;gt;&lt;&#x2F;code&gt;. And the final output should look like:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;Interval thread launched
Interval thread still alive, value was: 0
Counter is: 1
Interval thread shutting down
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If you want to be sure, you can see the &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;snoyberg&#x2F;rush-crash-course-tokio-exercise-2&#x2F;tree&#x2F;03c5b9029263ce36e626e877e986a281c9334d4e&quot;&gt;initial version of the code on Github&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;solution-2&quot;&gt;Solution 2&lt;&#x2F;h3&gt;
&lt;p&gt;You can &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;snoyberg&#x2F;rush-crash-course-tokio-exercise-2&#x2F;commit&#x2F;1c58bf4df946763f5f9f0773235e975968260ba9&quot;&gt;check out the
diff&lt;&#x2F;a&gt;
and &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;snoyberg&#x2F;rush-crash-course-tokio-exercise-2&#x2F;tree&#x2F;1c58bf4df946763f5f9f0773235e975968260ba9&quot;&gt;full
solution&lt;&#x2F;a&gt;
on Github. Here&#x27;s the diff included inline:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-diff&quot; data-lang=&quot;diff&quot;&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;diff --git a&#x2F;src&#x2F;future.rs b&#x2F;src&#x2F;future.rs
index 9aaee3c..e231e7b 100644
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;--- a&#x2F;src&#x2F;future.rs
+++ b&#x2F;src&#x2F;future.rs
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;@@ -22,6 +22,8 @@ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;impl Future for IntervalFuture {
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;     fn poll(&amp;amp;mut self) -&amp;gt; Result&amp;lt;Async&amp;lt;Self::Item&amp;gt;, Self::Error&amp;gt; {
         let curr = self.interval.get_counter();
         if curr == self.last {
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;+            let task = futures::task::current();
+            self.interval.set_task(task);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;             Ok(Async::NotReady)
         } else {
             self.last = curr;
diff --git a&#x2F;src&#x2F;interval.rs b&#x2F;src&#x2F;interval.rs
index 044e2ca..8013ac6 100644
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;--- a&#x2F;src&#x2F;interval.rs
+++ b&#x2F;src&#x2F;interval.rs
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;@@ -1,12 +1,14 @@
 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#dc322f;&quot;&gt;-use std::sync::Arc;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;+use std::sync::{Arc, Mutex};
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; use std::thread::{sleep, spawn};
 use std::time::Duration;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;+use futures::task::Task;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; #[derive(Clone)]
 pub struct Interval {
     counter: Arc&amp;lt;AtomicUsize&amp;gt;,
     still_running: Arc&amp;lt;AtomicBool&amp;gt;,
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;+    task: Arc&amp;lt;Mutex&amp;lt;Option&amp;lt;Task&amp;gt;&amp;gt;&amp;gt;,
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; }

 impl Drop for Interval {
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;@@ -26,22 +28,37 @@ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;impl Interval {
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;         let still_running = Arc::new(AtomicBool::new(true));
         let still_running_clone = still_running.clone();

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;+        let task: Arc&amp;lt;Mutex&amp;lt;Option&amp;lt;Task&amp;gt;&amp;gt;&amp;gt; = Arc::new(Mutex::new(None));
+        let task_clone = task.clone();
+
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;         spawn(move || {
             println!(&amp;quot;Interval thread launched&amp;quot;);
             while still_running_clone.load(Ordering::SeqCst) {
                 sleep(duration);
                 let old = counter_clone.fetch_add(1, Ordering::SeqCst);
                 println!(&amp;quot;Interval thread still alive, value was: {}&amp;quot;, old);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;+
+                let task = task_clone.lock().unwrap();
+                match *task {
+                    None =&amp;gt; (),
+                    Some(ref task) =&amp;gt; task.notify(),
+                };
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;             }
         });

         Interval {
             counter,
             still_running,
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;+            task,
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;         }
     }

     pub fn get_counter(&amp;amp;self) -&amp;gt; usize {
         self.counter.load(Ordering::SeqCst)
     }
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;+
+    pub fn set_task(&amp;amp;mut self, task: Task) {
+        let mut guard = self.task.lock().unwrap();
+        *guard = Some(task);
+    }
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; }
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Hurrah, we finally have a working tokio program!&lt;&#x2F;p&gt;
&lt;p&gt;In case you&#x27;re worried about how complex that was, don&#x27;t
be. Notification is a vital aspect of how tokio works
internally. However, in most cases, you won&#x27;t be creating your own
primitive &lt;code&gt;Future&lt;&#x2F;code&gt;s, but instead dealing with existing ones provided
by tokio or other libraries. Those existing &lt;code&gt;Future&lt;&#x2F;code&gt;s will provide the
necessary notification logic. You&#x27;ll simply need to obey this one
rule:&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;Only return a &lt;code&gt;NotReady&lt;&#x2F;code&gt; from a &lt;code&gt;poll&lt;&#x2F;code&gt; function if you received a
&lt;code&gt;NotReady&lt;&#x2F;code&gt; from an underlying &lt;code&gt;Future&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;h2 id=&quot;just-one-value&quot;&gt;Just one value?&lt;&#x2F;h2&gt;
&lt;p&gt;It&#x27;s a bit disappointing that our wonderful long running counter only
ends up printing a single value. Can we create some kind of a loop? A
simple approach like the following doesn&#x27;t work:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; interval_printer = interval_future.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;and_then&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;curr&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Counter is: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, curr);
    interval_printer
});
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This isn&#x27;t Haskell, we can&#x27;t recursively refer to &lt;code&gt;interval_printer&lt;&#x2F;code&gt;
we&#x27;re in the middle of defining. Go ahead and take a few other stabs
at doing something like that, and you&#x27;ll eventually get frustrated and
go back to the whiskey. Digging through the &lt;code&gt;futures&lt;&#x2F;code&gt; docs, a helper
function like
&lt;a href=&quot;https:&#x2F;&#x2F;docs.rs&#x2F;futures&#x2F;0.1.25&#x2F;futures&#x2F;future&#x2F;fn.loop_fn.html&quot;&gt;&lt;code&gt;loop_fn&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;
looks promising, but I didn&#x27;t see a simple way to make it work in this
case. (Please let me know if I missed something!) I ended up with
something wonky like this before stopping:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; interval = Interval::from_millis(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;500&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;&#x2F;&#x2F; half a second
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; interval_future = Arc::new(Mutex::new(IntervalFuture::new(interval)));
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; interval_printer = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;loop_fn&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(interval_future, |&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;interval_future&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; interval_future_clone = interval_future.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;clone&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;();
        interval_future.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;lock&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;().&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;().&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;and_then&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;curr&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| {
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Counter: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, curr);
            futures::future::ok(Continue(interval_future_clone))
        })
    });

    tokio::run(interval_printer)
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;another-struct&quot;&gt;Another struct!&lt;&#x2F;h3&gt;
&lt;p&gt;Like before, we&#x27;re going to define another helper type to implement
this concept of looping. Then we&#x27;ll see that this problem has already
been solved better in the &lt;code&gt;futures&lt;&#x2F;code&gt; crate itself, but we&#x27;ll get there
soon.&lt;&#x2F;p&gt;
&lt;p&gt;We want to define a new struct, &lt;code&gt;KeepPrinting&lt;&#x2F;code&gt;, which is a newtype
around an &lt;code&gt;IntervalFuture&lt;&#x2F;code&gt;. It&#x27;s going to:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Have a &lt;code&gt;Future&lt;&#x2F;code&gt; implementation&lt;&#x2F;li&gt;
&lt;li&gt;Have &lt;code&gt;Item = ()&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;Use a &lt;code&gt;loop&lt;&#x2F;code&gt; in its implementation&lt;&#x2F;li&gt;
&lt;li&gt;Use the &lt;code&gt;try_ready!&lt;&#x2F;code&gt; macro&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;&lt;strong&gt;Exercise 3&lt;&#x2F;strong&gt; Try implementing &lt;code&gt;KeepPrinting&lt;&#x2F;code&gt; and using it in the
&lt;code&gt;main&lt;&#x2F;code&gt; function. Solution follows immediately, but try not to cheat!&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;#[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;macro_use&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extern crate&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; futures;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extern crate&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; tokio;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;mod &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;future&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;mod &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;interval&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::future::IntervalFuture;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::interval::Interval;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::prelude::*;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;struct &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;KeepPrinting&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(IntervalFuture);

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;impl &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;Future &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;KeepPrinting &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;Item &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= ();
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;Error &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= ();
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;poll&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) -&amp;gt; Poll&amp;lt;(), ()&amp;gt; {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;loop &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; curr = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;try_ready!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;0.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;poll&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;());
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Counter: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, curr);
        }
    }
}

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; interval = Interval::from_millis(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;500&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;&#x2F;&#x2F; half a second
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; interval_future = IntervalFuture::new(interval);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; keep_printing = KeepPrinting(interval_future);

    tokio::run(keep_printing)
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And, just like that, we get an infinitely looping program. This almost
looks like something that we could have done with &lt;code&gt;Iterator&lt;&#x2F;code&gt;s. Which
makes me wonder... is there something like &lt;code&gt;Iterator&lt;&#x2F;code&gt;s in &lt;code&gt;futures&lt;&#x2F;code&gt;?&lt;&#x2F;p&gt;
&lt;h2 id=&quot;streams&quot;&gt;Streams&lt;&#x2F;h2&gt;
&lt;p&gt;A &lt;code&gt;Future&lt;&#x2F;code&gt; is an action with a delayed single result. A &lt;code&gt;Stream&lt;&#x2F;code&gt; is a
stream of results, like an &lt;code&gt;Iterator&lt;&#x2F;code&gt;, with a delay between each
value.&lt;&#x2F;p&gt;
&lt;p&gt;In &lt;code&gt;src&#x2F;main.rs&lt;&#x2F;code&gt;, add &lt;code&gt;mod stream;&lt;&#x2F;code&gt; and then edit &lt;code&gt;src&#x2F;stream.rs&lt;&#x2F;code&gt;. The
file will end up looking remarkably similar to &lt;code&gt;src&#x2F;future.rs&lt;&#x2F;code&gt;,
except:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Call the struct &lt;code&gt;IntervalStream&lt;&#x2F;code&gt; instead of &lt;code&gt;IntervalFuture&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;Provide an &lt;code&gt;impl Stream for IntervalStream&lt;&#x2F;code&gt; instead of &lt;code&gt;impl Future&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;Follow the compiler errors to fix it&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;Within the &lt;code&gt;main&lt;&#x2F;code&gt; function, instead of using &lt;code&gt;KeepPrinting&lt;&#x2F;code&gt; or
anything else, we&#x27;ll want to create an &lt;code&gt;IntervalStream&lt;&#x2F;code&gt;
value. However, &lt;code&gt;tokio::run&lt;&#x2F;code&gt; needs a &lt;code&gt;Future&lt;&#x2F;code&gt;, not a &lt;code&gt;Stream&lt;&#x2F;code&gt;, to
run. Fortunately, there&#x27;s a helper function, &lt;code&gt;for_each&lt;&#x2F;code&gt;, that runs a
given &lt;code&gt;closure&lt;&#x2F;code&gt; on each value in the stream.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;Exercise 4&lt;&#x2F;strong&gt; Try to implement &lt;code&gt;src&#x2F;stream.rs&lt;&#x2F;code&gt; and
&lt;code&gt;src&#x2F;main.rs&lt;&#x2F;code&gt;. Solution to follow.&lt;&#x2F;p&gt;
&lt;p&gt;The trickiest bit for me when first learning &lt;code&gt;for_each&lt;&#x2F;code&gt; was to realize
that, like &lt;code&gt;and_then&lt;&#x2F;code&gt;, it needs to end with a &lt;code&gt;Future&lt;&#x2F;code&gt;. I don&#x27;t know
if that was just my own shortcoming, or a common issue. In any event,
if you struggled to realize you needed something like &lt;code&gt;future::ok(())&lt;&#x2F;code&gt;
at the end of your closure, you&#x27;re in good company.&lt;&#x2F;p&gt;
&lt;p&gt;In addition, the &lt;code&gt;poll&lt;&#x2F;code&gt; function for a &lt;code&gt;Stream&lt;&#x2F;code&gt; is slightly different,
in returning an &lt;code&gt;Option&amp;lt;Item&amp;gt;&lt;&#x2F;code&gt;. This is similar to how &lt;code&gt;Iterator&lt;&#x2F;code&gt;
works. In our case, we have an infinite stream, so we never provide
the &lt;code&gt;None&lt;&#x2F;code&gt; case.&lt;&#x2F;p&gt;
&lt;p&gt;Anyway, here&#x27;s &lt;code&gt;src&#x2F;stream.rs&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extern crate&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; futures;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use super&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::interval::Interval;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;futures::prelude::*;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;pub &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;struct &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;IntervalStream &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;interval&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: Interval,
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;last&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;usize&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;,
}

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;impl &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;IntervalStream &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;pub &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;interval&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: Interval) -&amp;gt; IntervalStream {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; last = interval.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;get_counter&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;();
        IntervalStream { interval, last }
    }
}

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;impl &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;Stream &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;IntervalStream &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;Item &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;usize&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;Error &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= ();

    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;poll&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) -&amp;gt; Poll&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Option&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Self::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;Item&amp;gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Self::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;Error&amp;gt; {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; curr = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;.interval.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;get_counter&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;();
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; curr == &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;.last {
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; task = futures::task::current();
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;.interval.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;set_task&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(task);
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Ok&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(Async::NotReady)
        } &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;else &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;.last = curr;
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Ok&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(Async::Ready(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(curr)))
        }
    }
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And &lt;code&gt;src&#x2F;main.rs&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extern crate&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; futures;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extern crate&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; tokio;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;mod &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;interval&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;mod &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;stream&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::interval::Interval;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::stream::IntervalStream;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::prelude::*;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; interval = Interval::from_millis(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;500&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;&#x2F;&#x2F; half a second
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; interval_stream = IntervalStream::new(interval);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; future = interval_stream.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for_each&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;curr&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Counter: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, curr);
        futures::future::ok(())
    });

    tokio::run(future)
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;strong&gt;Exercise 5&lt;&#x2F;strong&gt; Like &lt;code&gt;Iterator&lt;&#x2F;code&gt;s, &lt;code&gt;Stream&lt;&#x2F;code&gt;s have helper methods that
you can use to build up more complex things. For example, try throwing
in &lt;code&gt;map&lt;&#x2F;code&gt; and &lt;code&gt;take&lt;&#x2F;code&gt; to print only the first 10 counter values, but
double them before printing. (No solution provided.)&lt;&#x2F;p&gt;
&lt;p&gt;This is all beginning to fit together nicely! While there are still
details to learn in the &lt;code&gt;futures&lt;&#x2F;code&gt; crate, you&#x27;ve got most of the big
ideas down. The next bit is to get familiar with the API in tokio, but
relatively speaking this is less mind-bending. To hammer home what
we&#x27;ve done so far, we&#x27;ll hit a few exercises, and then continue with
tokio.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;exercise-6&quot;&gt;Exercise 6&lt;&#x2F;h2&gt;
&lt;p&gt;Define a new struct &lt;code&gt;MyOk&lt;&#x2F;code&gt; such that this &lt;code&gt;main&lt;&#x2F;code&gt; function works:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; name = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::from(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Alice&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; future = MyOk::new(name).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;and_then&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Name: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, name);
        MyOk::new(())
    });

    tokio::run(future)
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Hint: before cheating and looking at the solution, here&#x27;s one piece of
help: you&#x27;ll want an &lt;code&gt;Option&lt;&#x2F;code&gt; inside the &lt;code&gt;MyOk&lt;&#x2F;code&gt; newtype, and it&#x27;s
invalid to call &lt;code&gt;poll&lt;&#x2F;code&gt; on it twice.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;solution-6&quot;&gt;Solution 6&lt;&#x2F;h3&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;struct &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;MyOk&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;T&amp;gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Option&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;T&amp;gt;);

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;impl&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;T&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;MyOk&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;T&amp;gt; {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;t&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: T) -&amp;gt; MyOk&amp;lt;T&amp;gt; {
        MyOk(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(t))
    }
}

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;impl&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;T&amp;gt; Future &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;MyOk&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;T&amp;gt; {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;Item &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= T;
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;Error &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= ();
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;poll&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) -&amp;gt; Poll&amp;lt;T, ()&amp;gt; {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Ok&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(Async::Ready(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;0.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;take&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;().&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()))
    }
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;exercise-7&quot;&gt;Exercise 7&lt;&#x2F;h2&gt;
&lt;p&gt;Use &lt;code&gt;iter_ok&lt;&#x2F;code&gt; to convert the range &lt;code&gt;1..11&lt;&#x2F;code&gt; to a &lt;code&gt;Stream&lt;&#x2F;code&gt;, and then
collect it as a &lt;code&gt;Vec&lt;&#x2F;code&gt; and print it.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;solution-7&quot;&gt;Solution 7&lt;&#x2F;h3&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    tokio::run(stream::iter_ok(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;..&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;11&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;collect&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;().&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;and_then&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;x&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{:?}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, x);
        future::ok(())
    }))
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;async-i-o&quot;&gt;Async I&#x2F;O&lt;&#x2F;h2&gt;
&lt;p&gt;We&#x27;ve played around with the &lt;code&gt;futures&lt;&#x2F;code&gt; crate by creating a fake async
I&#x2F;O source of data (the &lt;code&gt;Interval&lt;&#x2F;code&gt;). We&#x27;ve built up &lt;code&gt;Future&lt;&#x2F;code&gt;s and
&lt;code&gt;Stream&lt;&#x2F;code&gt;s in that world. And we&#x27;ve used tokio&#x27;s executor to run these
things. It&#x27;s now time to take it to use some real async I&#x2F;O.&lt;&#x2F;p&gt;
&lt;p&gt;Most async I&#x2F;O we care about will end up being network
traffic. Filesystem operations don&#x27;t always play nicely with async I&#x2F;O
at an operating system level. That said, to get our feet wet, let&#x27;s
play with a filesystem based example.&lt;&#x2F;p&gt;
&lt;p&gt;You&#x27;ll want to look at the docs quite a bit, you can &lt;a href=&quot;https:&#x2F;&#x2F;docs.rs&#x2F;tokio&#x2F;0.1.11&#x2F;tokio&#x2F;&quot;&gt;find them on
docs.rs&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;list-files-in-a-directory&quot;&gt;List files in a directory&lt;&#x2F;h2&gt;
&lt;p&gt;If you look through the docs above, you may find the function
&lt;a href=&quot;https:&#x2F;&#x2F;docs.rs&#x2F;tokio&#x2F;0.1.11&#x2F;tokio&#x2F;fs&#x2F;fn.read_dir.html&quot;&gt;&lt;code&gt;read_dir&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;. It
takes a path, and returns a &lt;code&gt;ReadDirFuture&lt;&#x2F;code&gt;. This is a standard
approach in tokio, like we had with &lt;code&gt;Iterator&lt;&#x2F;code&gt;s: simple wrapper
functions providing access to the structs that do the heavy
lifting. One thing to get used to in tokio is how to read these docs.&lt;&#x2F;p&gt;
&lt;p&gt;Click through on the &lt;code&gt;ReadDirFuture&lt;&#x2F;code&gt; struct. It has a &lt;code&gt;Future&lt;&#x2F;code&gt;
implementation, where &lt;code&gt;Item&lt;&#x2F;code&gt; is &lt;code&gt;ReadDir&lt;&#x2F;code&gt;, and &lt;code&gt;Error&lt;&#x2F;code&gt; is
&lt;code&gt;std::io::Error&lt;&#x2F;code&gt;. Before we deal with that &lt;code&gt;ReadDir&lt;&#x2F;code&gt;, let&#x27;s just get
something that compiles. Since this is still a crash course, we&#x27;ll
bash our heads against a brick wall each step of the way.&lt;&#x2F;p&gt;
&lt;p&gt;First, to call &lt;code&gt;read_dir&lt;&#x2F;code&gt;, we need a directory. Let&#x27;s use &lt;code&gt;&amp;quot;.&amp;quot;&lt;&#x2F;code&gt; (the
current directory). We&#x27;ll use command line arguments later. Here&#x27;s a
naive implementation:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extern crate&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; tokio;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::prelude::*;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::fs;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; future = fs::read_dir(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
    tokio::run(future)
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This gives us a somewhat intimidating error message:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;error[E0271]: type mismatch resolving `&amp;lt;tokio_fs::read_dir::ReadDirFuture&amp;lt;&amp;amp;str&amp;gt; as tokio::prelude::Future&amp;gt;::Item == ()`
 --&amp;gt; src&#x2F;main.rs:8:5
  |
8 |     tokio::run(future)
  |     ^^^^^^^^^^ expected struct `tokio_fs::read_dir::ReadDir`, found ()
  |
  = note: expected type `tokio_fs::read_dir::ReadDir`
             found type `()`
  = note: required by `tokio::run`

error[E0271]: type mismatch resolving `&amp;lt;tokio_fs::read_dir::ReadDirFuture&amp;lt;&amp;amp;str&amp;gt; as tokio::prelude::Future&amp;gt;::Error == ()`
 --&amp;gt; src&#x2F;main.rs:8:5
  |
8 |     tokio::run(future)
  |     ^^^^^^^^^^ expected struct `std::io::Error`, found ()
  |
  = note: expected type `std::io::Error`
             found type `()`
  = note: required by `tokio::run`
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;However, let me narrow that down for you:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;expected type `tokio_fs::read_dir::ReadDir`, found type `()`
expected type `std::io::Error`, found type `()`
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Oh, right! &lt;code&gt;tokio::run&lt;&#x2F;code&gt; requires that we have &lt;code&gt;Item&lt;&#x2F;code&gt; and &lt;code&gt;Error&lt;&#x2F;code&gt; as
&lt;code&gt;()&lt;&#x2F;code&gt;. We can modify the &lt;code&gt;Error&lt;&#x2F;code&gt; with &lt;code&gt;map_err&lt;&#x2F;code&gt;. Let&#x27;s just print out
the error if one occurs:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; future = fs::read_dir(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
    .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;map_err&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;e&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;eprintln!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Error reading directory: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, e))
    ;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;That knocked out the first compilation error. Let&#x27;s also throw in a
&lt;code&gt;.and_then&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;and_then&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;readdir&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;FIXME: use this: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{:?}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, readdir);
})
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Uh oh, we got this compilation error. Can you figure out how to solve it?&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;error[E0277]: the trait bound `(): tokio::prelude::Future` is not satisfied
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In my experience, when you see that, it almost always means: &amp;quot;I forgot
to add &lt;code&gt;future::ok(())&lt;&#x2F;code&gt;. Remember, &lt;code&gt;and_then&lt;&#x2F;code&gt; needs to end with the
next &lt;code&gt;Future&lt;&#x2F;code&gt; to run. Add that line, and your code should
compile. Running produces the output:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;FIXME: use this: ReadDir(ReadDir(&amp;quot;.&amp;quot;))
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Cool! Now it&#x27;s time to look at the &lt;a href=&quot;https:&#x2F;&#x2F;docs.rs&#x2F;tokio-fs&#x2F;0.1.3&#x2F;tokio_fs&#x2F;struct.ReadDir.html&quot;&gt;docs for
&lt;code&gt;ReadDir&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;. Instead
of a &lt;code&gt;Future&lt;&#x2F;code&gt; implementation, this has a &lt;code&gt;Stream&lt;&#x2F;code&gt;. Let&#x27;s shove a
&lt;code&gt;for_each&lt;&#x2F;code&gt; in there and see what happens.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;Challenge&lt;&#x2F;strong&gt; try to guess the errors in the code below before you compile it.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;and_then&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;readdir&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| {
    readdir
        .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for_each&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;entry&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| {
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{:?}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, entry.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;path&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;());
        })
})
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;There are two problems with this code:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;It leaves an error type of &lt;code&gt;std::io::Error&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;It doesn&#x27;t include &lt;code&gt;future::ok(())&lt;&#x2F;code&gt; at the end of the closure
provided to &lt;code&gt;for_each&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;Go ahead and fix those problems. To be sure we&#x27;re on the same page,
here&#x27;s my solution which compiles and runs successfully:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extern crate&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; tokio;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::prelude::*;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::fs;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; future = fs::read_dir(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
        .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;map_err&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;e&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;eprintln!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Error reading directory: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, e))
        .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;and_then&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;readdir&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| {
            readdir
                .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;map_err&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;e&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;eprintln!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Error reading directory: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, e))
                .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for_each&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;entry&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| {
                    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{:?}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, entry.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;path&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;());
                    future::ok(())
                })
        })
        ;
    tokio::run(future)
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;duplicated-error-handling&quot;&gt;Duplicated error handling&lt;&#x2F;h3&gt;
&lt;p&gt;It&#x27;s a bit irritating that we have two identical &lt;code&gt;map_err&lt;&#x2F;code&gt; calls. We
have two different sources of errors: the initial &lt;code&gt;read_dir&lt;&#x2F;code&gt; &lt;code&gt;Future&lt;&#x2F;code&gt;,
and then streaming the individual &lt;code&gt;DirEntry&lt;&#x2F;code&gt;s from it. However, the
type of the errors in both cases is the same:
&lt;code&gt;std::io::Error&lt;&#x2F;code&gt;. Therefore, we can move our error handling to the
end, and just do it once:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; future = fs::read_dir(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
        .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;and_then&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;readdir&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| {
            readdir
                .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for_each&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;entry&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| {
                    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{:?}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, entry.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;path&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;());
                    future::ok(())
                })
        })
        .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;map_err&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;e&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;eprintln!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Error reading directory: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, e))
        ;
    tokio::run(future)
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;flattening&quot;&gt;Flattening&lt;&#x2F;h3&gt;
&lt;p&gt;It turns out that it&#x27;s common enough to have a &lt;code&gt;Future&lt;&#x2F;code&gt; that generates
another &lt;code&gt;Future&lt;&#x2F;code&gt;, and then we want to run that second &lt;code&gt;Future&lt;&#x2F;code&gt;, that
there&#x27;s a helper method for it &lt;code&gt;flatten()&lt;&#x2F;code&gt;. There&#x27;s &lt;em&gt;also&lt;&#x2F;em&gt; a
&lt;a href=&quot;https:&#x2F;&#x2F;docs.rs&#x2F;futures&#x2F;0.1.23&#x2F;futures&#x2F;future&#x2F;trait.Future.html#method.flatten_stream&quot;&gt;&lt;code&gt;flatten_stream()&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;
that does the same thing when a &lt;code&gt;Future&lt;&#x2F;code&gt; gives us a &lt;code&gt;Stream&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;Exercise 8&lt;&#x2F;strong&gt; Rewrite the code above to use &lt;code&gt;flatten_stream&lt;&#x2F;code&gt;. You
should end up with &lt;em&gt;no calls to &lt;code&gt;and_then&lt;&#x2F;code&gt;&lt;&#x2F;em&gt;. Solution follows
immediately:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extern crate&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; tokio;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::prelude::*;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::fs;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; future = fs::read_dir(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
        .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;flatten_stream&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()
        .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for_each&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;entry&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| {
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{:?}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, entry.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;path&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;());
            future::ok(())
        })
        .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;map_err&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;e&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;eprintln!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Error reading directory: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, e))
        ;
    tokio::run(future)
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;command-line-arguments&quot;&gt;Command line arguments&lt;&#x2F;h3&gt;
&lt;p&gt;It&#x27;s somewhat boring to always print out what&#x27;s in the current
directory. Instead, let&#x27;s take all of the command line arguments
(skipping the first, which is the executable name), and list the
directory contents. We&#x27;ll use &lt;code&gt;stream::iter_ok&lt;&#x2F;code&gt; to convert the &lt;code&gt;Args&lt;&#x2F;code&gt;
&lt;code&gt;Iterator&lt;&#x2F;code&gt; into a &lt;code&gt;Stream&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extern crate&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; tokio;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::prelude::*;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::fs;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;std::env::args;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; future = stream::iter_ok(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;args&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;())
        .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;skip&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
        .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for_each&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;dir&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| {
            fs::read_dir(dir)
                .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;flatten_stream&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()
                .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for_each&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;entry&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| {
                    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{:?}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, entry.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;path&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;());
                    future::ok(())
                })
        })
        .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;map_err&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;e&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;eprintln!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Error reading directory: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, e))
        ;
    tokio::run(future)
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Unfortunately, this doesn&#x27;t compile. The full error message is large
(I encourage you to check it out yourself), but the first few lines
are sufficient to find the problem:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;error[E0277]: `std::env::Args` cannot be sent between threads safely
  --&amp;gt; src&#x2F;main.rs:20:5
   |
20 |     tokio::run(future)
   |     ^^^^^^^^^^ `std::env::Args` cannot be sent between threads safely
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Oh, darn. &lt;code&gt;Args&lt;&#x2F;code&gt; isn&#x27;t thread safe, and so cannot be converted into a
&lt;code&gt;Stream&lt;&#x2F;code&gt;. Fair enough: vectors to the rescue!&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;Exercise 9&lt;&#x2F;strong&gt; Create a vector of arguments before the definition of
&lt;code&gt;future&lt;&#x2F;code&gt; and use that.&lt;&#x2F;p&gt;
&lt;p&gt;Solution:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extern crate&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; tokio;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::prelude::*;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::fs;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;std::env::args;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; args: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Vec&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;gt; = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;args&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;().&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;skip&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;collect&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;();
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; future = stream::iter_ok(args)
        .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for_each&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;dir&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| {
            fs::read_dir(dir)
                .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;flatten_stream&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()
                .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for_each&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;entry&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| {
                    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{:?}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, entry.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;path&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;());
                    future::ok(())
                })
        })
        .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;map_err&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;e&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;eprintln!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Error reading directory: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, e))
        ;
    tokio::run(future)
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;where-s-the-concurrency&quot;&gt;Where&#x27;s the concurrency?&lt;&#x2F;h3&gt;
&lt;p&gt;If you provide this program two different directories with a large
number of files, you may notice that it processes these directories
sequentially: it will print all of the files in the first directory,
and then all of the files in the second directory. Given that async
I&#x2F;O and concurrency usually go hand-in-hand, that may be a bit
surprising.&lt;&#x2F;p&gt;
&lt;p&gt;So far, we&#x27;ve only ever had a single task at a time. Our program
streams out the value of &lt;code&gt;args&lt;&#x2F;code&gt;, and for each one provides a
&lt;code&gt;Future&lt;&#x2F;code&gt;. That &lt;code&gt;Future&lt;&#x2F;code&gt; is run to completion, and then the next value
from &lt;code&gt;args&lt;&#x2F;code&gt; is processed.&lt;&#x2F;p&gt;
&lt;p&gt;What if we want to process each directory concurrently? To do that, we
need to &lt;em&gt;spawn&lt;&#x2F;em&gt; another task, the same way we would spawn a new
thread. Like &lt;code&gt;tokio::run&lt;&#x2F;code&gt;, &lt;code&gt;tokio::spawn&lt;&#x2F;code&gt; takes a &lt;code&gt;Future&lt;&#x2F;code&gt; where both
&lt;code&gt;Item&lt;&#x2F;code&gt; and &lt;code&gt;Error&lt;&#x2F;code&gt; are &lt;code&gt;()&lt;&#x2F;code&gt;. Here&#x27;s a more concurrent version of our
program:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; future = stream::iter_ok(args)
    .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for_each&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;dir&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; future = fs::read_dir(dir)
            .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;flatten_stream&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()
            .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;map_err&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;e&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;eprintln!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Error reading directory: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, e))
            .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for_each&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;entry&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| {
                &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{:?}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, entry.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;path&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;());
                future::ok(())
            })
            ;
        tokio::spawn(future);
        future::ok(())
    })
    ;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Notice how I&#x27;ve put &lt;code&gt;future::ok(())&lt;&#x2F;code&gt; after the
&lt;code&gt;tokio::spawn(future);&lt;&#x2F;code&gt; call. It turns out that&#x27;s not needed: &lt;code&gt;spawn&lt;&#x2F;code&gt;
returns a &lt;code&gt;Spawn&lt;&#x2F;code&gt; value, which behaves like &lt;code&gt;future::ok(())&lt;&#x2F;code&gt; (via its
&lt;code&gt;IntoFuture&lt;&#x2F;code&gt; implementation). So just remove &lt;code&gt;future::ok&lt;&#x2F;code&gt; and the
semicolon after &lt;code&gt;spawn&lt;&#x2F;code&gt;, and your code will still work.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;NOTE&lt;&#x2F;strong&gt; You may not notice the concurrency unless you have a large
number of files in each directory.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;skipping-the-vector&quot;&gt;Skipping the vector&lt;&#x2F;h3&gt;
&lt;p&gt;One final thing that annoyed me above is that &lt;code&gt;Vec&lt;&#x2F;code&gt;. It really seems
like we should be able to get away without it. We can&#x27;t convert &lt;code&gt;Args&lt;&#x2F;code&gt;
into a &lt;code&gt;Stream&lt;&#x2F;code&gt;, because that would require sending the value between
threads. But now we&#x27;ve got a new trick up our sleeves: spawning. What
if we never send the &lt;code&gt;Args&lt;&#x2F;code&gt; anywhere, but just spawn a bunch of tasks.&lt;&#x2F;p&gt;
&lt;p&gt;When I was first learning tokio, I&#x27;ll admit that I spent way more time
trying to figure out the trick I&#x27;m about to show you than I&#x27;m proud
of. We need to create a &lt;code&gt;Future&lt;&#x2F;code&gt;, then run a &lt;code&gt;for&lt;&#x2F;code&gt; loop inside of
it. How do we create a &lt;code&gt;Future&lt;&#x2F;code&gt; that lets us run some code without
waiting for anything else? We can use &lt;code&gt;future::ok(())&lt;&#x2F;code&gt; to create a
dummy &lt;code&gt;Future&lt;&#x2F;code&gt;, and then chain the next action together with
&lt;code&gt;and_then&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; future = future::ok(()).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;and_then&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|()| {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; dir &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;in args&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;().&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;skip&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; future = fs::read_dir(dir)
            .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;flatten_stream&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()
            .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;map_err&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;e&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;eprintln!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Error reading directory: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, e))
            .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for_each&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;entry&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| {
                &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{:?}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, entry.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;path&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;());
                future::ok(())
            })
            ;
        tokio::spawn(future);
    }
    future::ok(())
});
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Another approach, if you&#x27;re so inclined, is to use the &lt;a href=&quot;https:&#x2F;&#x2F;docs.rs&#x2F;tokio&#x2F;0.1.11&#x2F;tokio&#x2F;prelude&#x2F;future&#x2F;fn.poll_fn.html&quot;&gt;&lt;code&gt;future::poll_fn&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; helper function. This takes a 0 argument function which returns a &lt;code&gt;Result&amp;lt;Async&amp;lt;Item&amp;gt;, Error&amp;gt;&lt;&#x2F;code&gt;, just like the &lt;code&gt;poll&lt;&#x2F;code&gt; method of &lt;code&gt;Future&lt;&#x2F;code&gt; does.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;Exercise 10&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Rewrite our program above to use &lt;code&gt;future::poll_fn&lt;&#x2F;code&gt;. Your program
should not use &lt;code&gt;and_then&lt;&#x2F;code&gt; at all.&lt;&#x2F;p&gt;
&lt;p&gt;Solution:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extern crate&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; tokio;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::prelude::*;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::fs;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;std::env::args;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; future = future::poll_fn(|| {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; dir &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;in args&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;().&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;skip&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) {
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; future = fs::read_dir(dir)
                .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;flatten_stream&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()
                .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;map_err&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;e&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;eprintln!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Error reading directory: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, e))
                .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for_each&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;entry&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| {
                    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{:?}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, entry.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;path&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;());
                    future::ok(())
                })
                ;
            tokio::spawn(future);
        }
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Ok&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(Async::Ready(()))
    });
    tokio::run(future)
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Is all of this ceremony worth it for file system operations? Probably
not. Let&#x27;s get into something more interesting: network communications!&lt;&#x2F;p&gt;
&lt;h2 id=&quot;tcp-client&quot;&gt;TCP client&lt;&#x2F;h2&gt;
&lt;p&gt;Rust&#x27;s standard library already provides really nice support for TCP
communications out of the box. For example, the following code will
print the full response headers and body from an HTTP request to
http:&#x2F;&#x2F;httpbin.org&#x2F;json:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;std::io::{Read, Write};
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;std::net::TcpStream;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Result&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;(), &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Box&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;std::error::Error&amp;gt;&amp;gt; {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; stream = TcpStream::connect(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;httpbin.org:80&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
    stream.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;write_all&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;b&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;GET &#x2F;json HTTP&#x2F;1.1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#dc322f;&quot;&gt;\r\n&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Host: httpbin.org&lt;&#x2F;span&gt;&lt;span style=&quot;color:#dc322f;&quot;&gt;\r\n&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Connection: close&lt;&#x2F;span&gt;&lt;span style=&quot;color:#dc322f;&quot;&gt;\r\n\r\n&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;

    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; buffer = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;vec!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;[];
    stream.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;read_to_end&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; buffer)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;

    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, std::str::from_utf8(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;buffer)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Ok&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(())
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;There are some simplifying assumptions here, like using &lt;code&gt;connection: close&lt;&#x2F;code&gt; so that we can use &lt;code&gt;read_to_end&lt;&#x2F;code&gt;, and assuming the response
body is properly UTF-8 encoded. But that&#x27;s not really an indictment of
the TCP support in the standard library.&lt;&#x2F;p&gt;
&lt;p&gt;The real problem is the same one we&#x27;ve been talking about throughout
this lesson: it hogs an entire OS thread blocking on a successful
write to and subsequent read from the network. Let&#x27;s look at how tokio
can help.&lt;&#x2F;p&gt;
&lt;p&gt;It looks like there&#x27;s a &lt;code&gt;TcpStream&lt;&#x2F;code&gt; type in
&lt;a href=&quot;https:&#x2F;&#x2F;docs.rs&#x2F;tokio&#x2F;0.1.12&#x2F;tokio&#x2F;net&#x2F;struct.TcpStream.html&quot;&gt;&lt;code&gt;tokio::net::TcpStream&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;. That
looks like a good place to start. It takes a
&lt;a href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;nightly&#x2F;std&#x2F;net&#x2F;addr&#x2F;enum.SocketAddr.html&quot;&gt;&lt;code&gt;SocketAddr&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;,
which we can probably make easily enough. And it returns a
&lt;a href=&quot;https:&#x2F;&#x2F;docs.rs&#x2F;tokio&#x2F;0.1.12&#x2F;tokio&#x2F;net&#x2F;tcp&#x2F;struct.ConnectFuture.html&quot;&gt;&lt;code&gt;ConnectFuture&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;. Let&#x27;s
start with some code that simple establishes a connection:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extern crate&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; tokio;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::net::TcpStream;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::prelude::*;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;std::net::ToSocketAddrs;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; addr_iter = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;httpbin.org:80&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;to_socket_addrs&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;().&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;();
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; addr = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;match&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; addr_iter.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;next&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;None =&amp;gt; panic!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;DNS resolution failed&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;),
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(addr) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; addr,
    };
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; future = TcpStream::connect(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;addr)
        .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;map_err&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;e&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;eprintln!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Error connecting: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{:?}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, e))
        .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;map&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;stream&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| {
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Got a stream: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{:?}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, stream);
        });
    tokio::run(future)
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The &lt;code&gt;to_socket_addrs&lt;&#x2F;code&gt; business isn&#x27;t our focus right now, so I&#x27;m going
to ignore it. Feel free as an exercise to improve the error handling
of that bit of code.&lt;&#x2F;p&gt;
&lt;p&gt;We&#x27;ve got all of the familiar pieces here: define a &lt;code&gt;Future&lt;&#x2F;code&gt;, handle
errors, and use &lt;code&gt;map&lt;&#x2F;code&gt; to chain together the action to take with the
open connection.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s look a bit more closely at that &lt;code&gt;stream&lt;&#x2F;code&gt; value passed to the
closure. It comes from a &lt;code&gt;ConnectFuture&lt;&#x2F;code&gt;, so we need to look at the
&lt;code&gt;Item&lt;&#x2F;code&gt; associated type there. And sure enough, if you check the docs,
you&#x27;ll see that it&#x27;s &lt;code&gt;TcpStream&lt;&#x2F;code&gt;. Great.&lt;&#x2F;p&gt;
&lt;p&gt;We used &lt;code&gt;write_all&lt;&#x2F;code&gt; in our original, non-async, blocking code. If I
&lt;a href=&quot;https:&#x2F;&#x2F;docs.rs&#x2F;tokio&#x2F;0.1.12&#x2F;tokio&#x2F;prelude&#x2F;trait.AsyncWrite.html?search=write_all&quot;&gt;search for &lt;code&gt;write_all&lt;&#x2F;code&gt; in
tokio&lt;&#x2F;a&gt;,
I find that &lt;a href=&quot;https:&#x2F;&#x2F;docs.rs&#x2F;tokio&#x2F;0.1.12&#x2F;tokio&#x2F;io&#x2F;fn.write_all.html&quot;&gt;there&#x27;s such a helper
function&lt;&#x2F;a&gt;
which returns a
&lt;a href=&quot;https:&#x2F;&#x2F;docs.rs&#x2F;tokio&#x2F;0.1.12&#x2F;tokio&#x2F;io&#x2F;struct.WriteAll.html&quot;&gt;&lt;code&gt;WriteAll&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;
&lt;code&gt;Future&lt;&#x2F;code&gt;. Something interesting:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;write_all&lt;&#x2F;code&gt; takes two parameters: an &lt;code&gt;AsyncWrite&lt;&#x2F;code&gt; and an
&lt;code&gt;AsRef&amp;lt;[u8]&amp;gt;&lt;&#x2F;code&gt;. This will work out to be our &lt;code&gt;TcpStream&lt;&#x2F;code&gt; and the data
to send.&lt;&#x2F;li&gt;
&lt;li&gt;The &lt;code&gt;Item&lt;&#x2F;code&gt; for &lt;code&gt;AsyncWrite&lt;&#x2F;code&gt; is a pair of the variables &lt;code&gt;A&lt;&#x2F;code&gt; and &lt;code&gt;T&lt;&#x2F;code&gt;,
which turns out to be exactly the same as the parameters we passed
in.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;Giving us back the stream we originally provided is vital to
continuing the connection. I didn&#x27;t see any documentation clarifying
the point of returning the byte buffer, but I believe it&#x27;s returned so
that, if desired, you can reuse a mutable buffer.&lt;&#x2F;p&gt;
&lt;p&gt;Anyway, let&#x27;s put this together and make our request:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extern crate&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; tokio;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::net::TcpStream;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::io::write_all;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::prelude::*;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;std::net::ToSocketAddrs;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;REQ_BODY&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u8&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;] = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;b&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;GET &#x2F;json HTTP&#x2F;1.1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#dc322f;&quot;&gt;\r\n&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Host: httpbin.org&lt;&#x2F;span&gt;&lt;span style=&quot;color:#dc322f;&quot;&gt;\r\n&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Connection: close&lt;&#x2F;span&gt;&lt;span style=&quot;color:#dc322f;&quot;&gt;\r\n\r\n&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; addr_iter = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;httpbin.org:80&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;to_socket_addrs&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;().&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;();
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; addr = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;match&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; addr_iter.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;next&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;None =&amp;gt; panic!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;DNS resolution failed&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;),
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(addr) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; addr,
    };
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; future = TcpStream::connect(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;addr)
        .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;and_then&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;stream&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| {
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;write_all&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(stream, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;REQ_BODY&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
                .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;map&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;stream&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;_body&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Write succeeded: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{:?}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, stream))
        })
        .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;map_err&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;e&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;eprintln!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Error occured: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{:?}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, e))
        ;
    tokio::run(future)
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Notice how I replaced the previous &lt;code&gt;map&lt;&#x2F;code&gt; with an &lt;code&gt;and_then&lt;&#x2F;code&gt; call, so
that I could provide another &lt;code&gt;Future&lt;&#x2F;code&gt; to be performed after the
connection was established.&lt;&#x2F;p&gt;
&lt;p&gt;Would it be too much to ask to &lt;em&gt;also&lt;&#x2F;em&gt; get a &lt;code&gt;read_to_end&lt;&#x2F;code&gt; function in
tokio? &lt;a href=&quot;https:&#x2F;&#x2F;docs.rs&#x2F;tokio&#x2F;0.1.12&#x2F;tokio&#x2F;io&#x2F;fn.read_to_end.html&quot;&gt;Nope, not at
all.&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;Exercise 11&lt;&#x2F;strong&gt; Use &lt;code&gt;read_to_end&lt;&#x2F;code&gt; to consume the entire response into
a &lt;code&gt;Vec&amp;lt;u8&amp;gt;&lt;&#x2F;code&gt;, and then print that out, using &lt;code&gt;std::str::from_utf8&lt;&#x2F;code&gt; and
being as careless with error handling as you like.&lt;&#x2F;p&gt;
&lt;p&gt;Alright, solution:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extern crate&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; tokio;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;std::net::ToSocketAddrs;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::io::{read_to_end, write_all};
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::net::TcpStream;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::prelude::*;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;REQ_BODY&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u8&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;] = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;b&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;GET &#x2F;json HTTP&#x2F;1.1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#dc322f;&quot;&gt;\r\n&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Host: httpbin.org&lt;&#x2F;span&gt;&lt;span style=&quot;color:#dc322f;&quot;&gt;\r\n&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Connection: close&lt;&#x2F;span&gt;&lt;span style=&quot;color:#dc322f;&quot;&gt;\r\n\r\n&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; addr_iter = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;httpbin.org:80&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;to_socket_addrs&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;().&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;();
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; addr = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;match&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; addr_iter.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;next&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;None =&amp;gt; panic!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;DNS resolution failed&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;),
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(addr) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; addr,
    };
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; future = TcpStream::connect(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;addr)
        .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;and_then&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;stream&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| {
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;write_all&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(stream, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;REQ_BODY&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
        }).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;and_then&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;stream&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;_body&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)| {
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; buffer = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;vec!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;[];
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;read_to_end&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(stream, buffer)
        }).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;map&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;_stream&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;buffer&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)| {
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; s = std::str::from_utf8(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;buffer).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;();
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, s);
        }).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;map_err&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;e&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;eprintln!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Error occured: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{:?}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, e));
    tokio::run(future)
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;streaming-to-a-file&quot;&gt;Streaming to a file&lt;&#x2F;h2&gt;
&lt;p&gt;I&#x27;ll repeat for maximum annoyance: tokio is not intended for
asynchronous file operations. That said, there &lt;em&gt;is&lt;&#x2F;em&gt; a
&lt;code&gt;tokio::fs::File&lt;&#x2F;code&gt; struct which we can use. Let&#x27;s try to write the
response contents to &lt;code&gt;httpbin.json&lt;&#x2F;code&gt; instead:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; future = TcpStream::connect(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;addr)
    .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;and_then&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;stream&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;write_all&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(stream, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;REQ_BODY&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
    }).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;and_then&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;stream&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;_body&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)| {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; buffer = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;vec!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;[];
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;read_to_end&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(stream, buffer)
    }).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;and_then&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;_stream&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;buffer&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)| {
        File::create(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;httpbin.json&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;and_then&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;file&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| {
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;write_all&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(file, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;buffer).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;map&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|_| ())
        })
    }).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;map_err&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;e&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;eprintln!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Error occured: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{:?}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, e));
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Unfortunately, the compiler doesn&#x27;t like this too much:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;error[E0277]: the trait bound `std::fs::File: tokio::io::AsyncWrite` is not satisfied
  --&amp;gt; src&#x2F;main.rs:25:17
   |
25 |                 write_all(file, &amp;amp;buffer).map(|_| ())
   |                 ^^^^^^^^^ the trait `tokio::io::AsyncWrite` is not implemented for `std::fs::File`
   |
   = note: required by `tokio::io::write_all
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Well, I guess that makes sense: you can&#x27;t asynchronously write to a
&lt;code&gt;File&lt;&#x2F;code&gt;, so &lt;code&gt;tokio::io::write_all&lt;&#x2F;code&gt; isn&#x27;t going to work. Fortunately,
&lt;code&gt;File&lt;&#x2F;code&gt; &lt;em&gt;does&lt;&#x2F;em&gt; implement the &lt;code&gt;Write&lt;&#x2F;code&gt; trait, which provides a blocking
&lt;code&gt;write_all&lt;&#x2F;code&gt;, which is sufficient for our purposes.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;Exercise 12&lt;&#x2F;strong&gt; Rewrite the code above to successfully write
&lt;code&gt;httpbin.json&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;First solution, ignoring anything close to proper error handling:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; future = TcpStream::connect(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;addr)
    .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;and_then&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;stream&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;write_all&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(stream, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;REQ_BODY&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
    }).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;and_then&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;stream&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;_body&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)| {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; buffer = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;vec!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;[];
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;read_to_end&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(stream, buffer)
    }).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;and_then&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;_stream&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;buffer&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)| {
        File::create(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;httpbin.json&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;map&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;file&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| {
            file.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;write_all&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;buffer).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()
        })
    }).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;map_err&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;e&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;eprintln!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Error occured: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{:?}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, e));
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;But ideally, we&#x27;d like to avoid that &lt;code&gt;unwrap()&lt;&#x2F;code&gt; and instead promote an
I&#x2F;O error here to be handle by the &lt;code&gt;map_err&lt;&#x2F;code&gt; below. It turns out that
there&#x27;s a surprisingly trivial change to make that happen:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;File::create(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;httpbin.json&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;and_then&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;file&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| {
    file.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;write_all&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;buffer)
})
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Instead of using &lt;code&gt;map&lt;&#x2F;code&gt;, we use &lt;code&gt;and_then&lt;&#x2F;code&gt;, which requires that we
return some value that implements &lt;code&gt;Future&lt;&#x2F;code&gt;. But fortunately, &lt;code&gt;Result&lt;&#x2F;code&gt;
itself implements &lt;code&gt;Future&lt;&#x2F;code&gt;! The &lt;code&gt;Ok&lt;&#x2F;code&gt; variant becomes the &lt;code&gt;Item&lt;&#x2F;code&gt; for
that &lt;code&gt;Future&lt;&#x2F;code&gt;, and the &lt;code&gt;Err&lt;&#x2F;code&gt; variant becomes its &lt;code&gt;Error&lt;&#x2F;code&gt;. Problem
solved!&lt;&#x2F;p&gt;
&lt;h2 id=&quot;exercise-13&quot;&gt;Exercise 13&lt;&#x2F;h2&gt;
&lt;p&gt;We haven&#x27;t taken advantage of tokio here at all! Let&#x27;s make this
program concurrent. Write a program that takes command line arguments
to determine HTTP requests to make and files to store them to. To
simplify the implementation, we&#x27;ll have it take input that looks like
the following:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;$ cargo run httpbin.org:80 &#x2F;json httpbin.json example.com:80 &#x2F; homepage.html
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Feel free to handle invalid command line arguments however&#x27;s easiest.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;solution&quot;&gt;Solution&lt;&#x2F;h3&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extern crate&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; tokio;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;std::net::ToSocketAddrs;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::io::{read_to_end, write_all};
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::net::TcpStream;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::prelude::*;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;std::fs::File;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;download&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;host&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: String, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;path&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: String, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;filename&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: String) -&amp;gt; impl Future&amp;lt;Item=(), Error=()&amp;gt; {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; addr_iter = host.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;to_socket_addrs&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;().&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;();
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; addr = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;match&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; addr_iter.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;next&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;None =&amp;gt; panic!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;DNS resolution failed&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;),
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(addr) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; addr,
    };
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; req_body = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;format!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;GET &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt; HTTP&#x2F;1.1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#dc322f;&quot;&gt;\r\n&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Host: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;:80&lt;&#x2F;span&gt;&lt;span style=&quot;color:#dc322f;&quot;&gt;\r\n&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Connection: close&lt;&#x2F;span&gt;&lt;span style=&quot;color:#dc322f;&quot;&gt;\r\n\r\n&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;,
        path,
        host,
        );

    TcpStream::connect(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;addr)
        .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;and_then&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;stream&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| {
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;write_all&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(stream, req_body).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;and_then&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;stream&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;_body&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)| {
                &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; buffer = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;vec!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;[];
                &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;read_to_end&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(stream, buffer).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;and_then&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;_stream&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;buffer&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)| {
                    File::create(filename).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;and_then&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;file&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| {
                        file.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;write_all&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;buffer)
                    })
                })
            })
        }).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;map_err&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;e&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;eprintln!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Error occured: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{:?}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, e))
}

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    tokio::run(future::poll_fn(|| {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; args = std::env::args().&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;skip&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;loop &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;match &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(args.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;next&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(), args.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;next&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(), args.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;next&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()) {
                (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(host), &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(path), &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(filename)) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
                    tokio::spawn(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;download&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(host, path, filename));
                }
                &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;_ =&amp;gt; return Ok&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(Async::Ready(())),
            }
        }
    }))
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;nicer-error-handling&quot;&gt;Nicer error handling&lt;&#x2F;h2&gt;
&lt;p&gt;We&#x27;re just &lt;code&gt;panic!&lt;&#x2F;code&gt;ing when we have a bad address. Let&#x27;s do a little
bit better. First, I&#x27;ll define a helper function to return a nice
&lt;code&gt;Result&lt;&#x2F;code&gt;. We&#x27;ll use a &lt;code&gt;String&lt;&#x2F;code&gt; for the &lt;code&gt;Err&lt;&#x2F;code&gt; variant, but we could
should ideally define an &lt;code&gt;enum&lt;&#x2F;code&gt; instead:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;resolve_addr&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;host&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;str&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Result&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;SocketAddr, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;gt; {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; addr_iter = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;match&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; host.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;to_socket_addrs&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Ok&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(addr_iter) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; addr_iter,
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Err&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(e) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt; return Err&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;format!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Invalid host name &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{:?}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{:?}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, host, e)),
    };
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;match&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; addr_iter.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;next&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;None =&amp;gt; Err&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;format!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;No addresses found for host: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{:?}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, host)),
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(addr) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt; Ok&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(addr),
    }
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Inside &lt;code&gt;download&lt;&#x2F;code&gt;, we could continue &lt;code&gt;panic!&lt;&#x2F;code&gt;ing with:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; addr = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;resolve_addr&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;host).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;();
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;But let&#x27;s do better. Using &lt;code&gt;?&lt;&#x2F;code&gt; won&#x27;t work, since we aren&#x27;t returning a
&lt;code&gt;Result&lt;&#x2F;code&gt;. One idea would be to use &lt;code&gt;return&lt;&#x2F;code&gt; to return early:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; addr = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;match resolve_addr&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;host) {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Ok&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(addr) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; addr,
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Err&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(e) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;eprintln!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Error resolving address: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, e);
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;future::err(());
    }
};
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;However, we get an interesting error message from the compiler:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;error[E0308]: mismatched types
  --&amp;gt; src&#x2F;main.rs:34:5
   |
34 | &#x2F;     TcpStream::connect(&amp;amp;addr)
35 | |         .and_then(|stream| {
36 | |             write_all(stream, req_body).and_then(|(stream, _body)| {
37 | |                 let buffer = vec![];
...  |
43 | |             })
44 | |         }).map_err(|e| eprintln!(&amp;quot;Error occured: {:?}&amp;quot;, e))
   | |___________________________________________________________^ expected struct `tokio::prelude::future::FutureResult`, found struct `tokio::prelude::future::MapErr`
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In order to make things work, we need to ensure that we always return
the same type. We&#x27;ve so far used &lt;code&gt;impl Future&lt;&#x2F;code&gt; to say &amp;quot;we&#x27;ll return
some type which is a &lt;code&gt;Future&lt;&#x2F;code&gt;,&amp;quot; but we haven&#x27;t told the compiler what
that type is. Instead, the compiler has inferred that. But now, we
have two different types.&lt;&#x2F;p&gt;
&lt;p&gt;One approach would be dynamic dispatch, such as using
&lt;code&gt;Box&amp;lt;Future&amp;gt;&lt;&#x2F;code&gt;. But there&#x27;s a better way: using the &lt;code&gt;Either&lt;&#x2F;code&gt; helper
type. This type is used in a case where we have two different types of
&lt;code&gt;Future&lt;&#x2F;code&gt;s which both have the same &lt;code&gt;Item&lt;&#x2F;code&gt; and &lt;code&gt;Error&lt;&#x2F;code&gt;. Let&#x27;s see how
we can rewrite our code above to use &lt;code&gt;Either&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;download&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;host&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: String, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;path&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: String, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;filename&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: String) -&amp;gt; impl Future&amp;lt;Item=(), Error=()&amp;gt; {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; addr = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;match resolve_addr&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;host) {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Ok&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(addr) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; addr,
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Err&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(e) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;eprintln!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Error resolving address: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, e);
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;future::Either::A(future::err(()));
        }
    };
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; req_body = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;format!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;GET &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt; HTTP&#x2F;1.1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#dc322f;&quot;&gt;\r\n&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Host: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;:80&lt;&#x2F;span&gt;&lt;span style=&quot;color:#dc322f;&quot;&gt;\r\n&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Connection: close&lt;&#x2F;span&gt;&lt;span style=&quot;color:#dc322f;&quot;&gt;\r\n\r\n&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;,
        path,
        host,
        );

    future::Either::B(TcpStream::connect(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;addr)
        .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;and_then&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;stream&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| {
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;write_all&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(stream, req_body).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;and_then&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;stream&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;_body&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)| {
                &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; buffer = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;vec!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;[];
                &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;read_to_end&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(stream, buffer).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;and_then&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;_stream&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;buffer&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)| {
                    File::create(filename).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;and_then&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;file&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| {
                        file.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;write_all&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;buffer)
                    })
                })
            })
        }).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;map_err&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;e&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;eprintln!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Error occured: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{:?}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, e)))
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;strong&gt;Exercise 14&lt;&#x2F;strong&gt; Implement your own &lt;code&gt;Either&lt;&#x2F;code&gt; data type and use it in
the code above.&lt;&#x2F;p&gt;
&lt;p&gt;Solution:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;enum &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;Either&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;A, B&amp;gt; {
    A(A),
    B(B),
}

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;impl&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;A, B&amp;gt; Future &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;Either&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;A, B&amp;gt;
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;where&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; A: Future&amp;lt;Item=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;B::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;Item, Error=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;B::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;Error&amp;gt;,
          B: Future,
{
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;Item &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= A::Item;
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;Error &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= A::Error;

    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;poll&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) -&amp;gt; Poll&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Self::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;Item, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Self::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;Error&amp;gt; {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;match &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;self &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
            Either::A(a) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; a.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;poll&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(),
            Either::B(b) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; b.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;poll&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(),
        }
    }
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;tcp-server&quot;&gt;TCP server&lt;&#x2F;h2&gt;
&lt;p&gt;Having been so successful with our TCP client, let&#x27;s move over to the
server side. Conceptually, we want to:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;Bind a listening socket&lt;&#x2F;li&gt;
&lt;li&gt;Accept connections from that socket&lt;&#x2F;li&gt;
&lt;li&gt;Copy all data from the input side of the socket to the output side
of the socket&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;Binding a listening socket is going to be a &lt;a href=&quot;https:&#x2F;&#x2F;docs.rs&#x2F;tokio&#x2F;0.1.12&#x2F;tokio&#x2F;net&#x2F;struct.TcpListener.html#method.bind&quot;&gt;blocking
call&lt;&#x2F;a&gt;
to &lt;code&gt;bind&lt;&#x2F;code&gt;, taking our old friend &lt;code&gt;SocketAddr&lt;&#x2F;code&gt;. Since we&#x27;re not playing
around with DNS resolution anymore, we can be a bit lazier about how
we do this:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extern crate&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; tokio;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::prelude::*;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::net::TcpListener;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; addr = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;127.0.0.1:3000&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;parse&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;().&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;expect&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;couldn&amp;#39;t parse address&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; listener = TcpListener::bind(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;addr).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;expect&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;couldn&amp;#39;t bind address&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;It worked! &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{:?}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, listener);
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We now have have a &lt;code&gt;TcpListener&lt;&#x2F;code&gt;. Unlike other types we&#x27;ve seen, this
doesn&#x27;t have an implementation of either &lt;code&gt;Future&lt;&#x2F;code&gt; or
&lt;code&gt;Stream&lt;&#x2F;code&gt;. However, it does have a method called &lt;code&gt;incoming()&lt;&#x2F;code&gt;, which
returns an &lt;code&gt;Incoming&lt;&#x2F;code&gt;, which has a &lt;code&gt;Stream&lt;&#x2F;code&gt; implementation, where
&lt;code&gt;Item&lt;&#x2F;code&gt; is &lt;code&gt;TcpStream&lt;&#x2F;code&gt;. That looks promising!&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; future = listener
    .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;incoming&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()
    .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for_each&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;socket&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Accepted a connection! &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{:?}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, socket);
        future::ok(())
    })
    .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;map_err&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;e&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;eprintln!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;An error occurred: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{:?}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, e))
    ;
tokio::run(future)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And just like that, we&#x27;ve implemented points (1) and (2) above. We&#x27;re
just left with point 3: copying all of the data. Let&#x27;s &lt;a href=&quot;https:&#x2F;&#x2F;docs.rs&#x2F;tokio&#x2F;0.1.12&#x2F;tokio&#x2F;net&#x2F;struct.TcpListener.html?search=copy&quot;&gt;search tokio
for something to do
copying&lt;&#x2F;a&gt;. It
looks like
&lt;a href=&quot;https:&#x2F;&#x2F;docs.rs&#x2F;tokio&#x2F;0.1.12&#x2F;tokio&#x2F;io&#x2F;fn.copy.html&quot;&gt;&lt;code&gt;tokio::io::copy&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;
will do. We need to provide it both a reader and writer. Since we&#x27;re
reader from and writing to the same socket, let&#x27;s just provide the
same value for both:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; future = listener
    .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;incoming&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()
    .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for_each&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;socket&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;copy&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(socket, socket)
            .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;map&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|_| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Connection closed&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))
    })
    .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;map_err&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;e&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;eprintln!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;An error occurred: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{:?}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, e))
    ;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Are you already laughing at my comically silly mistake?&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;error[E0382]: use of moved value: `socket`
  --&amp;gt; src&#x2F;main.rs:13:26
   |
13 |             copy(socket, socket)
   |                  ------  ^^^^^^ value used here after move
   |                  |
   |                  value moved here
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Of course we can&#x27;t use the same value in both positions. Fortunately,
when designing &lt;code&gt;Stream&lt;&#x2F;code&gt;s, the authors provided a method called
&lt;a href=&quot;https:&#x2F;&#x2F;docs.rs&#x2F;tokio&#x2F;0.1.12&#x2F;tokio&#x2F;prelude&#x2F;trait.Stream.html#method.split&quot;&gt;&lt;code&gt;split&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;
to give us a read and write end of the stream. With that in hand, our
echo server becomes trivial:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extern crate&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; tokio;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::prelude::*;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::net::TcpListener;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::io::copy;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; addr = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;127.0.0.1:3000&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;parse&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;().&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;expect&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;couldn&amp;#39;t parse address&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; listener = TcpListener::bind(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;addr).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;expect&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;couldn&amp;#39;t bind address&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; future = listener
        .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;incoming&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()
        .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for_each&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;socket&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| {
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(reader, writer) = socket.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;split&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;();
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;copy&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(reader, writer)
                .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;map&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|_| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Connection closed&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))
        })
        .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;map_err&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;e&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;eprintln!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;An error occurred: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{:?}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, e))
        ;
    tokio::run(future)
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;writing-directly&quot;&gt;Writing directly&lt;&#x2F;h2&gt;
&lt;p&gt;Using &lt;code&gt;copy&lt;&#x2F;code&gt; kind of ignores the gory details of what&#x27;s going on under
the surface. Let&#x27;s start off by writing some arbitrary message to the
writer side of things, using the &lt;a href=&quot;https:&#x2F;&#x2F;docs.rs&#x2F;tokio&#x2F;0.1.12&#x2F;tokio&#x2F;io&#x2F;fn.write_all.html&quot;&gt;&lt;code&gt;write_all&lt;&#x2F;code&gt;
function&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;NOTE&lt;&#x2F;strong&gt; The &lt;code&gt;tokio::io::write_all&lt;&#x2F;code&gt; function takes a &lt;code&gt;AsyncWrite&lt;&#x2F;code&gt; and
returns a &lt;code&gt;WriteAll&lt;&#x2F;code&gt; &lt;code&gt;Future&lt;&#x2F;code&gt;. Don&#x27;t be confused by the presence of a
&lt;code&gt;write_all&lt;&#x2F;code&gt; &lt;em&gt;method&lt;&#x2F;em&gt;, which in fact is a blocking call. I wasted about
5 minutes fumbling with that while writing this tutorial.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extern crate&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; tokio;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::prelude::*;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::net::TcpListener;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::io::{copy, write_all};

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; addr = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;127.0.0.1:3000&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;parse&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;().&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;expect&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;couldn&amp;#39;t parse address&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; listener = TcpListener::bind(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;addr).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;expect&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;couldn&amp;#39;t bind address&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; future = listener
        .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;incoming&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()
        .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for_each&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;socket&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| {
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(reader, writer) = socket.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;split&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;();
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;write_all&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(writer, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;b&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Welcome to the echo server&lt;&#x2F;span&gt;&lt;span style=&quot;color:#dc322f;&quot;&gt;\r\n&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
                .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;map&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|_| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Connection closed&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))
        })
        .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;map_err&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;e&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;eprintln!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;An error occurred: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{:?}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, e))
        ;
    tokio::run(future)
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;strong&gt;Exercise 15&lt;&#x2F;strong&gt; Modify the code above so that, after printing &amp;quot;Welcome
to the echo server&amp;quot;, it proceeds to actually echo content sent in.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;write_all&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(writer, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;b&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Welcome to the echo server&lt;&#x2F;span&gt;&lt;span style=&quot;color:#dc322f;&quot;&gt;\r\n&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
    .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;and_then&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;writer&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, _)| {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;copy&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(reader, writer)
            .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;map&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|_| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Connection closed&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))
    })
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;codecs&quot;&gt;Codecs&lt;&#x2F;h2&gt;
&lt;p&gt;Reading from the &lt;code&gt;reader&lt;&#x2F;code&gt; directly is slightly trickier than writing
to the &lt;code&gt;writer&lt;&#x2F;code&gt;. We &lt;em&gt;could&lt;&#x2F;em&gt; go play around with the underlying polling
reading functions, but we&#x27;re not going to here. (Feel free to &lt;a href=&quot;https:&#x2F;&#x2F;tokio.rs&#x2F;docs&#x2F;getting-started&#x2F;hello-world&#x2F;&quot;&gt;read
the official tokio tutorial for more
information&lt;&#x2F;a&gt;.)&lt;&#x2F;p&gt;
&lt;p&gt;Instead, we&#x27;re going to introduce a new concept, &lt;em&gt;codecs&lt;&#x2F;em&gt;. So far,
we&#x27;ve implicitly been working with the &lt;code&gt;AsyncRead&lt;&#x2F;code&gt; and &lt;code&gt;AsyncWrite&lt;&#x2F;code&gt;
traits, which essentially provide the raw polling functions we&#x27;d need
for building up our own &lt;code&gt;Future&lt;&#x2F;code&gt;s (as we did way long ago at the
beginning of this lesson). However, we often don&#x27;t want to work at
that level of abstraction. Instead, we&#x27;d like to deal with some kind
of framed (or chunked) data.&lt;&#x2F;p&gt;
&lt;p&gt;The new abstraction instead will be a &lt;code&gt;Sink&lt;&#x2F;code&gt;, which is &amp;quot;a value into
which other values can be sent, asynchronously.&amp;quot; We&#x27;ll continue to use
the &lt;code&gt;Stream&lt;&#x2F;code&gt; trait for the read side, which we&#x27;re already quite
familiar with.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s contrive an example. Our echo server currently provides slightly
weird output:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;Hello
Hello
There
There
World
World
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;It&#x27;s hard to tell what I typed in, and what the server
responded. Instead, I&#x27;d like each line sent back from the server to
begin with &amp;quot;You said: &amp;quot;. Doing that with the abstractions we&#x27;ve seen
so far would be fairly painful: we&#x27;d need to grab chunks of data, look
for the newline character, break up the input, splice in the &amp;quot;You
said: &amp;quot; message. I know this is a crash course and all, but I&#x27;d rather
not crash into that.&lt;&#x2F;p&gt;
&lt;p&gt;Instead, let&#x27;s jump straight to the better solution. I want to treat
our TCP stream as a stream of lines of data. If I search for the word
&amp;quot;lines&amp;quot; (and this is &lt;em&gt;actually&lt;&#x2F;em&gt; how I learned about codecs), I end up
with
&lt;a href=&quot;https:&#x2F;&#x2F;docs.rs&#x2F;tokio&#x2F;0.1.12&#x2F;tokio&#x2F;codec&#x2F;struct.LinesCodec.html&quot;&gt;&lt;code&gt;LinesCodec&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;. It
provides a method &lt;code&gt;new()&lt;&#x2F;code&gt;, as well as &lt;code&gt;new_with_max_length&lt;&#x2F;code&gt;. We&#x27;ll use
&lt;code&gt;new&lt;&#x2F;code&gt; here, but I recommend reading the docs to see why that&#x27;s a
terrible idea in any kind of security sensitive context.&lt;&#x2F;p&gt;
&lt;p&gt;The only other method on the type is &lt;code&gt;max_length&lt;&#x2F;code&gt;, which doesn&#x27;t look
like it&#x27;s going to help us actually deal with a TCP socket as a stream
of lines. So let&#x27;s look down at the trait implementations. We&#x27;ve got
all of our usual suspects: &lt;code&gt;Clone&lt;&#x2F;code&gt;, &lt;code&gt;PartialOrd&lt;&#x2F;code&gt;, etc. But two new
ones stick out: &lt;code&gt;Decoder&lt;&#x2F;code&gt; and &lt;code&gt;Encoder&lt;&#x2F;code&gt;. Well &lt;em&gt;that&lt;&#x2F;em&gt; certainly looks
interesting.&lt;&#x2F;p&gt;
&lt;p&gt;Reading through the docs on &lt;code&gt;Decoder&lt;&#x2F;code&gt;, it provides a &lt;a href=&quot;https:&#x2F;&#x2F;docs.rs&#x2F;tokio&#x2F;0.1.12&#x2F;tokio&#x2F;codec&#x2F;trait.Decoder.html#method.framed&quot;&gt;method called
&lt;code&gt;framed&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;,
which has a description that is great. (Please, take a second to
follow that link and read the docs.) Without further ado, let&#x27;s try
adding in our &lt;code&gt;LinesCodec&lt;&#x2F;code&gt; to our echo server:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extern crate&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; tokio;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::prelude::*;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::net::TcpListener;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::codec::{Decoder, LinesCodec};

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; addr = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;127.0.0.1:3000&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;parse&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;().&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;expect&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;couldn&amp;#39;t parse address&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; listener = TcpListener::bind(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;addr).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;expect&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;couldn&amp;#39;t bind address&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; future = listener
        .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;incoming&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()
        .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for_each&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;socket&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| {
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; lines_codec = LinesCodec::new();
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; socket = lines_codec.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;framed&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(socket);
            socket
                .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;send&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::from(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Welcome to the echo server&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))
                .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;map&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|_| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Connection closed&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))
        })
        .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;map_err&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;e&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;eprintln!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;An error occurred: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{:?}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, e))
        ;
    tokio::run(future)
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You may have noticed that we no longer have a newline sequence at the
end of the &amp;quot;Welcome&amp;quot; string. That&#x27;s because our lines codec
automatically handles that. Additionally, we now need to use
&lt;code&gt;String::from&lt;&#x2F;code&gt;, since the &lt;code&gt;Item&lt;&#x2F;code&gt; for this &lt;code&gt;Sink&lt;&#x2F;code&gt; is a &lt;code&gt;String&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;We can also use &lt;code&gt;split&lt;&#x2F;code&gt; to isolate the &lt;code&gt;Sink&lt;&#x2F;code&gt; from the &lt;code&gt;Stream&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(sink, stream) = lines_codec.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;framed&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(socket).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;split&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;();
sink
    .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;send&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::from(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Welcome to the echo server&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))
    .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;map&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|_| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Connection closed&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And, we can use &lt;code&gt;for_each&lt;&#x2F;code&gt; on the &lt;code&gt;Stream&lt;&#x2F;code&gt; side to get a stream of the
lines:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extern crate&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; tokio;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::prelude::*;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::net::TcpListener;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::codec::{Decoder, LinesCodec};

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; addr = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;127.0.0.1:3000&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;parse&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;().&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;expect&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;couldn&amp;#39;t parse address&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; listener = TcpListener::bind(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;addr).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;expect&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;couldn&amp;#39;t bind address&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; future = listener
        .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;incoming&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()
        .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for_each&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;socket&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| {
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; lines_codec = LinesCodec::new();
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(sink, stream) = lines_codec.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;framed&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(socket).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;split&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;();
            sink
                .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;send&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::from(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Welcome to the echo server&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))
                .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;and_then&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;sink&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| {
                    stream
                        .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for_each&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;line&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| {
                            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Received a line: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, line);
                            future::ok(())
                        })
                        .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;map&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|_| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Connection closed&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))
                })
        })
        .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;map_err&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;e&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;eprintln!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;An error occurred: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{:?}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, e))
        ;
    tokio::run(future)
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We&#x27;re almost done here: we just need to &lt;code&gt;send&lt;&#x2F;code&gt; the lines back to the
&lt;code&gt;sink&lt;&#x2F;code&gt; instead of to &lt;code&gt;stdout&lt;&#x2F;code&gt;. Unfortunately, using the &lt;code&gt;send&lt;&#x2F;code&gt; method
we&#x27;ve seen so far is going to be tricky, since we&#x27;ll end up consuming
the &lt;code&gt;sink&lt;&#x2F;code&gt; in each iteration of &lt;code&gt;for_each&lt;&#x2F;code&gt;. We could figure out a way
to make that all work, but instead, let&#x27;s just cut to the chase and
use &lt;code&gt;send_all&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;Exercise 16&lt;&#x2F;strong&gt; Modify the code above so that, instead of printing the
lines to standard output, they get sent back to the client with the
message &amp;quot;You said: &amp;quot;. You&#x27;ll want to look at
&lt;a href=&quot;https:&#x2F;&#x2F;docs.rs&#x2F;tokio&#x2F;0.1.12&#x2F;tokio&#x2F;prelude&#x2F;trait.Sink.html#method.send_all&quot;&gt;&lt;code&gt;send_all&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;Solution&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extern crate&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; tokio;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::prelude::*;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::net::TcpListener;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;tokio::codec::{Decoder, LinesCodec};

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; addr = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;127.0.0.1:3000&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;parse&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;().&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;expect&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;couldn&amp;#39;t parse address&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; listener = TcpListener::bind(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;addr).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;expect&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;couldn&amp;#39;t bind address&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; future = listener
        .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;incoming&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()
        .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for_each&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;socket&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| {
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; lines_codec = LinesCodec::new();
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(sink, stream) = lines_codec.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;framed&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(socket).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;split&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;();
            sink
                .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;send&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::from(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Welcome to the echo server&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))
                .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;and_then&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;sink&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| {
                    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; stream = stream
                        .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;map&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;line&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;format!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;You said: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, line))
                        ;
                    sink.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;send_all&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(stream)
                        .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;map&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|_| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Connection closed&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;))
                })
        })
        .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;map_err&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;e&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;eprintln!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;An error occurred: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{:?}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, e))
        ;
    tokio::run(future)
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;next-time&quot;&gt;Next time&lt;&#x2F;h2&gt;
&lt;p&gt;Whew, that was a big lesson! At this point, you should have a &lt;em&gt;very&lt;&#x2F;em&gt;
solid footing in the ins and outs of tokio. It&#x27;s time to get lots more
experience with using the library, and related higher level libraries
for doing things like HTTP servers and clients.&lt;&#x2F;p&gt;
&lt;p&gt;Depending on reader feedback, the next lesson may either go deeper
into tokio and related libraries, or go back to more fundamental
aspects of Rust like lifetimes. From the tokio side, we&#x27;d play with:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Message passing between tasks&lt;&#x2F;li&gt;
&lt;li&gt;UDP communications&lt;&#x2F;li&gt;
&lt;li&gt;Recursive directory traversal&lt;&#x2F;li&gt;
&lt;li&gt;Parallel downloading of files&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;www.fpcomplete.com&#x2F;rust&quot;&gt;Rust at FP Complete&lt;&#x2F;a&gt; | &lt;a href=&quot;https:&#x2F;&#x2F;www.snoyman.com&#x2F;blog&#x2F;2018&#x2F;10&#x2F;introducing-rust-crash-course&quot;&gt;Introduction&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
</description>
        </item>
        <item>
            <title>Lifetimes and Slices - Rust Crash Course lesson 6 - exercise solutions</title>
            <pubDate>Wed, 28 Nov 2018 00:00:00 +0000</pubDate>
            <link>https://www.snoyman.com/blog/2018/11/rust-crash-course-06-lifetimes-slices-solutions/</link>
            <guid>https://www.snoyman.com/blog/2018/11/rust-crash-course-06-lifetimes-slices-solutions/</guid>
            <description>&lt;p&gt;&lt;strong&gt;Heads up&lt;&#x2F;strong&gt; This blog post series has been updated and published as an eBook by FP Complete. I&#x27;d recommend reading that version instead of these posts. If you&#x27;re interested, please check out the &lt;a href=&quot;https:&#x2F;&#x2F;www.fpcomplete.com&#x2F;rust&#x2F;crash-course&#x2F;&quot;&gt;Rust Crash Course eBook&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Below are the solutions to the exercises from the last Rust Crash
Course lesson, &amp;quot;Lifetimes and slices.&amp;quot;&lt;&#x2F;p&gt;
&lt;p&gt;This post is part of a series based on &lt;a href=&quot;https:&#x2F;&#x2F;www.fpcomplete.com&#x2F;rust&quot;&gt;teaching Rust at FP
Complete&lt;&#x2F;a&gt;. If you&#x27;re reading this post outside
of the blog, you can find links to all posts in the series &lt;a href=&quot;https:&#x2F;&#x2F;www.snoyman.com&#x2F;blog&#x2F;2018&#x2F;10&#x2F;introducing-rust-crash-course&quot;&gt;at the top of the
introduction
post&lt;&#x2F;a&gt;. You
can also &lt;a href=&quot;https:&#x2F;&#x2F;www.snoyman.com&#x2F;feed&#x2F;rust-crash-course&quot;&gt;subscribe to the RSS
feed&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;exercise-1&quot;&gt;Exercise 1&lt;&#x2F;h2&gt;
&lt;p&gt;If you try just throwing in the &lt;code&gt;ref&lt;&#x2F;code&gt; keyword like this:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;match&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; person.age {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;ref&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; age) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Age is &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, age);
        *age += &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
    }
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;None =&amp;gt; println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;No age provided&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;),
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You&#x27;ll get an error message:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;error[E0594]: cannot assign to immutable borrowed content `*age`
  --&amp;gt; src&#x2F;main.rs:16:13
   |
14 |         Some(ref age) =&amp;gt; {
   |              ------- help: use a mutable reference instead: `ref mut age`
15 |             println!(&amp;quot;Age is {}&amp;quot;, age);
16 |             *age += 1;
   |             ^^^^^^^^^ cannot borrow as mutable
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Instead, you need to say &lt;code&gt;ref mut age&lt;&#x2F;code&gt;. And if you&#x27;re like me and
regularly type in &lt;code&gt;mut ref age&lt;&#x2F;code&gt; instead of &lt;code&gt;ref mut age&lt;&#x2F;code&gt;, don&#x27;t worry,
the compiler&#x27;s got your back:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;error: the order of `mut` and `ref` is incorrect
  --&amp;gt; src&#x2F;main.rs:14:14
   |
14 |         Some(mut ref age) =&amp;gt; {
   |              ^^^^^^^ help: try switching the order: `ref mut`

error: aborting due to previous error
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;exercise-2&quot;&gt;Exercise 2&lt;&#x2F;h2&gt;
&lt;p&gt;You need to provide mutable references for the two arguments to
&lt;code&gt;swap&lt;&#x2F;code&gt;. Additionally, in order to get a mutable reference to &lt;code&gt;res&lt;&#x2F;code&gt;,
&lt;code&gt;res&lt;&#x2F;code&gt; itself needs to be &lt;code&gt;mut&lt;&#x2F;code&gt;able.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;next&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Option&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;T&amp;gt; {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; res = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;None&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
    std::mem::swap(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; res, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;.next);
    res
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;exercise-3&quot;&gt;Exercise 3&lt;&#x2F;h2&gt;
&lt;p&gt;We need to have two different parameters, and ensure that &lt;code&gt;ret&lt;&#x2F;code&gt; and
the return value have the same lifetime parameter:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;message_and_return&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&amp;#39;a&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&amp;#39;b&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;msg&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&amp;#39;a&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; String, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;ret&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&amp;#39;b&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; String) -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&amp;#39;b&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; String {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Printing the message: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, msg);
    ret
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;exercise-4&quot;&gt;Exercise 4&lt;&#x2F;h2&gt;
&lt;p&gt;Since the data is stored in the program executable itself, it lives
for the entire program execution. Therefore, the lifetime is
&lt;code&gt;&#x27;static&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; bytearray1: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&amp;#39;static &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u8&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;22&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;] = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;b&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Hello World in binary!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; bytearray2: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&amp;#39;static &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u8&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;] = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;b&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Hello World in binary!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{:?}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, bytearray1);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{:?}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, bytearray2);
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;exercise-5&quot;&gt;Exercise 5&lt;&#x2F;h2&gt;
&lt;p&gt;This is a great use case for the iterator method &lt;code&gt;count&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; arg &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;std::env::args() {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;arg: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;, characters: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;, bytes: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;,
            arg,
            arg.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;chars&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;().&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;count&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(),
            arg.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;bytes&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;().&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;count&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(),
            );
    }
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;www.fpcomplete.com&#x2F;rust&quot;&gt;Rust at FP Complete&lt;&#x2F;a&gt; | &lt;a href=&quot;https:&#x2F;&#x2F;www.snoyman.com&#x2F;blog&#x2F;2018&#x2F;10&#x2F;introducing-rust-crash-course&quot;&gt;Introduction&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
</description>
        </item>
        <item>
            <title>Lifetimes and Slices - Rust Crash Course lesson 6</title>
            <pubDate>Mon, 26 Nov 2018 00:00:00 +0000</pubDate>
            <link>https://www.snoyman.com/blog/2018/11/rust-crash-course-06-lifetimes-slices/</link>
            <guid>https://www.snoyman.com/blog/2018/11/rust-crash-course-06-lifetimes-slices/</guid>
            <description>&lt;p&gt;&lt;strong&gt;Heads up&lt;&#x2F;strong&gt; This blog post series has been updated and published as an eBook by FP Complete. I&#x27;d recommend reading that version instead of these posts. If you&#x27;re interested, please check out the &lt;a href=&quot;https:&#x2F;&#x2F;www.fpcomplete.com&#x2F;rust&#x2F;crash-course&#x2F;&quot;&gt;Rust Crash Course eBook&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;We&#x27;ve glossed over some details of lifetimes and sequences of values so far.
It&#x27;s time to dive in and learn about lifetimes and &lt;em&gt;slices&lt;&#x2F;em&gt; correctly.&lt;&#x2F;p&gt;
&lt;p&gt;This post is part of a series based on &lt;a href=&quot;https:&#x2F;&#x2F;www.fpcomplete.com&#x2F;rust&quot;&gt;teaching Rust at FP
Complete&lt;&#x2F;a&gt;. If you&#x27;re reading this post outside
of the blog, you can find links to all posts in the series &lt;a href=&quot;https:&#x2F;&#x2F;www.snoyman.com&#x2F;blog&#x2F;2018&#x2F;10&#x2F;introducing-rust-crash-course&quot;&gt;at the top of the
introduction
post&lt;&#x2F;a&gt;. You
can also &lt;a href=&quot;https:&#x2F;&#x2F;www.snoyman.com&#x2F;feed&#x2F;rust-crash-course&quot;&gt;subscribe to the RSS
feed&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;printing-a-person&quot;&gt;Printing a person&lt;&#x2F;h2&gt;
&lt;p&gt;Let&#x27;s look at some fairly unsurprising code:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;#[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;derive&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(Debug)]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;struct &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;Person &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Option&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;gt;,
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;age&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Option&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;gt;,
}

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;print_person&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;person&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: Person) {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;match&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; person.name {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(name) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt; println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Name is &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, name),
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;None =&amp;gt; println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;No name provided&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;),
    }

    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;match&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; person.age {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(age) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt; println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Age is &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, age),
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;None =&amp;gt; println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;No age provided&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;),
    }
}

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;print_person&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(Person {
        name: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::from(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Alice&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)),
        age: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;30&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;),
    });
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Fairly simple, and a nice demonstration of pattern matching. However,
let&#x27;s throw in one extra line. Try adding this at the beginning of the
&lt;code&gt;print_person&lt;&#x2F;code&gt; function:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Full Person value: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{:?}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, person);
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;All good. We&#x27;re printing the full contents of the &lt;code&gt;Person&lt;&#x2F;code&gt; and then
pattern matching. But try adding that line to the &lt;em&gt;end&lt;&#x2F;em&gt; of the
function, and you&#x27;ll get a compilation error:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;error[E0382]: use of partially moved value: `person`
  --&amp;gt; main.rs:18:41
   |
9  |         Some(name) =&amp;gt; println!(&amp;quot;Name is {}&amp;quot;, name),
   |              ---- value moved here
...
18 |     println!(&amp;quot;Full Person value: {:?}&amp;quot;, person);
   |                                         ^^^^^^ value used here after move
   |
   = note: move occurs because the value has type `std::string::String`, which does not implement the `Copy` trait

error: aborting due to previous error
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;strong&gt;NOTE&lt;&#x2F;strong&gt; This is an error with the Rust compiler I&#x27;m using,
1.30.1. However, there are plans in place to improve this situation.&lt;&#x2F;p&gt;
&lt;p&gt;The problem is that we&#x27;ve consumed a part of the &lt;code&gt;person&lt;&#x2F;code&gt; value, and
therefore cannot display it. We can fix that by setting it
again. Let&#x27;s make the &lt;code&gt;person&lt;&#x2F;code&gt; argument &lt;code&gt;mut&lt;&#x2F;code&gt;able, and then fill
in the moved &lt;code&gt;person.name&lt;&#x2F;code&gt; with a default &lt;code&gt;None&lt;&#x2F;code&gt; value:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;print_person&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;person&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: Person) {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;match&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; person.name {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(name) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt; println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Name is &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, name),
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;None =&amp;gt; println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;No name provided&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;),
    }

    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;match&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; person.age {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(age) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt; println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Age is &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, age),
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;None =&amp;gt; println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;No age provided&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;),
    }

    person.name = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;None&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;

    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Full Person value: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{:?}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, person);
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;That compiles, but now the output is confusingly:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;Name is Alice
Age is 30
Full Person value: Person { name: None, age: Some(30) }
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Notice how the &lt;code&gt;name&lt;&#x2F;code&gt; in the last line in &lt;code&gt;None&lt;&#x2F;code&gt;, when ideally it should be
&lt;code&gt;Some(Alice)&lt;&#x2F;code&gt;. We can do better, by returning the name from the &lt;code&gt;match&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;person.name = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;match&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; person.name {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(name) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Name is &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, name);
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(name)
    },
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;None =&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;No name provided&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;None
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
};
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;But that&#x27;s decidely inelegant. Let&#x27;s take a step back. Do we actually
need to consume&#x2F;move the &lt;code&gt;person.name&lt;&#x2F;code&gt; at all? Not really. It should
work to do everything by reference. So let&#x27;s go back and avoid the
move entirely, by using a borrow:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;match &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;person.name {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(name) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt; println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Name is &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, name),
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;None =&amp;gt; println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;No name provided&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;),
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Much better! We don&#x27;t need to put the borrow on &lt;code&gt;person.age&lt;&#x2F;code&gt; though,
since the &lt;code&gt;u32&lt;&#x2F;code&gt; is &lt;code&gt;Copy&lt;&#x2F;code&gt;able. Here, we&#x27;re pattern matching on a
reference, and therefore the &lt;code&gt;name&lt;&#x2F;code&gt; is &lt;em&gt;also&lt;&#x2F;em&gt; a reference.&lt;&#x2F;p&gt;
&lt;p&gt;However, we can be more explicit about that with the &lt;code&gt;ref&lt;&#x2F;code&gt;
keyword. This keyword says that, when pattern matching, we want the
pattern to be a reference, &lt;em&gt;not&lt;&#x2F;em&gt; a move of the original value. (&lt;a href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;book&#x2F;second-edition&#x2F;ch18-03-pattern-syntax.html#creating-references-in-patterns-with-ref-and-ref-mut&quot;&gt;More
info in the Rust
book.&lt;&#x2F;a&gt;)
We end up with:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;match&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; person.name {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;ref&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; name) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt; println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Name is &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, name),
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;None =&amp;gt; println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;No name provided&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;),
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In our case, this is the same basic result as &lt;code&gt;&amp;amp;person.name&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;birthday&quot;&gt;Birthday!&lt;&#x2F;h2&gt;
&lt;p&gt;Let&#x27;s modify our code so that, when printing the age, we also increase
the age by 1. First stab is below. Note that the code won&#x27;t compile,
try to predict why:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;match&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; person.age {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(age) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Age is &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, age);
        age += &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
    }
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;None =&amp;gt; println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;No age provided&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;),
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We&#x27;re trying to mutate the local &lt;code&gt;age&lt;&#x2F;code&gt; binding, but it&#x27;s
immutable. Well, that&#x27;s easy enough to fix, just replace &lt;code&gt;Some(age)&lt;&#x2F;code&gt;
with &lt;code&gt;Some(mut age)&lt;&#x2F;code&gt;. That compiles, but with a warning:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;warning: value assigned to `age` is never read
  --&amp;gt; src&#x2F;main.rs:16:13
   |
16 |             age += 1;
   |             ^^^
   |
   = note: #[warn(unused_assignments)] on by default
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And then the output is:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;Name is Alice
Age is 30
Full Person value: Person { name: Some(&amp;quot;Alice&amp;quot;), age: Some(30) }
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Notice how on the last line, the age is still 30, not 31. Take a
minute and try to understand what&#x27;s happening here... Done? Cool.&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;We pattern match on &lt;code&gt;person.age&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;If it&#x27;s &lt;code&gt;Some&lt;&#x2F;code&gt;, we need to move the age into the local &lt;code&gt;age&lt;&#x2F;code&gt;
binding&lt;&#x2F;li&gt;
&lt;li&gt;But since the type is &lt;code&gt;u32&lt;&#x2F;code&gt;, it will make a copy and move the copy&lt;&#x2F;li&gt;
&lt;li&gt;When we increment the age, we&#x27;re incrementing a copy, which is never used.&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;We can try solving this by taking a mutable reference to &lt;code&gt;person.age&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;print_person&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;person&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: Person) {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;match&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; person.name {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;ref&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; name) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt; println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Name is &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, name),
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;None =&amp;gt; println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;No name provided&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;),
    }

    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;match &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; person.age {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(age) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Age is &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, age);
            age += &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
        }
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;None =&amp;gt; println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;No age provided&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;),
    }

    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Full Person value: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{:?}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, person);
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The compiler complains: &lt;code&gt;age&lt;&#x2F;code&gt; is a &lt;code&gt;&amp;amp;mut u32&lt;&#x2F;code&gt;, but we&#x27;re trying to use
&lt;code&gt;+=&lt;&#x2F;code&gt; on it:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;error[E0368]: binary assignment operation `+=` cannot be applied to type `&amp;amp;mut u32`
  --&amp;gt; src&#x2F;main.rs:16:13
   |
16 |             age += 1;
   |             ---^^^^^
   |             |
   |             cannot use `+=` on type `&amp;amp;mut u32`
   |
   = help: `+=` can be used on &amp;#39;u32&amp;#39;, you can dereference `age`: `*age`
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The compiler taketh, and the compiler giveth as well: we just need to
dereference the &lt;code&gt;age&lt;&#x2F;code&gt; reference. Close, but one more error:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;error[E0596]: cannot borrow field `person.age` of immutable binding as mutable
  --&amp;gt; src&#x2F;main.rs:13:16
   |
7  | fn print_person(person: Person) {
   |                 ------ consider changing this to `mut person`
...
13 |     match &amp;amp;mut person.age {
   |                ^^^^^^^^^^ cannot mutably borrow field of immutable binding

error: aborting due to previous error
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Again, the compiler tells us exactly how to solve the problem: make
&lt;code&gt;person&lt;&#x2F;code&gt; &lt;code&gt;mut&lt;&#x2F;code&gt;able. Go ahead and make that change, and everything
should work.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;Exercise 1&lt;&#x2F;strong&gt; In the case of &lt;code&gt;person.name&lt;&#x2F;code&gt;, we came up with two
solutions: borrow the &lt;code&gt;person.name&lt;&#x2F;code&gt;, or use the &lt;code&gt;ref&lt;&#x2F;code&gt; keyword. The
same two styles of solutions will work for our current problem. We&#x27;ve
just demonstrated the borrow approach. Try to solve this instead using
the &lt;code&gt;ref&lt;&#x2F;code&gt; keyword.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;the-single-iterator&quot;&gt;The single iterator&lt;&#x2F;h2&gt;
&lt;p&gt;Let&#x27;s make a silly little iterator which produces a single
value. We&#x27;ll track whether or not the value has been produced by using
an &lt;code&gt;Option&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;struct &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;Single&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;T&amp;gt; {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;next&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Option&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;T&amp;gt;,
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Let&#x27;s make a helper function to create &lt;code&gt;Single&lt;&#x2F;code&gt; values:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;single&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;T&amp;gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;t&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: T) -&amp;gt; Single&amp;lt;T&amp;gt; {
    Single {
        next: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(t),
    }
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And let&#x27;s write a &lt;code&gt;main&lt;&#x2F;code&gt; that tests that this works as expected:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; actual: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Vec&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;gt; = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;single&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;42&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;collect&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;();
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;assert_eq!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;vec!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;42&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;], actual);
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If you try to compile that, you&#x27;ll get an error message:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;error[E0599]: no method named `collect` found for type `Single&amp;lt;{integer}&amp;gt;` in the current scope
  --&amp;gt; src&#x2F;main.rs:12:39
   |
1  | struct Single&amp;lt;T&amp;gt; {
   | ---------------- method `collect` not found for this
...
12 |     let actual: Vec&amp;lt;u32&amp;gt; = single(42).collect();
   |                                       ^^^^^^^
   |
   = note: the method `collect` exists but the following trait bounds were not satisfied:
           `&amp;amp;mut Single&amp;lt;{integer}&amp;gt; : std::iter::Iterator`
   = help: items from traits can only be used if the trait is implemented and in scope
   = note: the following trait defines an item `collect`, perhaps you need to implement it:
           candidate #1: `std::iter::Iterator`
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We need to provide an &lt;code&gt;Iterator&lt;&#x2F;code&gt; implementation in order to use
&lt;code&gt;collect()&lt;&#x2F;code&gt;. The &lt;code&gt;Item&lt;&#x2F;code&gt; is going to be &lt;code&gt;T&lt;&#x2F;code&gt;. And we&#x27;ve already got a
great &lt;code&gt;Option&amp;lt;T&amp;gt;&lt;&#x2F;code&gt; available for the return value from the &lt;code&gt;next&lt;&#x2F;code&gt;
function:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;impl&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;T&amp;gt; Iterator &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;Single&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;T&amp;gt; {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;Item &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= T;

    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;next&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Option&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;T&amp;gt; {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;.next
    }
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Unfortunately this doesn&#x27;t work:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;error[E0507]: cannot move out of borrowed content
  --&amp;gt; src&#x2F;main.rs:21:9
   |
21 |         self.next
   |         ^^^^ cannot move out of borrowed content

error: aborting due to previous error
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Oh, right. We can&#x27;t move the result value out, since our &lt;code&gt;next&lt;&#x2F;code&gt;
function only mutable borrows &lt;code&gt;self&lt;&#x2F;code&gt;. Let&#x27;s try some pattern matching:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;match &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;.next {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(next) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt; Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(next),
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;None =&amp;gt; None&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;,
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Except this &lt;em&gt;also&lt;&#x2F;em&gt; involves moving out of a borrow, so it fails. Let&#x27;s
try one more time: we&#x27;ll move into a local variable, set &lt;code&gt;self.next&lt;&#x2F;code&gt;
to &lt;code&gt;None&lt;&#x2F;code&gt; (so it doesn&#x27;t repeat the value again), and return the local
variable:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;next&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Option&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;T&amp;gt; {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; res = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;.next;
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;.next = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;None&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
    res
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Nope, the compiler is &lt;em&gt;still&lt;&#x2F;em&gt; not happy! I guess we&#x27;ll just have to
give up on our grand vision of a &lt;code&gt;Single&lt;&#x2F;code&gt; iterator. We could of course
just cheat:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;next&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Option&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;T&amp;gt; {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;None
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;But while that compiles, it fails our test at runtime:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;thread &amp;#39;main&amp;#39; panicked at &amp;#39;assertion failed: `(left == right)`
  left: `[42]`,
 right: `[]`&amp;#39;, src&#x2F;main.rs:13:5
note: Run with `RUST_BACKTRACE=1` for a backtrace.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;swap&quot;&gt;Swap&lt;&#x2F;h2&gt;
&lt;p&gt;What we did above was attempt to swap the &lt;code&gt;self.next&lt;&#x2F;code&gt; with a local
variable. However, the borrow checker wasn&#x27;t a fan of the approach we
took. However, there&#x27;s a helper function in the standard library,
&lt;code&gt;std::mem::swap&lt;&#x2F;code&gt;, which may be able to help us. It looks like:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;pub &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;swap&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;T&amp;gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;x&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; T, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;y&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; T)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And sure enough, we can use it to solve our problem:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;next&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Option&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;T&amp;gt; {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; res = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;None&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
    std::mem::swap(res, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;.next);
    res
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;strong&gt;Exercise 2&lt;&#x2F;strong&gt; The code above doesn&#x27;t quite compile, though the
compiler can guide you to a correct solution. Try to identify the
problems above and fix them yourself. Failing that, ask the compiler
to help you out.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;replace-and-take&quot;&gt;replace and take&lt;&#x2F;h2&gt;
&lt;p&gt;Did you find that whole create-a-temp-variable thing a bit verbose?
Yeah, it does to the authors of the Rust standard library too. There&#x27;s
a helper function that bypasses that temporary variable:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;next&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Option&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;T&amp;gt; {
    std::mem::replace(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;.next, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;None&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Much nicer! However, that &lt;strong&gt;still&lt;&#x2F;strong&gt; seems like more work for something
that should be really easy. And fortunately, yet again, it does to the
authors of the Rust standard library too. This pattern of replacing
the value in an &lt;code&gt;Option&lt;&#x2F;code&gt; with &lt;code&gt;None&lt;&#x2F;code&gt; and then working with the
original value is common enough that they&#x27;ve given it a name and a
method: &lt;code&gt;take&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;next&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Option&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;T&amp;gt; {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;.next.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;take&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And we&#x27;re done!&lt;&#x2F;p&gt;
&lt;h2 id=&quot;lifetimes&quot;&gt;Lifetimes&lt;&#x2F;h2&gt;
&lt;p&gt;We&#x27;ve briefly mentioned lifetimes in previous lessons, but it&#x27;s time
to get a bit more serious about them. Let&#x27;s look at a simple usage of
references:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;struct &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;Person &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: String,
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;age&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;,
}

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;get_name&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;person&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;Person) -&amp;gt; String {
    person.name
}

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; alice = Person {
        name: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::from(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Alice&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;),
        age: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;30&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;,
    };
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; name = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;get_name&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;alice);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Name: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, name);
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This code doesn&#x27;t compile. Our &lt;code&gt;get_name&lt;&#x2F;code&gt; function takes a reference
to a &lt;code&gt;Person&lt;&#x2F;code&gt;, and then tries to move that person&#x27;s &lt;code&gt;name&lt;&#x2F;code&gt; in its
result. This isn&#x27;t possible. One solution would be to clone the name:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;get_name&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;person&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;Person) -&amp;gt; String {
    person.name.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;clone&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;While this works, it&#x27;s relatively inefficient. We like to avoid making
copies when we can. Instead, let&#x27;s simply return a reference to the
name:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;get_name&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;person&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;Person) -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;String {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;person.name
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Hurrah! But let&#x27;s make our function a little bit more complicated. We
now want a function that will take &lt;em&gt;two&lt;&#x2F;em&gt; &lt;code&gt;Person&lt;&#x2F;code&gt;s, and return the
name of the older one. That sounds fairly easy to write:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;struct &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;Person &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: String,
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;age&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;,
}

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;get_older_name&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;person1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;Person, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;person2&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;Person) -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;String {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; person1.age &amp;gt;= person2.age {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;person1.name
    } &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;else &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;person2.name
    }
}

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; alice = Person {
        name: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::from(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Alice&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;),
        age: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;30&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;,
    };
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; bob = Person {
        name: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::from(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Bob&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;),
        age: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;35&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;,
    };
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; name = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;get_older_name&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;alice, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;bob);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Older person: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, name);
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Unfortunately, the compiler is quite cross with us:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;error[E0106]: missing lifetime specifier
 --&amp;gt; src&#x2F;main.rs:6:58
  |
6 | fn get_older_name(person1: &amp;amp;Person, person2: &amp;amp;Person) -&amp;gt; &amp;amp;String {
  |                                                          ^ expected lifetime parameter
  |
  = help: this function&amp;#39;s return type contains a borrowed value, but the signature does not say whether it is borrowed from `person1` or `person2`
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;That error message is remarkably clear. Our function is returning a
borrowed value. That value must be borrowed from &lt;em&gt;somewhere&lt;&#x2F;em&gt;. The only
two options* are &lt;code&gt;person1&lt;&#x2F;code&gt; and &lt;code&gt;person2&lt;&#x2F;code&gt;. And it seems that Rust
needs to know this for some reason.&lt;&#x2F;p&gt;
&lt;p&gt;* This is a small fib, see &amp;quot;static lifetime&amp;quot; below.&lt;&#x2F;p&gt;
&lt;p&gt;Remember how we have some rules about references? References cannot
outlive the original values they come from. We need to track how long
the result value is allowed to live, which must be less than or equal
to the time the value it came from lives. This whole concept is
&lt;em&gt;lifetimes&lt;&#x2F;em&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;For reasons we&#x27;ll get to in a bit (under &amp;quot;lifetime elision&amp;quot;), we can
often bypass the need to explicitly talk about lifetimes. However,
sometimes we do need to be explicit. To do this, we introduce some new
parameters. But this time, they are &lt;em&gt;lifetime parameters&lt;&#x2F;em&gt;, which begin
with a single quote and are lower case. Usually, they are just a
single letter. For example:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;get_older_name&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&amp;#39;a&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&amp;#39;b&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;person1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&amp;#39;a&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; Person, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;person2&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&amp;#39;b&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; Person) -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;String
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We still get an error from the compiler because our return value
doesn&#x27;t have a lifetime. Should we choose &lt;code&gt;&#x27;a&lt;&#x2F;code&gt; or &lt;code&gt;&#x27;b&lt;&#x2F;code&gt;? Or maybe we
should create a new &lt;code&gt;&#x27;c&lt;&#x2F;code&gt; and try that? Let&#x27;s start off with &lt;code&gt;&#x27;a&lt;&#x2F;code&gt;. We
get the error message:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;error[E0623]: lifetime mismatch
  --&amp;gt; src&#x2F;main.rs:10:9
   |
6  | fn get_older_name&amp;lt;&amp;#39;a, &amp;#39;b&amp;gt;(person1: &amp;amp;&amp;#39;a Person, person2: &amp;amp;&amp;#39;b Person) -&amp;gt; &amp;amp;&amp;#39;a String {
   |                                                         ----------     ----------
   |                                                         |
   |                                                         this parameter and the return type are declared with different lifetimes...
...
10 |         &amp;amp;person2.name
   |         ^^^^^^^^^^^^^ ...but data from `person2` is returned here
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;That makes sense: since our result may come from &lt;code&gt;person2&lt;&#x2F;code&gt;, we have no
guarantee that the &lt;code&gt;&#x27;a&lt;&#x2F;code&gt; lifetime parameter is less than or equal to
the &lt;code&gt;&#x27;b&lt;&#x2F;code&gt; lifetime parameter. Fortunately, we can explicitly state
that, in the same way that we state that types implement some traits:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;get_older_name&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&amp;#39;a&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&amp;#39;b&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&amp;#39;a&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;person1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&amp;#39;a&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; Person, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;person2&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&amp;#39;b&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; Person) -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&amp;#39;a&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; String {
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And this actually compiles! Alternatively, in this case, we can just
completely bypass the second lifetime parameter, and say that
&lt;code&gt;person1&lt;&#x2F;code&gt; and &lt;code&gt;person2&lt;&#x2F;code&gt; must have the same lifetime, which must be the
same as the return value:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;get_older_name&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&amp;#39;a&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;person1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&amp;#39;a&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; Person, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;person2&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&amp;#39;a&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; Person) -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&amp;#39;a&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; String {
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If you&#x27;re like me, you may think that this would be overly
limiting. For example, I initially thought that with the signature
above, this code wouldn&#x27;t compile:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; alice = Person {
        name: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::from(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Alice&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;),
        age: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;30&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;,
    };
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;foo&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;alice);
}

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;foo&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;alice&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;Person) {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; bob = Person {
        name: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::from(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Bob&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;),
        age: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;35&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;,
    };
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; name = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;get_older_name&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;alice, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;bob);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Older person: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, name);
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;After all, the lifetime for &lt;code&gt;alice&lt;&#x2F;code&gt; is demonstrably bigger than the
lifetime for &lt;code&gt;bob&lt;&#x2F;code&gt;. However, the semantics for lifetimes in functions
signatures is that all of the values have at least the same
lifetime. If they happen to live a bit longer, no harm, no foul.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;requirement-for-multiple-lifetime-parameters&quot;&gt;Requirement for multiple lifetime parameters&lt;&#x2F;h2&gt;
&lt;p&gt;So can we cook up an example where multiple lifetime parameters are absolutely necessary? Sure!&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;message_and_return&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;msg&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;String, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;ret&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;String) -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;String {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Printing the message: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, msg);
    ret
}

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; name = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::from(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Alice&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; msg = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::from(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;This is the message&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; ret = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;message_and_return&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;msg, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;name);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Return value: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, ret);
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This code won&#x27;t compile, because we need some lifetime parameters. So
let&#x27;s use our trick from above, and use the same parameter:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;message_and_return&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&amp;#39;a&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;msg&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&amp;#39;a&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; String, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;ret&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&amp;#39;a&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; String) -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&amp;#39;a&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; String {
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;That compiles, but let&#x27;s make our calling code a bit more complicated:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; name = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::from(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Alice&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; ret = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;foo&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;name);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Return value: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, ret);
}

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;foo&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;String) -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;String {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; msg = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::from(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;This is the message&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;message_and_return&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;msg, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;name)
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now the compiler isn&#x27;t happy:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;error[E0597]: `msg` does not live long enough
  --&amp;gt; src&#x2F;main.rs:14:25
   |
14 |     message_and_return(&amp;amp;msg, &amp;amp;name)
   |                         ^^^ borrowed value does not live long enough
15 | }
   | - borrowed value only lives until here
   |
note: borrowed value must be valid for the anonymous lifetime #1 defined on the function body at 12:1...
  --&amp;gt; src&#x2F;main.rs:12:1
   |
12 | &#x2F; fn foo(name: &amp;amp;String) -&amp;gt; &amp;amp;String {
13 | |     let msg = String::from(&amp;quot;This is the message&amp;quot;);
14 | |     message_and_return(&amp;amp;msg, &amp;amp;name)
15 | | }
   | |_^
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We&#x27;ve stated that the return value must live the same amount of time
as the &lt;code&gt;msg&lt;&#x2F;code&gt; parameter. But we return the return value &lt;em&gt;outside&lt;&#x2F;em&gt; of
the &lt;code&gt;foo&lt;&#x2F;code&gt; function, while the &lt;code&gt;msg&lt;&#x2F;code&gt; value will not live beyond the end
of &lt;code&gt;foo&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;The calling code should be fine, we just need to tell Rust that it&#x27;s
OK if the &lt;code&gt;msg&lt;&#x2F;code&gt; parameter has a shorter lifetime than the return
value.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;Exercise 3&lt;&#x2F;strong&gt; Modify the signature of &lt;code&gt;message_and_return&lt;&#x2F;code&gt; so that
the code compiles and runs.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;lifetime-elision&quot;&gt;Lifetime elision&lt;&#x2F;h2&gt;
&lt;p&gt;Why do we sometimes get away with skipping the lifetimes, and
sometimes we need to include them? There are rules in the language
called &amp;quot;lifetime elision.&amp;quot; Instead of trying to cover this myself,
I&#x27;ll refer to the Nomicon:&lt;&#x2F;p&gt;
&lt;p&gt;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;nomicon&#x2F;lifetime-elision.html&lt;&#x2F;p&gt;
&lt;h2 id=&quot;static-lifetime&quot;&gt;Static lifetime&lt;&#x2F;h2&gt;
&lt;p&gt;Above, I implied that if you return a reference, then it must have the
same lifetime as one of its input parameters. This mostly makes sense,
because otherwise we&#x27;d have to conjure some arbitrary lifetime out of
thin air. However, it&#x27;s also a lie. There&#x27;s one special lifetime that
survives the entire program, called &lt;code&gt;&#x27;static&lt;&#x2F;code&gt;. And here&#x27;s some fun
news: you&#x27;ve implicitly used it since the first Hello World we wrote
together!&lt;&#x2F;p&gt;
&lt;p&gt;Every single string literal is in fact a reference with the lifetime
of &lt;code&gt;&#x27;static&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&amp;#39;static &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;str &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Alice&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;());
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;arrays-slices-vectors-and-string&quot;&gt;Arrays, slices, vectors, and String&lt;&#x2F;h2&gt;
&lt;p&gt;Here&#x27;s another place where we&#x27;ve been cheeky. What&#x27;s the difference
between &lt;code&gt;String&lt;&#x2F;code&gt; and &lt;code&gt;str&lt;&#x2F;code&gt;? Both of these have popped up quite a
bit. We&#x27;ll get to those in a little bit. First, we need to talk about
arrays, slices, and vectors.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;arrays&quot;&gt;Arrays&lt;&#x2F;h3&gt;
&lt;p&gt;To my knowledge, the best official documentation on arrays is in &lt;a href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;std&#x2F;primitive.array.html&quot;&gt;the
API docs
themselves&lt;&#x2F;a&gt;. Arrays
are contiguous blocks of memory containing a single type of data with
a fixed length. The type is represented as &lt;code&gt;[T; N]&lt;&#x2F;code&gt;, where &lt;code&gt;T&lt;&#x2F;code&gt; is the
type of value, and &lt;code&gt;N&lt;&#x2F;code&gt; is the length of the array. And like any sane
programming language, arrays are 0-indexed in Rust.&lt;&#x2F;p&gt;
&lt;p&gt;There are two syntaxes for initiating arrays. List literal syntax
(like Javascript, Python, or Haskell):&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; nums: [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;] = [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;];
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{:?}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, nums);
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And a repeat expression:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; nums: [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;] = [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;42&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;];
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{:?}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, nums);
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You can make arrays mutable and then, well, mutate them:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; nums: [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;] = [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;42&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;];
    nums[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;] += &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{:?}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, nums);
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;That&#x27;s very nice, but what if you need something more dynamic? For
that, we have...&lt;&#x2F;p&gt;
&lt;h3 id=&quot;vec&quot;&gt;Vec&lt;&#x2F;h3&gt;
&lt;p&gt;A &lt;code&gt;Vec&lt;&#x2F;code&gt; is a &amp;quot;contiguous, growable array type.&amp;quot; You can &lt;code&gt;push&lt;&#x2F;code&gt; and
&lt;code&gt;pop&lt;&#x2F;code&gt;, check its length, and access via index in O(1) time. We also
have a nifty &lt;code&gt;vec!&lt;&#x2F;code&gt; macro for constructing them:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; v: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Vec&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;gt; = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;vec!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;];
    v.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;push&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;assert_eq!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(v.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;pop&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(), &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;));
    v.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;push&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
    v.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;push&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
    v.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;push&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;6&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;assert_eq!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(v.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;pop&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(), &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;6&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;));
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;assert_eq!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(v[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;], &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{:?}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, v); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;&#x2F;&#x2F; 1, 2, 3, 4, 5
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;slices&quot;&gt;Slices&lt;&#x2F;h3&gt;
&lt;p&gt;I&#x27;m going to write a helper function that prints all the values in a
&lt;code&gt;Vec&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; v: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Vec&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;gt; = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;vec!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;];
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;print_vals&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(v);
}

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;print_vals&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;v&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Vec&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;gt;) {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; i &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;in&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; v {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, i);
    }
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Of course, since this is a pass-by-value, the following doesn&#x27;t compile:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; v: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Vec&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;gt; = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;vec!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;];
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;print_vals&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(v);
    v.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;push&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;print_vals&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(v);
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Easy enough to fix: have &lt;code&gt;print_vals&lt;&#x2F;code&gt; take a reference to a &lt;code&gt;Vec&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; v: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Vec&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;gt; = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;vec!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;];
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;print_vals&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;v);
    v.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;push&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;print_vals&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;v);
}

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;print_vals&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;v&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;Vec&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;gt;) {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; i &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;in&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; v {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, i);
    }
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Unfortunately, this doesn&#x27;t generalize to, say, an array:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; a: [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;] = [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;];
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;print_vals&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;a);
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This fails since &lt;code&gt;print_vals&lt;&#x2F;code&gt; takes a &lt;code&gt;&amp;amp;Vec&amp;lt;u32&amp;gt;&lt;&#x2F;code&gt;, but we&#x27;ve provided
a &lt;code&gt;&amp;amp;[u32; 5]&lt;&#x2F;code&gt;. But this is pretty disappointing. A dynamic vector and
a fixed length array behave the same for so many things. Wouldn&#x27;t it
be nice if there was something that generalized both of these?&lt;&#x2F;p&gt;
&lt;p&gt;Enter slices. To quote the Rust book:&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;Slices let you reference a contiguous sequence of elements in a
collection rather than the whole collection.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;To make this all work, we need to change the signature of &lt;code&gt;print_vals&lt;&#x2F;code&gt;
to:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;print_vals&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;v&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;]) {
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;code&gt;&amp;amp;[u32]&lt;&#x2F;code&gt; is a reference to a &lt;em&gt;slice&lt;&#x2F;em&gt; of &lt;code&gt;u32&lt;&#x2F;code&gt;s. A slice can be created
from an array &lt;em&gt;or&lt;&#x2F;em&gt; a &lt;code&gt;Vec&lt;&#x2F;code&gt;, not to mention some other cases as
well. (We&#x27;ll discuss how the &lt;code&gt;&amp;amp;&lt;&#x2F;code&gt; borrow operator works its magic in a
bit.) As a general piece of advice, if you&#x27;re receiving a parameter
which is a sequence of values, try to use a slice, as it will give the
caller much more control about where the data comes from.&lt;&#x2F;p&gt;
&lt;p&gt;I played a bit of a word game above, switching between &amp;quot;reference to a
slice&amp;quot; and &amp;quot;a slice.&amp;quot; Obviously we&#x27;re using a reference. Can we
dereference a slice reference and get the slice itself? Let&#x27;s try!&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;print_vals&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;vref&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;]) {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; v = *vref;
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; i &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;in&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; v {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, i);
    }
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The compiler is cross with us again:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;error[E0277]: the size for values of type `[u32]` cannot be known at compilation time
 --&amp;gt; src&#x2F;main.rs:7:9
  |
7 |     let v = *vref;
  |         ^ doesn&amp;#39;t have a size known at compile-time
  |
  = help: the trait `std::marker::Sized` is not implemented for `[u32]`
  = note: to learn more, visit &amp;lt;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;book&#x2F;second-edition&#x2F;ch19-04-advanced-types.html#dynamically-sized-types-and-sized&amp;gt;
  = note: all local variables must have a statically known size

error[E0277]: the size for values of type `[u32]` cannot be known at compilation time
 --&amp;gt; src&#x2F;main.rs:8:14
  |
8 |     for i in v {
  |              ^ doesn&amp;#39;t have a size known at compile-time
  |
  = help: the trait `std::marker::Sized` is not implemented for `[u32]`
  = note: to learn more, visit &amp;lt;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;book&#x2F;second-edition&#x2F;ch19-04-advanced-types.html#dynamically-sized-types-and-sized&amp;gt;
  = note: required by `std::iter::IntoIterator::into_iter`

error[E0277]: the trait bound `[u32]: std::iter::Iterator` is not satisfied
 --&amp;gt; src&#x2F;main.rs:8:14
  |
8 |     for i in v {
  |              ^ `[u32]` is not an iterator; maybe try calling `.iter()` or a similar method
  |
  = help: the trait `std::iter::Iterator` is not implemented for `[u32]`
  = note: required by `std::iter::IntoIterator::into_iter`
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Basically, there&#x27;s no way to dereference a slice. It logically makes
sense in any event to just keep a reference to the block of memory
holding the values, whether it&#x27;s on the stack, heap, or the executable
itself (like string literals, which we&#x27;ll get to later).&lt;&#x2F;p&gt;
&lt;h2 id=&quot;deref&quot;&gt;Deref&lt;&#x2F;h2&gt;
&lt;p&gt;There&#x27;s something fishy; why does the ampersand&#x2F;borrow operator give
us different types? The following compiles just fine!&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; v = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;vec!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;];
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;Vec&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;gt; = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;v;
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;] = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;v;
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;It turns out that the borrow operator interacts with &amp;quot;&lt;code&gt;Deref&lt;&#x2F;code&gt;
coercion.&amp;quot; If you&#x27;re curious about this, please check out &lt;a href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;std&#x2F;ops&#x2F;trait.Deref.html&quot;&gt;the docs
for the &lt;code&gt;Deref&lt;&#x2F;code&gt;
trait&lt;&#x2F;a&gt;. As an
example, I can create a new struct which can be borrowed into a slice:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;std::ops::Deref;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;struct &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;MyArray&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;([&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;; 5]);

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;impl &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;MyArray &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() -&amp;gt; MyArray {
        MyArray([&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;42&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;])
    }
}

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;impl &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;Deref &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;MyArray &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;Target &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;];

    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;deref&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Self::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;Target {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;0
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
}

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; ma = MyArray::new();
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;MyArray = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;ma;
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;] = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;ma;
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Thanks to udoprog for &lt;a href=&quot;https:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;53250856&#x2F;can-i-borrow-a-custom-type-into-a-slice&quot;&gt;answering this
question&lt;&#x2F;a&gt;. Also,
just because you &lt;em&gt;can&lt;&#x2F;em&gt; do this &lt;a href=&quot;https:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;45086595&#x2F;is-it-considered-a-bad-practice-to-implement-deref-for-newtypes&quot;&gt;doesn&#x27;t necessarily mean you
should&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;using-slices&quot;&gt;Using slices&lt;&#x2F;h2&gt;
&lt;p&gt;Slices are data types like any others. You can check out the
&lt;a href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;std&#x2F;slice&#x2F;index.html&quot;&gt;&lt;code&gt;std::slice&lt;&#x2F;code&gt; module
documentation&lt;&#x2F;a&gt; and the
&lt;a href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;std&#x2F;primitive.slice.html&quot;&gt;&lt;code&gt;slice&lt;&#x2F;code&gt; primitive
type&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Some common ways to interact with them include:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Using them as &lt;code&gt;Iterator&lt;&#x2F;code&gt;s&lt;&#x2F;li&gt;
&lt;li&gt;Indexing them with &lt;code&gt;slice[idx]&lt;&#x2F;code&gt; syntax&lt;&#x2F;li&gt;
&lt;li&gt;Taking subslices with &lt;code&gt;slice[start..end]&lt;&#x2F;code&gt; syntax&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h2 id=&quot;byte-literals&quot;&gt;Byte literals&lt;&#x2F;h2&gt;
&lt;p&gt;If you put a lower case &lt;code&gt;b&lt;&#x2F;code&gt; in front of a string literal, you&#x27;ll get a
byte array. You can either treat this as a fixed length array or, more
commonly, as a slice:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; bytearray1: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u8&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;22&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;] = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;b&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Hello World in binary!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; bytearray2: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u8&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;] = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;b&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Hello World in binary!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{:?}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, bytearray1);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{:?}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, bytearray2);
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Note that you always receive a &lt;em&gt;reference&lt;&#x2F;em&gt; to the value, not the value
itself. The data is stored in the program executable itself, and
therefore cannot be modified (thus always receiving an immutable
reference).&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;Exercise 4&lt;&#x2F;strong&gt; Add lifetime parameters to the &lt;code&gt;bytearray1&lt;&#x2F;code&gt; and
&lt;code&gt;bytearray2&lt;&#x2F;code&gt; types above.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;strings&quot;&gt;Strings&lt;&#x2F;h2&gt;
&lt;p&gt;And finally we can talk about strings! You may think that a string
literal would be a fixed length array of &lt;code&gt;char&lt;&#x2F;code&gt;s. You can in fact
create such a thing:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; char_array: [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;char&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;] = [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;H&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;e&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;l&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;l&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;o&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;];
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{:?}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, char_array);
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;However, this is &lt;em&gt;not&lt;&#x2F;em&gt; what a &lt;code&gt;str&lt;&#x2F;code&gt; is. The representation above is
highly inefficient. Since a &lt;code&gt;char&lt;&#x2F;code&gt; in Rust has full Unicode support,
it takes up 4 bytes in memory (32 bits). However, for most data, this
is overkill. A character encoding like UTF-8 will be far more
efficient.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;NOTE&lt;&#x2F;strong&gt; If you&#x27;re not familiar with Unicode and character encodings,
it&#x27;s safe to gloss over these details here. It&#x27;s not vitally important
to understanding how strings work in Rust.&lt;&#x2F;p&gt;
&lt;p&gt;Instead, a string slice (&lt;code&gt;&amp;amp;str&lt;&#x2F;code&gt;) is essentially a newtype wrapper
around a byte slice (&lt;code&gt;&amp;amp;[u8]&lt;&#x2F;code&gt;), which is guaranteed to be in UTF-8
encoding. This has some important trade-offs:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;You can cheaply (freely?) convert from a &lt;code&gt;&amp;amp;str&lt;&#x2F;code&gt; to a &lt;code&gt;&amp;amp;[u8]&lt;&#x2F;code&gt;, which
can be great for making system calls&lt;&#x2F;li&gt;
&lt;li&gt;You cannot get O(1) random access within strings, since the UTF-8
encoding doesn&#x27;t allow for this. Instead, you need to work with a
character iterator to view the individual characters.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;&lt;strong&gt;Exercise 5&lt;&#x2F;strong&gt; Use &lt;code&gt;std::env::args&lt;&#x2F;code&gt; and the &lt;code&gt;chars()&lt;&#x2F;code&gt; method on
&lt;code&gt;String&lt;&#x2F;code&gt; to print out the number of characters in each command line
arguments. Bonus points: also print out the number of bytes. Sample
usage:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;$ cargo run שלום
arg: target&#x2F;debug&#x2F;foo, characters: 16, bytes: 16
arg: שלום, characters: 4, bytes: 8
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Don&#x27;t forget, the first argument is the name of the executable.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;lifetimes-in-data-structures&quot;&gt;Lifetimes in data structures&lt;&#x2F;h2&gt;
&lt;p&gt;One final topic for today is lifetimes in data structures. It&#x27;s
entirely possible to keep references in your data structures. However,
when you do so, you need to be explicit about their lifetimes. For
example, this will fail to compile:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;struct &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;Person &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;str&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;,
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;age&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;,
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Instead, you would need to write it as:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;struct &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;Person&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&amp;#39;a&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;gt; {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&amp;#39;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;str&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;,
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;age&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;,
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The general recommendation I&#x27;ve received, and which I&#x27;d pass on, is
avoid this when possible. Things end up getting significantly more
complicated when dealing with lifetime parameters in data
structures. Typically, you should use owned versions of values
(e.g. &lt;code&gt;String&lt;&#x2F;code&gt; instead of &lt;code&gt;&amp;amp;str&lt;&#x2F;code&gt;, or &lt;code&gt;Vec&lt;&#x2F;code&gt; or array instead of a
slice) inside your data structures. In such a case, you need to
ensure that the lifetime of the reference within the structure
outlives the structure itself.&lt;&#x2F;p&gt;
&lt;p&gt;There are times when you can avoid some extra cloning and allocation
if you use references in your data structure, and the time will
probably come when you need to do it. But I&#x27;d recommend waiting until
your profiling points you at a specific decision being the bottleneck.
For more information, see &lt;a href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;book&#x2F;second-edition&#x2F;ch10-03-lifetime-syntax.html#lifetime-annotations-in-struct-definitions&quot;&gt;the Rust book&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;references-and-slices-in-apis&quot;&gt;References and slices in APIs&lt;&#x2F;h2&gt;
&lt;p&gt;Some general advice which I received and has mostly steered me
correctly is:&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;When receiving parameters, prefer slices when possible&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;However, there are times when this is overly simplistic. If you want a
deeper dive, there a great blog post covering some trade-offs in
public APIs: &lt;a href=&quot;https:&#x2F;&#x2F;phaazon.net&#x2F;blog&#x2F;on-owning-borrowing-pub-interface&quot;&gt;On dealing with owning and borrowing in public
interfaces&lt;&#x2F;a&gt;. The
&lt;a href=&quot;https:&#x2F;&#x2F;www.reddit.com&#x2F;r&#x2F;rust&#x2F;comments&#x2F;9tzygo&#x2F;on_dealing_with_owning_and_borrowing_in_public&#x2F;&quot;&gt;Reddit
discussion&lt;&#x2F;a&gt;
is also great.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;www.fpcomplete.com&#x2F;rust&quot;&gt;Rust at FP Complete&lt;&#x2F;a&gt; | &lt;a href=&quot;https:&#x2F;&#x2F;www.snoyman.com&#x2F;blog&#x2F;2018&#x2F;10&#x2F;introducing-rust-crash-course&quot;&gt;Introduction&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
</description>
        </item>
        <item>
            <title>Rule of Three - Parameters, Iterators, and Closures - Rust Crash Course lesson 5 - exercise solutions</title>
            <pubDate>Wed, 21 Nov 2018 00:00:00 +0000</pubDate>
            <link>https://www.snoyman.com/blog/2018/11/rust-crash-course-05-rule-of-three-solutions/</link>
            <guid>https://www.snoyman.com/blog/2018/11/rust-crash-course-05-rule-of-three-solutions/</guid>
            <description>&lt;p&gt;&lt;strong&gt;Heads up&lt;&#x2F;strong&gt; This blog post series has been updated and published as an eBook by FP Complete. I&#x27;d recommend reading that version instead of these posts. If you&#x27;re interested, please check out the &lt;a href=&quot;https:&#x2F;&#x2F;www.fpcomplete.com&#x2F;rust&#x2F;crash-course&#x2F;&quot;&gt;Rust Crash Course eBook&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Below are the solutions to the exercises from the last Rust Crash
Course lesson, &amp;quot;Rule of Three - Parameters, Iterators, and Closures.&amp;quot;&lt;&#x2F;p&gt;
&lt;p&gt;This post is part of a series based on &lt;a href=&quot;https:&#x2F;&#x2F;www.fpcomplete.com&#x2F;rust&quot;&gt;teaching Rust at FP
Complete&lt;&#x2F;a&gt;. If you&#x27;re reading this post outside
of the blog, you can find links to all posts in the series &lt;a href=&quot;https:&#x2F;&#x2F;www.snoyman.com&#x2F;blog&#x2F;2018&#x2F;10&#x2F;introducing-rust-crash-course&quot;&gt;at the top of the
introduction
post&lt;&#x2F;a&gt;. You
can also &lt;a href=&quot;https:&#x2F;&#x2F;www.snoyman.com&#x2F;feed&#x2F;rust-crash-course&quot;&gt;subscribe to the RSS
feed&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;exercise-1&quot;&gt;Exercise 1&lt;&#x2F;h2&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;double&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;x&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) {
    *x *= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
}

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; x = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;double&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; x);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, x);
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Notice that the variable &lt;code&gt;x&lt;&#x2F;code&gt; does not need to be mutable, since we&#x27;re
only modifying the value it references.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;exercise-2&quot;&gt;Exercise 2&lt;&#x2F;h2&gt;
&lt;p&gt;The (IMO) straightforward solution is:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;struct &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;InfiniteUnit&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;impl &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;IntoIterator &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;InfiniteUnit &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;Item &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= ();
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;IntoIter &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= InfiniteUnitIter;

    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;into_iter&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Self::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;IntoIter {
        InfiniteUnitIter
    }
}

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;struct &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;InfiniteUnitIter&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;impl &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;Iterator &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;InfiniteUnitIter &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;Item &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= ();
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;next&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Option&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;()&amp;gt; {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(())
    }
}

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; count = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for _ in&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; InfiniteUnit {
        count += &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;count == &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, count);
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; count &amp;gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;5 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;break&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
        }
    }
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;However, if you want to be a bit more clever, there&#x27;s already a
function in the standard library that creates an infinite iterator,
called
&lt;a href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;std&#x2F;iter&#x2F;fn.repeat.html&quot;&gt;&lt;code&gt;repeat&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;. Using
that, you can bypass the extra struct here:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;struct &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;InfiniteUnit&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;impl &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;IntoIterator &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;InfiniteUnit &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;Item &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= ();
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;IntoIter &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= std::iter::Repeat&amp;lt;()&amp;gt;;

    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;into_iter&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Self::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;IntoIter {
        std::iter::repeat(())
    }
}

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; count = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for _ in&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; InfiniteUnit {
        count += &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;count == &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, count);
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; count &amp;gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;5 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;break&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
        }
    }
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;exercise-3&quot;&gt;Exercise 3&lt;&#x2F;h2&gt;
&lt;p&gt;The closure version:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; msg: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;str &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Hi!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;say_hi &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;msg&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, msg);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;say_hi&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(msg);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;say_hi&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(msg);
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And the function version:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; msg: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;str &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Hi!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;say_hi&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;msg&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;str&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, msg);
    }
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;say_hi&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(msg);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;say_hi&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(msg);
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Since &lt;code&gt;say_hi&lt;&#x2F;code&gt; is no longer referring to any variables in the local
scope, it doesn&#x27;t need to be a closure.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;exercise-4&quot;&gt;Exercise 4&lt;&#x2F;h2&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;call_with_hi&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(say_message);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;call_with_hi&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(say_message);
}

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;say_message&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;msg&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;str&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, msg);
}

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;call_with_hi&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;F&amp;gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: F)
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;where&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; F: Fn(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;str&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
{
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Hi!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;exercise-5&quot;&gt;Exercise 5&lt;&#x2F;h2&gt;
&lt;p&gt;The first error message we get is:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;error[E0599]: no method named `map` found for type `std::vec::Vec&amp;lt;u32&amp;gt;` in the current scope
 --&amp;gt; main.rs:5:23
  |
5 |         for i in nums.map(unimplemented!()) {
  |                       ^^^
  |
  = note: the method `map` exists but the following trait bounds were not satisfied:
          `&amp;amp;mut std::vec::Vec&amp;lt;u32&amp;gt; : std::iter::Iterator`
          `&amp;amp;mut [u32] : std::iter::Iterator`
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Looks like we need to get an &lt;code&gt;Iterator&lt;&#x2F;code&gt; out of our &lt;code&gt;nums&lt;&#x2F;code&gt;. We have
three different choices: &lt;code&gt;into_iter()&lt;&#x2F;code&gt;, &lt;code&gt;iter()&lt;&#x2F;code&gt;, and
&lt;code&gt;iter_mut()&lt;&#x2F;code&gt;. Since we need to use the result multiple times, and
don&#x27;t need any mutation, &lt;code&gt;iter()&lt;&#x2F;code&gt; seems like the right call. Once we
replace &lt;code&gt;nums.map&lt;&#x2F;code&gt; with &lt;code&gt;nums.iter().map&lt;&#x2F;code&gt;, we can move on to the
&lt;code&gt;unimplemented!()&lt;&#x2F;code&gt; bit.&lt;&#x2F;p&gt;
&lt;p&gt;We need a closure that will double a number. That&#x27;s pretty easy: &lt;code&gt;|x| x * 2&lt;&#x2F;code&gt;. Plugging that in works! Extra challenge: is that closure a
&lt;code&gt;FnOnce&lt;&#x2F;code&gt;, &lt;code&gt;FnMut&lt;&#x2F;code&gt;, or &lt;code&gt;Fn&lt;&#x2F;code&gt;?&lt;&#x2F;p&gt;
&lt;h2 id=&quot;exercise-6&quot;&gt;Exercise 6&lt;&#x2F;h2&gt;
&lt;p&gt;You need to add a &lt;code&gt;.unwrap()&lt;&#x2F;code&gt; call on the &lt;code&gt;create&lt;&#x2F;code&gt; call:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;std::io::Write;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; file = std::fs::File::create(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;mylog.txt&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;();
file.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;write_all&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;b&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;I was clicked.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#dc322f;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Like this, you&#x27;ll get a warning from the compiler that you&#x27;ve ignored
the &lt;code&gt;Result&lt;&#x2F;code&gt; coming from &lt;code&gt;write_all&lt;&#x2F;code&gt;. That&#x27;s bad practice, and the
compiler is rightfully yelling at you. You can fix that with
&lt;code&gt;unwrap()&lt;&#x2F;code&gt;. However, that&#x27;s also bad practice :).&lt;&#x2F;p&gt;
&lt;h2 id=&quot;exercise-7&quot;&gt;Exercise 7&lt;&#x2F;h2&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extern crate&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; gtk;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;gtk::prelude::*;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;gtk::{Button, Window, WindowType};

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;std::cell::RefCell;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;std::io::Write;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Result&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;(), &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Box&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;std::error::Error&amp;gt;&amp;gt; {
    gtk::init()&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;

    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; window = Window::new(WindowType::Toplevel);
    window.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;set_title&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;First GTK+ Program&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
    window.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;set_default_size&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;350&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;70&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; button = Button::new_with_label(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Click me!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
    window.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;add&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;button);
    window.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;show_all&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;();

    window.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;connect_delete_event&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|_, _| {
        gtk::main_quit();
        Inhibit(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;false&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
    });

    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; file = std::fs::File::create(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;mylog.txt&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; file = RefCell::new(file);
    button.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;connect_clicked&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;move &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;|_| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;match&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; file.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;borrow_mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;().&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;write_all&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;b&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;I was clicked.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#dc322f;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) {
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Ok&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(()) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(),
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Err&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(e) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt; eprintln!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Error writing to file: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, e),
        }
    });

    gtk::main();

    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Ok&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(())
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;www.fpcomplete.com&#x2F;rust&quot;&gt;Rust at FP Complete&lt;&#x2F;a&gt; | &lt;a href=&quot;https:&#x2F;&#x2F;www.snoyman.com&#x2F;blog&#x2F;2018&#x2F;10&#x2F;introducing-rust-crash-course&quot;&gt;Introduction&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
</description>
        </item>
        <item>
            <title>Rule of Three - Parameters, Iterators, and Closures - Rust Crash Course lesson 5</title>
            <pubDate>Mon, 19 Nov 2018 00:00:00 +0000</pubDate>
            <link>https://www.snoyman.com/blog/2018/11/rust-crash-course-05-rule-of-three/</link>
            <guid>https://www.snoyman.com/blog/2018/11/rust-crash-course-05-rule-of-three/</guid>
            <description>&lt;p&gt;&lt;strong&gt;Heads up&lt;&#x2F;strong&gt; This blog post series has been updated and published as an eBook by FP Complete. I&#x27;d recommend reading that version instead of these posts. If you&#x27;re interested, please check out the &lt;a href=&quot;https:&#x2F;&#x2F;www.fpcomplete.com&#x2F;rust&#x2F;crash-course&#x2F;&quot;&gt;Rust Crash Course eBook&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;In this lesson, we&#x27;re going to cover what I&#x27;m dubbing the &amp;quot;rule of
three,&amp;quot; which applies to function parameters, iterators, and
closures. We&#x27;ve already seen this rule applied to function parameters,
but didn&#x27;t discuss is so explicitly. We&#x27;ll expand on parameters, and
use that to launch into new information on both iterators and
closures.&lt;&#x2F;p&gt;
&lt;p&gt;This post is part of a series based on &lt;a href=&quot;https:&#x2F;&#x2F;www.fpcomplete.com&#x2F;rust&quot;&gt;teaching Rust at FP
Complete&lt;&#x2F;a&gt;. If you&#x27;re reading this post outside
of the blog, you can find links to all posts in the series &lt;a href=&quot;https:&#x2F;&#x2F;www.snoyman.com&#x2F;blog&#x2F;2018&#x2F;10&#x2F;introducing-rust-crash-course&quot;&gt;at the top of the
introduction
post&lt;&#x2F;a&gt;. You
can also &lt;a href=&quot;https:&#x2F;&#x2F;www.snoyman.com&#x2F;feed&#x2F;rust-crash-course&quot;&gt;subscribe to the RSS
feed&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;types-of-parameters&quot;&gt;Types of parameters&lt;&#x2F;h2&gt;
&lt;p&gt;The first thing I want to deal with is a potential misconception. This
may be one of those &amp;quot;my brain has been scrambled by Haskell&amp;quot;
misconceptions that imperative programmers won&#x27;t feel, so apologies if
I&#x27;m just humoring myself and other Haskellers.&lt;&#x2F;p&gt;
&lt;p&gt;Do these two functions have the same type signature?&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;foo&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;person&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: Person) { &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;unimplemented!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() }
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;bar&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;person&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: Person) { &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;unimplemented!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() }
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The Haskeller in me screams &amp;quot;they&#x27;re different!&amp;quot; However, they&#x27;re
&lt;em&gt;exactly the same&lt;&#x2F;em&gt;. The &lt;em&gt;inner mutability&lt;&#x2F;em&gt; of the &lt;code&gt;person&lt;&#x2F;code&gt; variable in
the function is &lt;em&gt;irrelevant&lt;&#x2F;em&gt; to someone calling the function. The
caller of the function will move the &lt;code&gt;Person&lt;&#x2F;code&gt; value into the function,
regardless of whether the value can be mutated or not. We&#x27;ve already
seen a hint of this: the fact that we can pass an immutable value to a
function like &lt;code&gt;foo&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; alice = Person { name: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::from(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Alice&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;), age: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;30 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;};
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;foo&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(alice); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;&#x2F;&#x2F; it works!
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;With that misconception out of the way, let&#x27;s consider two other
similar functions:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;baz&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;person&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;Person) { &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;unimplemented!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() }
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;bin&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;person&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; Person) { &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;unimplemented!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() }
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Firstly, it&#x27;s pretty easy to say that both &lt;code&gt;baz&lt;&#x2F;code&gt; and &lt;code&gt;bin&lt;&#x2F;code&gt; have
different signatures than &lt;code&gt;foo&lt;&#x2F;code&gt;. These are taking references to a
&lt;code&gt;Person&lt;&#x2F;code&gt;, not a &lt;code&gt;Person&lt;&#x2F;code&gt; itself. But what about &lt;code&gt;baz&lt;&#x2F;code&gt; vs &lt;code&gt;bin&lt;&#x2F;code&gt;? Are
they the same or different? You may be tempted to follow the same
logic as &lt;code&gt;foo&lt;&#x2F;code&gt; vs &lt;code&gt;bar&lt;&#x2F;code&gt; and decide that the &lt;code&gt;mut&lt;&#x2F;code&gt; is an internal
detail of the function. But this isn&#x27;t true! Observe:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; alice = Person { name: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::from(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Alice&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;), age: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;30 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;};
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;baz&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;alice); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;&#x2F;&#x2F; this works
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;bin&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;alice); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;&#x2F;&#x2F; this fails
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;bin&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; alice); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;&#x2F;&#x2F; but this works
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The first call to &lt;code&gt;bin&lt;&#x2F;code&gt; will not compile, because &lt;code&gt;bin&lt;&#x2F;code&gt; requires a
&lt;em&gt;mutable&lt;&#x2F;em&gt; reference, and we&#x27;ve provided an &lt;em&gt;immutable&lt;&#x2F;em&gt; reference. We
need to use the second version of the call. And not only does this
have a &lt;em&gt;syntactic&lt;&#x2F;em&gt; difference, but a &lt;em&gt;semantic&lt;&#x2F;em&gt; difference as well:
we&#x27;ve taken a mutable reference, which means we can have no other
references at the same time (remember our borrow rules from lesson 2).&lt;&#x2F;p&gt;
&lt;p&gt;The upshot of this is that there are three different ways we can pass
a value into a function which appear at the type level:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Pass by value (move semantics), like &lt;code&gt;foo&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;Pass by immutable reference, like &lt;code&gt;baz&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;Pass by mutable reference, like &lt;code&gt;bin&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;In addition, orthogonally, the variable that captures that parameters
can itself be either immutable or mutable.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;mutable-vs-immutable-pass-by-value&quot;&gt;Mutable vs immutable pass-by-value&lt;&#x2F;h3&gt;
&lt;p&gt;This one is relatively easy to see. What extra functionality do we get
by having a mutable pass-by-value? The ability to mutate the value of
course! Let&#x27;s look at two different ways of implementing a birthday
function, which increases someone&#x27;s age by 1.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;#[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;derive&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(Debug)]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;struct &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;Person &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: String,
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;age&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;,
}

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;birthday_immutable&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;person&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: Person) -&amp;gt; Person {
    Person {
        name: person.name,
        age: person.age + &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;,
    }
}

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;birthday_mutable&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;person&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: Person) -&amp;gt; Person {
    person.age += &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
    person
}

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; alice1 = Person { name: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::from(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Alice&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;), age: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;30 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;};
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Alice 1: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{:?}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, alice1);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; alice2 = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;birthday_immutable&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(alice1);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Alice 2: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{:?}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, alice2);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; alice3 = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;birthday_mutable&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(alice2);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Alice 3: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{:?}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, alice3);
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Some important takeaways:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Our &lt;code&gt;_immutable&lt;&#x2F;code&gt; implementation follows a more functional idiom,
creating a new &lt;code&gt;Person&lt;&#x2F;code&gt; value by deconstructing the original
&lt;code&gt;Person&lt;&#x2F;code&gt; value. This works just fine in Rust, but is not idiomatic,
and potentially less efficient.&lt;&#x2F;li&gt;
&lt;li&gt;We call both versions of this function in exactly the same way,
reinforcing the claim that these two functions have the same
signature.&lt;&#x2F;li&gt;
&lt;li&gt;You cannot reuse the &lt;code&gt;alice1&lt;&#x2F;code&gt; or &lt;code&gt;alice2&lt;&#x2F;code&gt; values in &lt;code&gt;main&lt;&#x2F;code&gt;, since
they&#x27;ve been moved during their calls.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;alice2&lt;&#x2F;code&gt; is an immutable variable, but it still gets passed in to a
function which mutates it.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h3 id=&quot;mutable-vs-immutable-pass-by-mutable-reference&quot;&gt;Mutable vs immutable pass-by-mutable-reference&lt;&#x2F;h3&gt;
&lt;p&gt;This one already gets significantly harder to observe, which indicates a simple fact of Rust: &lt;em&gt;it&#x27;s unusual to want a mutable variable for references&lt;&#x2F;em&gt;. The example below is very contrived, and requires playing with the more advanced concept of explicit lifetime parameters to even make it make sense. But it does demonstrate the difference between where the &lt;code&gt;mut&lt;&#x2F;code&gt; appears.&lt;&#x2F;p&gt;
&lt;p&gt;Before we dive in: parameters that begin with a single quote (&lt;code&gt;&#x27;&lt;&#x2F;code&gt;) are
&lt;em&gt;lifetime parameters&lt;&#x2F;em&gt;, and indicate how long a reference needs to
live. In the examples below, we&#x27;re saying &amp;quot;the two references must
have the same lifetime.&amp;quot; We won&#x27;t cover this in more detail here, at
least not yet. If you want to learn about lifetimes, please &lt;a href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;book&#x2F;2018-edition&#x2F;ch10-03-lifetime-syntax.html&quot;&gt;check out
the Rust
book&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;OK, let&#x27;s see a difference between an immutable variable holding a
mutable reference and a mutable variable holding a mutable reference!&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;#[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;derive&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(Debug)]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;struct &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;Person &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: String,
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;age&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;,
}

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;birthday_immutable&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;person&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; Person) {
    person.age += &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
}

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;birthday_mutable&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&amp;#39;a&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;person&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&amp;#39;a mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; Person, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;replacement&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&amp;#39;a mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; Person) {
    person = replacement;
    person.age += &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
}

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; alice = Person { name: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::from(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Alice&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;), age: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;30 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;};
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; bob = Person { name: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::from(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Bob&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;), age: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;20 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;};
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Alice 1: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{:?}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;, Bob 1: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{:?}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, alice, bob);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;birthday_immutable&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; alice);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Alice 2: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{:?}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;, Bob 2: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{:?}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, alice, bob);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;birthday_mutable&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; alice, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; bob);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Alice 3: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{:?}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;, Bob 3: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{:?}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, alice, bob);
}

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;&#x2F;&#x2F; does not compile
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;birthday_immutable_broken&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&amp;#39;a&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;person&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&amp;#39;a mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; Person, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;replacement&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&amp;#39;a mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; Person) {
    person = replacement;
    person.age += &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;code&gt;birthday_immutable&lt;&#x2F;code&gt; is fairly simple. We have a mutable reference,
and we&#x27;ve stored it in an immutable variable. We&#x27;ve completely free to
mutate the value pointed to by that reference. The takeaway is: we&#x27;re
mutating the value, &lt;em&gt;not&lt;&#x2F;em&gt; the variable, which is remaining the same.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;birthday_mutable&lt;&#x2F;code&gt; is a contrived, ugly mess, but it demonstrates our
point. Here, we take &lt;em&gt;two&lt;&#x2F;em&gt; references: a &lt;code&gt;person&lt;&#x2F;code&gt;, and a
&lt;code&gt;replacement&lt;&#x2F;code&gt;. They&#x27;re both mutable references, but &lt;code&gt;person&lt;&#x2F;code&gt; is in a
mutable variable. The first thing we do is &lt;code&gt;person = replacement;&lt;&#x2F;code&gt;. This changes what our &lt;code&gt;person&lt;&#x2F;code&gt; variable is pointing at,
and &lt;em&gt;does not modify&lt;&#x2F;em&gt; the original value being pointed at by the
reference at all. In fact, when compiling this, we&#x27;ll get a warning
that we never used the value passed to &lt;code&gt;person&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;warning: value passed to `person` is never read
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Notice that we needed to mark both &lt;code&gt;alice&lt;&#x2F;code&gt; and &lt;code&gt;bob&lt;&#x2F;code&gt; as mutable in
&lt;code&gt;main&lt;&#x2F;code&gt; in this example. That&#x27;s because we pass them by mutable
reference, which requires that we have the ability to mutate
them. This is different from pass-by-value with move semantics,
because in our &lt;code&gt;main&lt;&#x2F;code&gt; function, we can directly observe the effect of
mutating the references we&#x27;ve passed in.&lt;&#x2F;p&gt;
&lt;p&gt;Also notice that we also have a &lt;code&gt;birthday_immutable_broken&lt;&#x2F;code&gt;
version. As you may guess from the name, it doesn&#x27;t compile. We cannot
change what &lt;code&gt;person&lt;&#x2F;code&gt; points to if it is an immutable variable.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;Challenge&lt;&#x2F;strong&gt; Figure out what the output of this program is going to
be before you run it.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;mutable-vs-immutable-pass-by-immutable-reference&quot;&gt;Mutable vs immutable pass-by-immutable-reference&lt;&#x2F;h3&gt;
&lt;p&gt;I&#x27;m not actually going to cover this case, since it&#x27;s basically the
same as the previous one. If you mark a variable as mutable, you can
change which reference it holds. Feel free to play around with an
example like the one above using immutable references.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;mutable-to-immutable&quot;&gt;Mutable to immutable&lt;&#x2F;h3&gt;
&lt;p&gt;Let&#x27;s point out one final bit:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;needs_mutable&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;x&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) {
    *x *= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
}

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;needs_immutable&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;x&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, x);
}

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; x: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; y: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; x;
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;needs_immutable&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(y);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;needs_mutable&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(y);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;needs_immutable&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(y);
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;From what I&#x27;ve told you so far, you should expect this program to fail
to compile. &lt;code&gt;y&lt;&#x2F;code&gt; is of type &lt;code&gt;&amp;amp;mut u32&lt;&#x2F;code&gt;, but we&#x27;re passing it to
&lt;code&gt;needs_immutable&lt;&#x2F;code&gt; which requires a &lt;code&gt;&amp;amp;u32&lt;&#x2F;code&gt;. Type mismatch, go home!&lt;&#x2F;p&gt;
&lt;p&gt;Not so fast: since the guarantees of a mutable reference are strictly
stronger than an immutable reference, you can always use a mutable
reference where an immutable was needed. (Hold onto this, it will be
important for closures below.)&lt;&#x2F;p&gt;
&lt;h3 id=&quot;summary-of-the-rule-of-three-for-parameters&quot;&gt;Summary of the rule of three for parameters&lt;&#x2F;h3&gt;
&lt;p&gt;There are three types of parameters:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Pass by value&lt;&#x2F;li&gt;
&lt;li&gt;Pass by immutable reference&lt;&#x2F;li&gt;
&lt;li&gt;Pass by mutable reference&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;This is what I&#x27;m calling the rule of three. The captured variables
within a function can either be mutable or immutable, which is
orthogonal to the type of the parameter. However, it&#x27;s by far most
common to have a mutable variable with a pass-by-value. Also, at the
call site, a variable must be mutable if it is called on a pass by
mutable reference functions. Finally, you can use a mutable reference
where an immutable was requested.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;exercise-1&quot;&gt;Exercise 1&lt;&#x2F;h2&gt;
&lt;p&gt;Fix the program below so that it outputs the number 10. Ensure that
there are no compiler warnings.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;double&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;x&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) {
    x *= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
}

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; x = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;double&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(x);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, x);
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Hint: you&#x27;ll need to know how to &lt;em&gt;dereference&lt;&#x2F;em&gt; a reference, by putting
a asterisk (&lt;code&gt;*&lt;&#x2F;code&gt;) in front of the variable.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;iterators&quot;&gt;Iterators&lt;&#x2F;h2&gt;
&lt;p&gt;What&#x27;s the output of the program below?&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; nums = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;vec!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;];
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; i &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;in&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; nums {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, i);
    }
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;That&#x27;s right: it prints the numbers 1 to 5. How about this one?&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; i &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;..&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;3 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; nums = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;vec!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;];
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; j &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;in&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; nums {
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, i, j);
        }
    }
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;It prints &lt;code&gt;1,1&lt;&#x2F;code&gt;, &lt;code&gt;1,2&lt;&#x2F;code&gt;, ..., &lt;code&gt;1,5&lt;&#x2F;code&gt;, &lt;code&gt;2,1&lt;&#x2F;code&gt;, ..., &lt;code&gt;2,5&lt;&#x2F;code&gt;. Cool, easy
enough. Let&#x27;s move &lt;code&gt;nums&lt;&#x2F;code&gt; a bit. What does this do?&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; nums = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;vec!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;];
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; i &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;..&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;3 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; j &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;in&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; nums {
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, i, j);
        }
    }
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Trick question: it doesn&#x27;t compile!&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;error[E0382]: use of moved value: `nums`
 --&amp;gt; main.rs:4:18
  |
4 |         for j in nums {
  |                  ^^^^ value moved here in previous iteration of loop
  |
  = note: move occurs because `nums` has type `std::vec::Vec&amp;lt;i32&amp;gt;`, which does not implement the `Copy` trait

error: aborting due to previous error
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Well, that kind of makes sense. The first time we run through the
outer loop, we &lt;em&gt;move&lt;&#x2F;em&gt; the &lt;code&gt;nums&lt;&#x2F;code&gt; value into the inner loop. Then, we
can&#x27;t use the &lt;code&gt;nums&lt;&#x2F;code&gt; value again on the second pass through the
loop. OK, logical.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;Side note&lt;&#x2F;strong&gt; This was one of my personal &amp;quot;mind blown&amp;quot; moments with
Rust, realizing how sophisticated lifetime tracking was to work
through loops like this. Rust is pretty amazing.&lt;&#x2F;p&gt;
&lt;p&gt;We can go back to our previous version and put &lt;code&gt;nums&lt;&#x2F;code&gt; inside the first
&lt;code&gt;for&lt;&#x2F;code&gt; loop. That means recreating the value each time we pass through
that outer &lt;code&gt;for&lt;&#x2F;code&gt; loop. For our little vector example, it&#x27;s not a big
deal. But imagine constructing &lt;code&gt;nums&lt;&#x2F;code&gt; was expensive. This would be a
major overhead!&lt;&#x2F;p&gt;
&lt;p&gt;If we want to avoid the move of &lt;code&gt;nums&lt;&#x2F;code&gt;, can we get away with just
borrowing it instead? Yes we can!&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; nums = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;vec!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;];
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; i &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;..&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;3 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; j &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;in &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;nums {
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, i, j);
        }
    }
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This works, but I&#x27;ve got a question for you: what&#x27;s the type of &lt;code&gt;j&lt;&#x2F;code&gt;?
I&#x27;ve got a sneaky little trick to test out different options. If you
throw this in just above the &lt;code&gt;println!&lt;&#x2F;code&gt; call, you&#x27;ll get an error
message:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= j;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;However, this will compile just fine:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= j;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;By iterating over a reference to &lt;code&gt;nums&lt;&#x2F;code&gt;, we got a reference to each
value instead of the value itself. That makes sense. Can we complete
our &amp;quot;rule of three&amp;quot; with mutable references? Yet again, yes!&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; nums = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;vec!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;];
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; i &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;..&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;3 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; j &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;in &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; nums {
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= j;
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, i, j);
            *j *= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
        }
    }
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;strong&gt;Challenges&lt;&#x2F;strong&gt; First, there&#x27;s a compilation error in the program above. Try to catch
it before asking the compiler to help. Second, guess the output of
this program before running it.&lt;&#x2F;p&gt;
&lt;p&gt;Our rule of three translates into iterators as well! We have can
iterators of values, iterators of references, and iterators of mutable
references. Sweet!&lt;&#x2F;p&gt;
&lt;h3 id=&quot;new-nomenclature&quot;&gt;New nomenclature&lt;&#x2F;h3&gt;
&lt;p&gt;The &lt;code&gt;Vec&lt;&#x2F;code&gt; struct has three different methods on it that are relevant to our examples above. Starting with the mutable case, we can replace the line:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; j &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;in &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; nums {
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;with&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; j &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;in&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; nums.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;iter_mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The signature of that method is:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;pub &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;iter_mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) -&amp;gt; IterMut&amp;lt;T&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Similarly, we&#x27;ve got a &lt;code&gt;iter()&lt;&#x2F;code&gt; method that can replace our immutable
reference case:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; nums = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;vec!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;];
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; i &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;..&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;3 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; j &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;in&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; nums.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;iter&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= j;
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, i, j);
        }
    }
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And, finally, what about the iterator of values case? There, the
nomenclature is &lt;code&gt;into_iter&lt;&#x2F;code&gt;. The idea is that we are &lt;em&gt;converting&lt;&#x2F;em&gt; the
existing value &lt;em&gt;into&lt;&#x2F;em&gt; an iterator, consuming the previous value (the
&lt;code&gt;Vec&lt;&#x2F;code&gt; in this case) completely. This code won&#x27;t compile, go ahead and
fix it by moving the &lt;code&gt;let nums&lt;&#x2F;code&gt; statement.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; nums = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;vec!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;];
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; i &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;..&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;3 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; j &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;in&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; nums.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;into_iter&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, i, j);
        }
    }
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;reexamining-for-loops&quot;&gt;Reexamining for loops&lt;&#x2F;h3&gt;
&lt;p&gt;Here&#x27;s a cool little trick I didn&#x27;t mention before. &lt;code&gt;for&lt;&#x2F;code&gt; loops are a
bit more flexible than I&#x27;d implied. The &lt;code&gt;into_iter&lt;&#x2F;code&gt; method I mention
is actually part of a trait, appropriately named
&lt;a href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;std&#x2F;iter&#x2F;trait.IntoIterator.html&quot;&gt;&lt;code&gt;IntoIterator&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;. Whenever
you use &lt;code&gt;for x in y&lt;&#x2F;code&gt;, the compiler automatically calls &lt;code&gt;into_iter()&lt;&#x2F;code&gt;
on &lt;code&gt;y&lt;&#x2F;code&gt;. This allows you to loop over types which don&#x27;t actually have
their own implementation of &lt;code&gt;Iterator&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;exercise-2&quot;&gt;Exercise 2&lt;&#x2F;h3&gt;
&lt;p&gt;Make this program compile by defining an &lt;code&gt;IntoIterator&lt;&#x2F;code&gt;
implementation for &lt;code&gt;InfiniteUnit&lt;&#x2F;code&gt;. Do &lt;em&gt;not&lt;&#x2F;em&gt; define an &lt;code&gt;Iterator&lt;&#x2F;code&gt;
implementation for it. You&#x27;ll probably want to define an extra
datatype. (Extra credit: also try to find a helper function in the
standard library that repeats values.)&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;struct &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;InfiniteUnit&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; count = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for _ in&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; InfiniteUnit {
        count += &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;count == &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, count);
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; count &amp;gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;5 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;break&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
        }
    }
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;summary-of-the-rule-of-three-for-iterators&quot;&gt;Summary of the rule of three for iterators&lt;&#x2F;h3&gt;
&lt;p&gt;Just like function parameters, iterators come in three flavors,
corresponding to the following naming scheme:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;into_iter()&lt;&#x2F;code&gt; is an iterator of values, with move semantics&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;iter()&lt;&#x2F;code&gt; is an iterator of immutable references&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;iter_mut()&lt;&#x2F;code&gt; is an iterator of mutable references&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;Only &lt;code&gt;iter_mut()&lt;&#x2F;code&gt; requires that the original variable itself be mutable.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;closures&quot;&gt;Closures&lt;&#x2F;h2&gt;
&lt;p&gt;We&#x27;ve danced around closures a bit throughout the crash course so
far. Closures are like functions, in that they can be called on some
arguments. Closures are unlike functions in that they can capture
values from the local scope. We&#x27;ll demonstrate this in an example,
after a word of warning.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;One word of warning&lt;&#x2F;strong&gt; If you&#x27;re coming from a non-functional
programming background, you&#x27;ll likely find closures in Rust very
powerful, and surprisingly common in library usage. If you come from a
functional programming background, you&#x27;ll likely be annoyed at how
much you have to think about ownership of data when working with
closures. As a Haskeller, this is still the aspect of Rust I most
often get caught on. I promise, the trade-offs in the design are
logical and necessary to achieve Rust&#x27;s goals, but it can feel a bit
onerous when compared to Haskell, or even compared to Javascript.&lt;&#x2F;p&gt;
&lt;p&gt;Alright, back to our function vs closure thing. Did you know that you can define a function &lt;em&gt;inside another function&lt;&#x2F;em&gt;?&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;say_hi&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; msg: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;str &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Hi!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, msg);
    };
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;say_hi&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;();
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;say_hi&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;();
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;That&#x27;s pretty nifty. Let&#x27;s slightly refactor that:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; msg: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;str &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Hi!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;say_hi&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, msg);
    };
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;say_hi&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;();
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;say_hi&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;();
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Unfortunately, Rust doesn&#x27;t like that very much:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;error[E0434]: can&amp;#39;t capture dynamic environment in a fn item
 --&amp;gt; main.rs:4:24
  |
4 |         println!(&amp;quot;{}&amp;quot;, msg);
  |                        ^^^
  |
  = help: use the `|| { ... }` closure form instead

error: aborting due to previous error
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Fortunately, the compiler tells us &lt;em&gt;exactly&lt;&#x2F;em&gt; how to fix it: use a
closure! Let&#x27;s rewrite that:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; msg: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;str &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Hi!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;say_hi &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;|| {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, msg);
    };
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;say_hi&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;();
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;say_hi&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;();
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We now have a closure (introduced by &lt;code&gt;||&lt;&#x2F;code&gt;) which takes 0
arguments. And everything just works.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;&#x2F;strong&gt; You can shorten this a bit with &lt;code&gt;let say_hi = || println!(&amp;quot;{}&amp;quot;, msg);&lt;&#x2F;code&gt;,
which is more idiomatic.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;Exercise 3&lt;&#x2F;strong&gt; Rewrite the above so that instead of taking 0
arguments, &lt;code&gt;say_hi&lt;&#x2F;code&gt; takes a single argument: the &lt;code&gt;msg&lt;&#x2F;code&gt; variable. Then
try out the &lt;code&gt;fn&lt;&#x2F;code&gt; version again.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;the-type-of-a-closure&quot;&gt;The type of a closure&lt;&#x2F;h2&gt;
&lt;p&gt;What exactly is the type of &lt;code&gt;say_hi&lt;&#x2F;code&gt;? I&#x27;m going to use an ugly trick
to get the compiler to tell us: give it the &lt;em&gt;wrong&lt;&#x2F;em&gt; type, and then try
to compile. It&#x27;s probably safe to assume that a closure isn&#x27;t a &lt;code&gt;u32&lt;&#x2F;code&gt;,
so let&#x27;s try this:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; msg: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;str &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Hi!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; say_hi: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;u32 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;msg&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, msg);
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And we get the error message:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;error[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;E0308&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;]: mismatched types
 --&amp;gt; main.rs:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;23
  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;|
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;3 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;|     &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; say_hi: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;u32 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;msg&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, msg);
  |                       ^^^^^^^^^^^^^^^^^^^^^^^^^ expected &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, found &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;closure
  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;|
  = note: expected &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;type&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; `&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;`
             found &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;type&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; `[closure&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;@&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;main.rs:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;23&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;48&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;]`

error: aborting due to previous error

For more information about this error, try `rustc --explain &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;E0308&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;`.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;code&gt;[closure@main.rs:3:23: 3:48]&lt;&#x2F;code&gt; looks like a weird type... but let&#x27;s
just give it a shot and see what happens:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; msg: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;str &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Hi!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; say_hi: [closure&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;@&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;main.rs:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;23&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;48&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;] = |&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;msg&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, msg);
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;But the compiler shoots us down:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;error: expected one of `!`, `(`, `+`, `::`, `;`, `&amp;lt;`, or `]`, found `@`
 --&amp;gt; main.rs:3:25
  |
3 |     let say_hi: [closure@main.rs:3:23: 3:48] = |msg| println!(&amp;quot;{}&amp;quot;, msg);
  |         ------          ^ expected one of 7 possible tokens here
  |         |
  |         while parsing the type for `say_hi`

error: aborting due to previous error
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Oh well, that isn&#x27;t a valid type. What exactly is the compiler telling
us then?&lt;&#x2F;p&gt;
&lt;h2 id=&quot;anonymous-types&quot;&gt;Anonymous types&lt;&#x2F;h2&gt;
&lt;p&gt;The types of closures are anonymous in Rust. We cannot directly refer
to them at all. But this leaves us in a bit of a pickle. What if we
want to pass a closure into another function? For example, let&#x27;s try
out this program:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;say_message &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;msg&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;str&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, msg);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;call_with_hi&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(say_message);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;call_with_hi&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(say_message);
}

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;call_with_hi&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;F&amp;gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: F) {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Hi!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We&#x27;ve added a type annotation on the &lt;code&gt;msg&lt;&#x2F;code&gt; parameter in the
closure. These are generally optional in closures, unless type
inference fails. And with our current broken code, type inference is
definitely failing. We&#x27;re including it now to get better error
messages later.&lt;&#x2F;p&gt;
&lt;p&gt;We also now have a type parameter, called &lt;code&gt;F&lt;&#x2F;code&gt;, for the closure we&#x27;re
passing in. We don&#x27;t know anything about &lt;code&gt;F&lt;&#x2F;code&gt; right now, but we&#x27;re
going to just try using it in a function call manner. If we compile
this, we get:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;error[E0618]: expected function, found `F`
 --&amp;gt; main.rs:8:5
  |
7 | fn call_with_hi&amp;lt;F&amp;gt;(f: F) {
  |                    - `F` defined here
8 |     f(&amp;quot;Hi!&amp;quot;);
  |     ^^^^^^^^ not a function

error: aborting due to previous error

For more information about this error, try `rustc --explain E0618`.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;OK, fair enough: the compiler doesn&#x27;t know that &lt;code&gt;F&lt;&#x2F;code&gt; is a function. It&#x27;s time to finally introduce the magic that will make this compile: the &lt;code&gt;Fn&lt;&#x2F;code&gt; trait!&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;call_with_hi&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;F&amp;gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: F)
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;where&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; F: Fn(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;str&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) -&amp;gt; ()
{
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Hi!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We&#x27;ve now put a constraint on &lt;code&gt;F&lt;&#x2F;code&gt; that it must be a function, which
takes a single argument of type &lt;code&gt;&amp;amp;str&lt;&#x2F;code&gt;, and returns a unit
value. Actually, returning unit values is the default, so we can just
omit that bit:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;call_with_hi&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;F&amp;gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: F)
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;where&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; F: Fn(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;str&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
{
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Hi!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Another nifty thing about the &lt;code&gt;Fn&lt;&#x2F;code&gt; trait is that it doesn&#x27;t just apply
to closures. It works on regular ol&#x27; functions too:&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Exercise 4&lt;&#x2F;strong&gt; Rewrite &lt;code&gt;say_message&lt;&#x2F;code&gt; as a function &lt;em&gt;outside&lt;&#x2F;em&gt; of &lt;code&gt;main&lt;&#x2F;code&gt; and make the program above compile.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;This was a bit boring, since &lt;code&gt;say_message&lt;&#x2F;code&gt; isn&#x27;t actually a
closure. Let&#x27;s change that a bit.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; name = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::from(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Alice&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;say_something &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;msg&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;str&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, msg, name);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;call_with_hi&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(say_something);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;call_with_hi&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(say_something);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;call_with_bye&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(say_something);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;call_with_bye&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(say_something);
}

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;call_with_hi&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;F&amp;gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: F)
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;where&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; F: Fn(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;str&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
{
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Hi&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
}

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;call_with_bye&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;F&amp;gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: F)
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;where&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; F: Fn(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;str&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
{
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Bye&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;mutable-variables&quot;&gt;Mutable variables&lt;&#x2F;h2&gt;
&lt;p&gt;Remember the good old days of visitor counters on webpages? Let&#x27;s
recreate that beautiful experience!&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; count = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;

    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for _ in &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;..&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;6 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
        count += &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;You are visitor #&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, count);
    }
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;That works, but it&#x27;s so boring! Let&#x27;s make it more interesting with a
closure.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; count = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;visit &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;|| {
        count += &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;You are visitor #&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, count);
    };

    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for _ in &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;..&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;6 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;visit&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;();
    }
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The compiler disagrees:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;error[E0596]: cannot borrow immutable local variable `visit` as mutable
 --&amp;gt; main.rs:9:9
  |
3 |     let visit = || {
  |         ----- help: make this binding mutable: `mut visit`
...
9 |         visit();
  |         ^^^^^ cannot borrow mutably

error: aborting due to previous error

For more information about this error, try `rustc --explain E0596`.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Huh... what? Apparently calling a function counts as borrowing
it. Fine, that explains why we&#x27;re allowed to call it multiple
times. But now we need to borrow it &lt;em&gt;mutably&lt;&#x2F;em&gt; for some reason. How
come?&lt;&#x2F;p&gt;
&lt;p&gt;That reason is fairly simple: &lt;code&gt;visit&lt;&#x2F;code&gt; has captured and is mutating a
local variable, &lt;code&gt;count&lt;&#x2F;code&gt;. Therefore, any borrow of it is implicitly
mutably borrowing &lt;code&gt;count&lt;&#x2F;code&gt; as well. Logically, this makes sense. But
how about at the type level? How is the compiler tracking this
mutability? To see that, let&#x27;s extend this a bit further with a helper
function:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; count = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;visit &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;|| {
        count += &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;You are visitor #&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, count);
    };

    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;call_five_times&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(visit);
}

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;call_five_times&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;F&amp;gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: F)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;    F: Fn(),
{
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for _ in &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;..&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;6 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;();
    }
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We get the error message:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;error[E0525]: expected a closure that implements the `Fn` trait, but this closure only implements `FnMut`
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Nice! Rust has two different traits for functions: one which covers
functions that don&#x27;t mutate their environment (&lt;code&gt;Fn&lt;&#x2F;code&gt;), and one for
functions which do mutate their environment (&lt;code&gt;FnMut&lt;&#x2F;code&gt;). Let&#x27;s try
modifying our &lt;code&gt;where&lt;&#x2F;code&gt; to use &lt;code&gt;FnMut&lt;&#x2F;code&gt; instead. We get one more error
message:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;error[E0596]: cannot borrow immutable argument `f` as mutable
  --&amp;gt; main.rs:16:9
   |
11 | fn call_five_times&amp;lt;F&amp;gt;(f: F)
   |                       - help: make this binding mutable: `mut f`
...
16 |         f();
   |         ^ cannot borrow mutably

error: aborting due to previous error

For more information about this error, try `rustc --explain E0596`.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Calling this mutating function requires taking a mutable borrow of the
variable, and that requires defining the variable as mutable. Go ahead
and stick a &lt;code&gt;mut&lt;&#x2F;code&gt; in front of the &lt;code&gt;f: F&lt;&#x2F;code&gt;, and you&#x27;ll be golden.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;multiple-traits&quot;&gt;Multiple traits?&lt;&#x2F;h2&gt;
&lt;p&gt;Is this closure a &lt;code&gt;Fn&lt;&#x2F;code&gt; or a &lt;code&gt;FnMut&lt;&#x2F;code&gt;?&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;|| println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Hello World!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Well, it doesn&#x27;t modify any variables in the local scope, so
presumably it&#x27;s an &lt;code&gt;Fn&lt;&#x2F;code&gt;. Therefore, passing it to
&lt;code&gt;call_five_times&lt;&#x2F;code&gt;—which expects a &lt;code&gt;FnMut&lt;&#x2F;code&gt;—should fail,
right? Not so fast, it works just fine! Go ahead and add this line to
the program above and prove it to yourself:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;call_five_times&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Hello World!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;));
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Every value which is a &lt;code&gt;Fn&lt;&#x2F;code&gt; is &lt;em&gt;automatically&lt;&#x2F;em&gt; an &lt;code&gt;FnMut&lt;&#x2F;code&gt;. This is
similar to what happens with a function parameter: if you have a
mutable reference, you can automatically use it as an immutable
reference, since the guarantees of a mutable reference are
stronger. Similarly, if we&#x27;re using a function in such a way that it&#x27;s
safe even if the function is mutable (&lt;code&gt;FnMut&lt;&#x2F;code&gt;), it&#x27;s certainly safe to
do the same thing with an immutable function (&lt;code&gt;Fn&lt;&#x2F;code&gt;).&lt;&#x2F;p&gt;
&lt;p&gt;Does this sound a bit like subtyping? Good, it should :)&lt;&#x2F;p&gt;
&lt;h2 id=&quot;the-rule-of-three&quot;&gt;The rule of three?&lt;&#x2F;h2&gt;
&lt;p&gt;If you&#x27;ve noticed, we now have two different types of functions, in a
lesson entitled &amp;quot;the rule of three.&amp;quot; What could possibly be coming
next? We&#x27;ve seen functions that can be called multiple times in an
immutable context, kind of like immutable references. We&#x27;ve seen
functions that can be called multiple times in a mutable context, kind
of like mutable references. That just leaves one thing... call by
value&#x2F;move semantics!&lt;&#x2F;p&gt;
&lt;p&gt;We&#x27;re going to define a closure that moves a local variable
around. We&#x27;re going to go back to use a &lt;code&gt;String&lt;&#x2F;code&gt; instead of a
&lt;code&gt;u32&lt;&#x2F;code&gt;, to avoid the fact that a &lt;code&gt;u32&lt;&#x2F;code&gt; is &lt;code&gt;Copy&lt;&#x2F;code&gt;able. And we&#x27;re going
to use a weird bit of magic in the middle to force things to be moved
instead of being treated as references. We&#x27;ll go into gory detail on
that trick later, and see alternatives.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; name = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::from(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Alice&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);

    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;welcome &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;|| {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; name = name; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;&#x2F;&#x2F; here&amp;#39;s the magic
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Welcome, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, name);
    };

    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;welcome&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;();
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Alright, &lt;code&gt;name&lt;&#x2F;code&gt; is moved into the &lt;code&gt;welcome&lt;&#x2F;code&gt; closure. This is forced
with the &lt;code&gt;let name = name;&lt;&#x2F;code&gt; bit. Still not 100% convinced that &lt;code&gt;name&lt;&#x2F;code&gt;
was actually moved in? Watch this:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; name1 = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::from(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Alice&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);

    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;welcome &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;|| {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; name2 = name1;
        name2 += &lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt; and Bob&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Welcome, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, name2);
    };

    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;welcome&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;();
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;code&gt;name1&lt;&#x2F;code&gt; is defined as &lt;em&gt;immutable&lt;&#x2F;em&gt;. But &lt;code&gt;name2&lt;&#x2F;code&gt; is mutable, and we do
in fact successfully mutate it. This can only happen if we pass by
value instead of by reference. Want further proof? Try to use &lt;code&gt;name1&lt;&#x2F;code&gt;
again after we&#x27;ve defined &lt;code&gt;welcome&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;the-third-function-trait&quot;&gt;The third function trait&lt;&#x2F;h2&gt;
&lt;p&gt;Let&#x27;s complete our rule of three. Remember our &lt;code&gt;call_five_times&lt;&#x2F;code&gt;?
Let&#x27;s use it on &lt;code&gt;welcome&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; name = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::from(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Alice&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);

    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;welcome &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;|| {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; name = name;
        name += &lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt; and Bob&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Welcome, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, name);
    };

    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;call_five_times&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(welcome);
}

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;call_five_times&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;F&amp;gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: F)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;    F: Fn(),
{
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for _ in &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;..&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;6 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;();
    }
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And we get a brand new error message, this time referencing &lt;code&gt;FnOnce&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;error[E0525]: expected a closure that implements the `Fn` trait, but this closure only implements `FnOnce`
  --&amp;gt; main.rs:4:19
   |
4  |     let welcome = || {
   |                   ^^ this closure implements `FnOnce`, not `Fn`
5  |         let mut name = name;
   |                        ---- closure is `FnOnce` because it moves the variable `name` out of its environment
...
10 |     call_five_times(welcome);
   |     --------------- the requirement to implement `Fn` derives from here

error: aborting due to previous error

For more information about this error, try `rustc --explain E0525`.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Replacing &lt;code&gt;Fn()&lt;&#x2F;code&gt; with &lt;code&gt;FnOnce()&lt;&#x2F;code&gt; should fix the compilation, right? Wrong!&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;error[E0382]: use of moved value: `f`
  --&amp;gt; main.rs:18:9
   |
18 |         f();
   |         ^ value moved here in previous iteration of loop
   |
   = note: move occurs because `f` has type `F`, which does not implement the `Copy` trait

error: aborting due to previous error

For more information about this error, try `rustc --explain E0382`.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Our loop ends up calling &lt;code&gt;f&lt;&#x2F;code&gt; multiple times. But each time we call
&lt;code&gt;f&lt;&#x2F;code&gt;, we&#x27;re moving the value. Therefore, the function can &lt;em&gt;only be
called once&lt;&#x2F;em&gt;. Maybe that&#x27;s why they named it &lt;code&gt;FnOnce&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s rewrite this to have a helper function that only calls things
once:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; name = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::from(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Alice&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);

    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;welcome &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;|| {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; name = name;
        name += &lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt; and Bob&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Welcome, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, name);
    };

    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;call_once&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(welcome);
}

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;call_once&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;F&amp;gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: F)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;    F: FnOnce(),
{
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;();
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;That works just fine. Hurrah!&lt;&#x2F;p&gt;
&lt;h2 id=&quot;further-function-subtyping&quot;&gt;Further function subtyping&lt;&#x2F;h2&gt;
&lt;p&gt;Previously, we said that every &lt;code&gt;Fn&lt;&#x2F;code&gt; is also an &lt;code&gt;FnMut&lt;&#x2F;code&gt;, since anywhere
you can safely call a mutable function, you can also call an immutable
function. It turns out that every &lt;code&gt;Fn&lt;&#x2F;code&gt; and every &lt;code&gt;FnMut&lt;&#x2F;code&gt; are also
&lt;code&gt;FnOnce&lt;&#x2F;code&gt;s, because any context you can guarantee the function will
only be called once is safe for running functions with mutable or
immutable environments.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;the-move-keyword&quot;&gt;The move keyword&lt;&#x2F;h2&gt;
&lt;p&gt;There&#x27;s a subtle point we&#x27;re about to get into, which I &lt;a href=&quot;https:&#x2F;&#x2F;stackoverflow.com&#x2F;q&#x2F;53029622&#x2F;369198&quot;&gt;didn&#x27;t
understand till I wrote this
lesson&lt;&#x2F;a&gt; (thanks to Sven
Marnach for the explanation there). The &lt;a href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;rust-by-example&#x2F;fn&#x2F;closures.html&quot;&gt;Rust by Example section on closures&lt;&#x2F;a&gt;
was the best resource for helping it all click for me. I&#x27;ll do my best
here explaining it myself.&lt;&#x2F;p&gt;
&lt;p&gt;Functions accept parameters explicitly, complete with type
signatures. You&#x27;re able to explicitly state whether a parameter is
pass by value, mutable reference, or immutable reference. Then, when
you use it, you&#x27;re able to choose any of the weaker forms
available. For example, if you pass a parameter by mutable reference,
you can later use it by immutable reference. However, you &lt;em&gt;cannot&lt;&#x2F;em&gt; use
it by value:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;pass_by_value&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;_x&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: String) {}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;pass_by_ref&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;_x&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;String) {}

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;pass_by_mut_ref&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;x&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; String) {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;pass_by_ref&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(x); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;&#x2F;&#x2F; that&amp;#39;s fine
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;pass_by_value&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(*x); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;&#x2F;&#x2F; that&amp;#39;s a paddlin&amp;#39;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Closures accept parameters, but they make the type annotations
optional. If you omit them, they are implicit. In addition, closures
allow you to capture variables. These never take a type annotation;
they are &lt;em&gt;always&lt;&#x2F;em&gt; implicit. Nonetheless, there needs to be some
concept of how these values were captured, just like we need to know
how parameters are passed into a function.&lt;&#x2F;p&gt;
&lt;p&gt;How a value is captured will imply the same set of borrow rules we&#x27;re
used to in Rust, in particular:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;If by reference, then other references can live concurrently with
the closure&lt;&#x2F;li&gt;
&lt;li&gt;If by mutable reference, then as long as the closure is alive, no
other references to the values can exist. However, once the closure
is dropped, other references can exist again.&lt;&#x2F;li&gt;
&lt;li&gt;If by value, then the value cannot be used by anything ever
again. (This automatically implies that the closure &lt;em&gt;owns&lt;&#x2F;em&gt; the
value.)&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;However, there&#x27;s an important and (dare I repeat myself) subtle
distinction between closures and functions:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;Closures can own data, functions cannot&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Sure, if you pass by value to a function, the function call takes
ownership of the data during execution. But closures are different:
the closure &lt;em&gt;itself&lt;&#x2F;em&gt; can own data, and use it while it is being
called. Let&#x27;s demonstrate:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;&#x2F;&#x2F; owned by main
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; name_outer = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::from(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Alice&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);

    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;say_hi &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;|| {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;&#x2F;&#x2F; force a move, again, we&amp;#39;ll get smarter in a second
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; name_inner = name_outer;
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Hello, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, name_inner);
    };

    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;&#x2F;&#x2F; main no longer owns name_outer, try this:
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Using name from main: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, name_outer); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;&#x2F;&#x2F; error!

    &#x2F;&#x2F; but name_inner lives on, in say_hi!
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;say_hi&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;&#x2F;&#x2F; success
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Try as you might, you could not achieve the same thing with a plain
old function, you&#x27;d need to keep &lt;code&gt;name_outer&lt;&#x2F;code&gt; alive separately and
then pass it in.&lt;&#x2F;p&gt;
&lt;p&gt;Alright, let&#x27;s get to that smarter way to force a move. In the closure
above, we have &lt;code&gt;let name_inner = name_outer;&lt;&#x2F;code&gt;. This forces the closure
to use &lt;code&gt;name_outer&lt;&#x2F;code&gt; by value. Since we use by value, we can only call
this closure once, since it fully consumes &lt;code&gt;name_outer&lt;&#x2F;code&gt; on the first
call. (Go ahead and try adding a second &lt;code&gt;say_hi()&lt;&#x2F;code&gt; call.) But in
reality, we&#x27;re only using the name by immutable reference inside the
closure. We &lt;em&gt;should&lt;&#x2F;em&gt; be able to call it multiple times. If we skip the
forced use by value, we can use by reference, leaving the &lt;code&gt;name_outer&lt;&#x2F;code&gt;
in the original scope:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;&#x2F;&#x2F; owned by main
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; name_outer = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::from(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Alice&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);

    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;say_hi &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;|| {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;&#x2F;&#x2F; use by reference
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; name_inner = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;name_outer;
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Hello, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, name_inner);
    };

    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;&#x2F;&#x2F; main still owns name_outer, this is fine
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Using name from main: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, name_outer); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;&#x2F;&#x2F; success

    &#x2F;&#x2F; but name_inner lives on, in say_hi!
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;say_hi&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;&#x2F;&#x2F; success
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;say_hi&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;&#x2F;&#x2F; success
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;However, if we change things around a bit, so that &lt;code&gt;name_outer&lt;&#x2F;code&gt; goes
out of scope before &lt;code&gt;say_hi&lt;&#x2F;code&gt;, everything falls apart!&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; say_hi = { &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;&#x2F;&#x2F; forcing the creation of a smaller scope
        &#x2F;&#x2F; owned by the smaller scope
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; name_outer = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::from(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Alice&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);

        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;&#x2F;&#x2F; doesn&amp;#39;t work, closure outlives captured values
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;|| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;&#x2F;&#x2F; use by reference
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; name_inner = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;name_outer;
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Hello, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, name_inner);
        }
    };

    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;&#x2F;&#x2F; syntactically invalid, name_outer isn&amp;#39;t in this scope
    &#x2F;&#x2F;println!(&amp;quot;Using name from main: {}&amp;quot;, name_outer); &#x2F;&#x2F; error!

    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;say_hi&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;();
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;say_hi&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;();
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;What we need is some way to say: I&#x27;d like the closure to own the
values it captures, but I don&#x27;t want to have to force a use by value
to do it. That will allow a closure to outlive the original scope of
the value, but still allow a closure to be called multiple times. And
to do that, we introduce the &lt;code&gt;move&lt;&#x2F;code&gt; keyword:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; say_hi = { &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;&#x2F;&#x2F; forcing the creation of a smaller scope
        &#x2F;&#x2F; owned by the smaller scope
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; name_outer = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::from(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Alice&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);

        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;&#x2F;&#x2F; now it works!
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;move &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;|| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;&#x2F;&#x2F; use by reference
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; name_inner = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;name_outer;
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Hello, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, name_inner);
        }
    };

    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;&#x2F;&#x2F; syntactically invalid, name_outer isn&amp;#39;t in this scope
    &#x2F;&#x2F;println!(&amp;quot;Using name from main: {}&amp;quot;, name_outer); &#x2F;&#x2F; error!

    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;say_hi&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;();
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;say_hi&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;();
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The ownership of &lt;code&gt;name_outer&lt;&#x2F;code&gt; passes from the original scope to the
closure itself. We still only use it by reference, and therefore we
can call it multiple times. Hurrah!&lt;&#x2F;p&gt;
&lt;p&gt;One final bit here. Using the &lt;code&gt;move&lt;&#x2F;code&gt; keyword like this moves all
captured variables into the closure, and therefore they cannot be used
after the closure. For example, this will fail to compile:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; name = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::from(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Alice&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;_ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;move &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;|| println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Hello, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, name);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Using name from main: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, name); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;&#x2F;&#x2F; error!
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;reluctant-rust&quot;&gt;Reluctant Rust&lt;&#x2F;h2&gt;
&lt;p&gt;Alright, one final point before we sum things up and dive into
examples. The type of capture is implicit in a closure. How does Rust
decide whether to capture by value, mutable reference, or immutable
reference. I like to think of Rust as being reluctant here: it strives
to capture the weakest way possible. To paraphrase the Rust by Example
book:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;Closures will preferentially capture by immutable reference, then by
mutable reference, and only then by value.&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;p&gt;In our previous examples with &lt;code&gt;let name_inner = name_outer;&lt;&#x2F;code&gt;, we
forced Rust to capture by value. However, it doesn&#x27;t like doing that,
and will instead capture by reference (mutable or immutable) if it can
get away with that. It does this based on the strongest kind of usage
for that value. That is:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;If any part of the closure uses a variable by value, it must be
captured by value.&lt;&#x2F;li&gt;
&lt;li&gt;Otherwise, if any part of the closure uses a variable by mutable
reference, it must be captured by mutable reference.&lt;&#x2F;li&gt;
&lt;li&gt;Otherwise, if any part of the closure uses a variable by immutable
reference, it must be captured by immutable reference.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;It does this reluctant capturing &lt;em&gt;even if it causes the program to
fail to compile&lt;&#x2F;em&gt;. Capturing by reference instead of value can cause
lifetime issues, as we&#x27;ve seen previously. However, Rust does not look
at the full context of the usage of the closure to determine how to
capture, it only looks at the body of the closure itself.&lt;&#x2F;p&gt;
&lt;p&gt;But, since there are many legitimate cases where we want to force a
capture by value to solve lifetime issues, we have the &lt;code&gt;move&lt;&#x2F;code&gt; keyword
to force the issue.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;Side note&lt;&#x2F;strong&gt; It may be a little annoying at times that Rust doesn&#x27;t
just look at your program as a whole and guess that you want that
&lt;code&gt;move&lt;&#x2F;code&gt; added. However, I think it&#x27;s a great decision in the language:
that kind of &amp;quot;do what I mean&amp;quot; logic is fragile and often times
surprising.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;recap-ownership-capture-and-usage&quot;&gt;Recap: ownership, capture, and usage&lt;&#x2F;h2&gt;
&lt;p&gt;To recap the salient points:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Within a closure, a variable can be used by value, mutable
reference, or immutable reference&lt;&#x2F;li&gt;
&lt;li&gt;In addition, all variables captured by a closure can be captured by
value, by mutable reference, or by immutable reference&lt;&#x2F;li&gt;
&lt;li&gt;We cannot use a variable in a stronger way than it was captured. If
it was captured by mutable reference, it can be used by immutable
reference, but not by value.&lt;&#x2F;li&gt;
&lt;li&gt;To solve lifetime issues, we can force a closure to capture by value
with the &lt;code&gt;move&lt;&#x2F;code&gt; keyword.&lt;&#x2F;li&gt;
&lt;li&gt;Short of the &lt;code&gt;move&lt;&#x2F;code&gt; keyword, Rust will be reluctant, and capture in
the weakest way allowed by the body of the closure.&lt;&#x2F;li&gt;
&lt;li&gt;Regarding the traits of closures:
&lt;ul&gt;
&lt;li&gt;If a closure uses anything by value, then the closure is a
&lt;code&gt;FnOnce&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;Otherwise, if a closure uses anything by mutable reference, then
the closure is a &lt;code&gt;FnMut&lt;&#x2F;code&gt;, which automatically implies &lt;code&gt;FnOnce&lt;&#x2F;code&gt;
as well&lt;&#x2F;li&gt;
&lt;li&gt;Otherwise, a closure is a &lt;code&gt;Fn&lt;&#x2F;code&gt;, which automatically implies both
&lt;code&gt;FnMut&lt;&#x2F;code&gt; and &lt;code&gt;FnOnce&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;I consider the points above complicated enough that I&#x27;m included a
number of further examples to help hammer the points home. These are
inspired heavily by the Rust by Example examples.&lt;&#x2F;p&gt;
&lt;p&gt;For all of the examples below, I&#x27;m going to assume the presence of the
following three helper functions in the source:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;call_fn&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;F&amp;gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: F) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;where&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; F: Fn() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()
}

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;call_fn_mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;F&amp;gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: F) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;where&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; F: FnMut() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()
}

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;call_fn_once&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;F&amp;gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: F) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;where&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; F: FnOnce() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;examples&quot;&gt;Examples&lt;&#x2F;h3&gt;
&lt;p&gt;Consider this &lt;code&gt;main&lt;&#x2F;code&gt; function:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; name = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::from(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Alice&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;say_hi &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;|| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Hello, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, name);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;call_fn&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(say_hi);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;call_fn_mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(say_hi);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;call_fn_once&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(say_hi);
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;code&gt;name&lt;&#x2F;code&gt; lives longer than &lt;code&gt;say_hi&lt;&#x2F;code&gt;, and therefore there&#x27;s no problem
with the closure keeping an immutable reference to &lt;code&gt;name&lt;&#x2F;code&gt;. Since it
only has immutable references to the environment and consumes no
values, &lt;code&gt;say_hi&lt;&#x2F;code&gt; is a &lt;code&gt;Fn&lt;&#x2F;code&gt;, &lt;code&gt;FnMut&lt;&#x2F;code&gt;, and &lt;code&gt;FnOnce&lt;&#x2F;code&gt;, and the code above
compiles.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;&#x2F;&#x2F; bad!
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; say_hi = {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; name = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::from(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Alice&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
        || &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Hello, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, name)
    };
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;By contrast, this example won&#x27;t compile. &lt;code&gt;name&lt;&#x2F;code&gt; will go out of scope
once we leave the curly braces. However, our closure is capturing it
by reference, and so the reference outlives value. We could do our
trick from before of forcing it to capture by value:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; say_hi = {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; name = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::from(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Alice&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
        || {
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; name = name;
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Hello, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, name)
        }
    };
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;&#x2F;&#x2F;call_fn(say_hi);
    &#x2F;&#x2F;call_fn_mut(say_hi);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;call_fn_once&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(say_hi);
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;But this only implements a &lt;code&gt;FnOnce&lt;&#x2F;code&gt;, since the value is captured and
consumed, preventing it from being run again. There&#x27;s a better way!
Instead, we can force the closure to take ownership of &lt;code&gt;name&lt;&#x2F;code&gt;, but
still capture by reference:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; say_hi = {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; name = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::from(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Alice&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;move &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;|| println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Hello, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, name)
    };
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;call_fn&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;say_hi);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;call_fn_mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;say_hi);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;call_fn_once&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;say_hi);
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now we&#x27;re back to having a &lt;code&gt;Fn&lt;&#x2F;code&gt;, &lt;code&gt;FnMut&lt;&#x2F;code&gt;, and &lt;code&gt;FnOnce&lt;&#x2F;code&gt;! To avoid the
&lt;code&gt;say_hi&lt;&#x2F;code&gt; value itself from being moved with each call, we now pass a
reference to the &lt;code&gt;call_fn&lt;&#x2F;code&gt; functions. I believe (though am not 100%
certain) that this wasn&#x27;t necessary in the first example since, above,
there was no captured environment, and therefore the closure could be
&lt;code&gt;Copy&lt;&#x2F;code&gt;ed. This closure, with a captured environment, cannot be
&lt;code&gt;Copy&lt;&#x2F;code&gt;ed`.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; say_hi = {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; name = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::from(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Alice&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
        || std::mem::drop(name)
    };
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;&#x2F;&#x2F;call_fn(say_hi);
    &#x2F;&#x2F;call_fn_mut(say_hi);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;call_fn_once&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(say_hi);
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This example uses the &lt;code&gt;drop&lt;&#x2F;code&gt; function to consume &lt;code&gt;name&lt;&#x2F;code&gt;. Since we use
by value, we must capture by value, and therefore must take ownership
of the value. As a result, sticking &lt;code&gt;move&lt;&#x2F;code&gt; at the front of the closure
is unnecessary, though it will do no harm.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; say_hi = {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; name = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::from(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Alice&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;move &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;|| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
            name += &lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt; and Bob&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Hello, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, name);
        }
    };
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;&#x2F;&#x2F;call_fn(say_hi);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;call_fn_mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; say_hi);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;call_fn_once&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; say_hi);
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Using the &lt;code&gt;+=&lt;&#x2F;code&gt; operator on a &lt;code&gt;String&lt;&#x2F;code&gt; requires a mutable reference, so
we&#x27;re out of the territory of immutable reference capturing. Rust
will fall back to capturing via mutable reference. That requires that
the &lt;code&gt;name&lt;&#x2F;code&gt; also be declared mutable. And since &lt;code&gt;name&lt;&#x2F;code&gt; will go out of
scope before the closure, we need to &lt;code&gt;move&lt;&#x2F;code&gt; ownership to the
closure. And since calling &lt;code&gt;say_hi&lt;&#x2F;code&gt; will mutate data, we need to put a
&lt;code&gt;mut&lt;&#x2F;code&gt; on its declaration too.&lt;&#x2F;p&gt;
&lt;p&gt;When we pass &lt;code&gt;say_hi&lt;&#x2F;code&gt; to the call functions, we need to use &lt;code&gt;&amp;amp;mut&lt;&#x2F;code&gt; to
ensure (1) the value isn&#x27;t moved, and (2) the value can be
mutated. Also, &lt;code&gt;call_fn&lt;&#x2F;code&gt; is invalid here, since our closure is &lt;code&gt;FnMut&lt;&#x2F;code&gt;
and &lt;code&gt;FnOnce&lt;&#x2F;code&gt;, but &lt;em&gt;not&lt;&#x2F;em&gt; &lt;code&gt;Fn&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;Challenge&lt;&#x2F;strong&gt; What will the output of this program be? How many times
do we add the string &lt;code&gt;&amp;quot; and Bob&amp;quot;&lt;&#x2F;code&gt; to &lt;code&gt;name&lt;&#x2F;code&gt;?&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; name = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::from(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Alice&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;say_hi &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;|| {
        name += &lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt; and Bob&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Hello, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, name);
    };
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;&#x2F;&#x2F;call_fn(say_hi);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;call_fn_mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; say_hi);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;call_fn_once&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; say_hi);
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We can also avoid the capture by letting the &lt;code&gt;name&lt;&#x2F;code&gt; live longer than the closure.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;&#x2F;&#x2F; bad!
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; name = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::from(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Alice&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;say_hi &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;|| {
        name += &lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt; and Bob&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Hello, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, name);
    };
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;&#x2F;&#x2F;call_fn(say_hi);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;call_fn_mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; say_hi);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;call_fn_once&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; say_hi);

    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;And now name is: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, name);
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Adding the &lt;code&gt;println!&lt;&#x2F;code&gt; at the end, which references &lt;code&gt;name&lt;&#x2F;code&gt;, is invalid,
since &lt;code&gt;say_hi&lt;&#x2F;code&gt; is still in scope. This is due to &lt;em&gt;lexical
lifetimes&lt;&#x2F;em&gt;. You can turn on the (at time of writing) experimental
feature non-lexical lifetimes by adding &lt;code&gt;#![feature(nll)]&lt;&#x2F;code&gt; to the top
of your source code. Or, you can explicitly use braces to denote the
scope of the closure:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; name = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::from(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Alice&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
    {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;say_hi &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;|| {
            name += &lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt; and Bob&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Hello, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, name);
        };
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;&#x2F;&#x2F;call_fn(say_hi);
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;call_fn_mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; say_hi);
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;call_fn_once&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; say_hi);
    }

    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;And now name is: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, name);
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You can also (perhaps somewhat obviously) use a value in multiple
different ways:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; name = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::from(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Alice&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;say_hi &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;|| {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Hello, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, name); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;&#x2F;&#x2F; use by ref
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;        name += &lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt; and Bob&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;&#x2F;&#x2F; use by mut ref
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;std::mem::drop(name); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;&#x2F;&#x2F; use by value
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;};
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;&#x2F;&#x2F;call_fn(say_hi);
    &#x2F;&#x2F;call_fn_mut(say_hi);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;call_fn_once&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(say_hi);
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In these cases, the most powerful use determines the kind of capture
we need. Since we used by value above, we must also capture by value,
and therefore must take ownership.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;which-trait-to-use&quot;&gt;Which trait to use?&lt;&#x2F;h2&gt;
&lt;p&gt;It may be intimidating to try and think through which of these three
traits you need. You can usually punt on this and let the compiler
yell at you. To quote &lt;a href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;book&#x2F;2018-edition&#x2F;ch13-01-closures.html#capturing-the-environment-with-closures&quot;&gt;the Rust
book&lt;&#x2F;a&gt;:&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;Most of the time when specifying one of the &lt;code&gt;Fn&lt;&#x2F;code&gt; trait bounds, you
can start with &lt;code&gt;Fn&lt;&#x2F;code&gt; and the compiler will tell you if you need
&lt;code&gt;FnMut&lt;&#x2F;code&gt; or &lt;code&gt;FnOnce&lt;&#x2F;code&gt; based on what happens in the closure body.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;I&#x27;d give a slightly different piece of advice, following the dictum of
&amp;quot;be lenient in what you accept.&amp;quot; When receiving functions as
arguments, the most lenient thing to start with is a &lt;code&gt;FnOnce&lt;&#x2F;code&gt;. If your
usage turns out to be more restrictive, then listen to the
compiler.&lt;&#x2F;p&gt;
&lt;p&gt;For more information on closures as output parameters, see &lt;a href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;rust-by-example&#x2F;fn&#x2F;closures&#x2F;output_parameters.html&quot;&gt;Rust by Example&#x27;s
chapter&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;summary-of-the-rule-of-three-for-closures&quot;&gt;Summary of the rule of three for closures&lt;&#x2F;h2&gt;
&lt;p&gt;Both functions and closures are annotated using the &lt;code&gt;Fn&lt;&#x2F;code&gt; family of
trait bounds. These form a subtyping relationship, where every &lt;code&gt;Fn&lt;&#x2F;code&gt; is
also an &lt;code&gt;FnMut&lt;&#x2F;code&gt;, and every &lt;code&gt;FnMut&lt;&#x2F;code&gt; is also an &lt;code&gt;FnOnce&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;FnOnce&lt;&#x2F;code&gt; works like pass by value&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;FnMut&lt;&#x2F;code&gt; works like pass by mutable reference&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;Fn&lt;&#x2F;code&gt; works like pass by immutable reference&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;How these captured variables are used by the closure determines which
of these three it is. Since functions, by definition, never capture
local variables, they are always &lt;code&gt;Fn&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;exercise-5&quot;&gt;Exercise 5&lt;&#x2F;h2&gt;
&lt;p&gt;Putting together what we&#x27;ve learned about iterators and closures,
modify line 5 below (the one starting with &lt;code&gt;for i in&lt;&#x2F;code&gt;) so that the
program prints the numbers &lt;code&gt;2,4,6,..,20&lt;&#x2F;code&gt; twice.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; nums: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Vec&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;gt; = (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;..&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;11&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;collect&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;();

    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for _ in &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;..&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;3 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; i &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;in&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; nums.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;map&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;unimplemented!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()) {
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, i);
        }
    }
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;guis-and-callbacks&quot;&gt;GUIs and callbacks&lt;&#x2F;h2&gt;
&lt;p&gt;What better way to tie this all off than by writing a GUI and some
callbacks? I&#x27;m going to use GTK+ and the wonderful
&lt;a href=&quot;http:&#x2F;&#x2F;gtk-rs.org&#x2F;&quot;&gt;gtk-rs&lt;&#x2F;a&gt; set of crates. Our goal ultimately is to
create a GUI with a single button on it. When that button is clicked,
a message will be written to a file that says &amp;quot;I was clicked.&amp;quot;&lt;&#x2F;p&gt;
&lt;p&gt;For this example, you&#x27;ll definitely want to use a cargo project. Go
ahead and run:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;$ cargo new clicky
$ cd clicky
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now add gtk as a dependency. Within the &lt;code&gt;[dependencies]&lt;&#x2F;code&gt; section of
the &lt;code&gt;Cargo.toml&lt;&#x2F;code&gt;, add the line:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-toml&quot; data-lang=&quot;toml&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;gtk &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;0.5&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And now we&#x27;re going to rip off the sample code from gtk-rs&#x27;s
website. Put this in your &lt;code&gt;main.rs&lt;&#x2F;code&gt; (bonus points if you type it
yourself instead of copy-pasting):&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extern crate&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; gtk;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;gtk::prelude::*;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;gtk::{Button, Window, WindowType};

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;gtk::init().&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;is_err&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Failed to initialize GTK.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;return&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
    }

    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; window = Window::new(WindowType::Toplevel);
    window.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;set_title&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;First GTK+ Program&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
    window.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;set_default_size&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;350&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;70&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; button = Button::new_with_label(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Click me!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
    window.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;add&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;button);
    window.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;show_all&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;();

    window.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;connect_delete_event&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|_, _| {
        gtk::main_quit();
        Inhibit(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;false&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
    });

    button.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;connect_clicked&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|_| {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Clicked!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
    });

    gtk::main();
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Assuming you&#x27;ve got all of your system libraries set up correctly,
running &lt;code&gt;cargo run&lt;&#x2F;code&gt; should get you a nice, simple GUI.&lt;&#x2F;p&gt;
&lt;p&gt;If you do have trouble installing the crates, check out &lt;a href=&quot;http:&#x2F;&#x2F;gtk-rs.org&#x2F;docs-src&#x2F;requirements.html&quot;&gt;gtk-rs&#x27;s
requirements page&lt;&#x2F;a&gt;
first.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;replacing-the-callback&quot;&gt;Replacing the callback&lt;&#x2F;h2&gt;
&lt;p&gt;You may have noticed that sample code already includes a callback,
which prints &lt;code&gt;Clicked!&lt;&#x2F;code&gt; to stdout each time the button is
clicked. That certainly makes our life a little bit easier. Now,
inside of that callback, we need to:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Open up a file&lt;&#x2F;li&gt;
&lt;li&gt;Write some data to the file&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;We&#x27;re going to take a first stab at this without doing any error
handling. Instead, we&#x27;ll use &lt;code&gt;.unwrap()&lt;&#x2F;code&gt; on all of the &lt;code&gt;Result&lt;&#x2F;code&gt;
values, causing our program to &lt;code&gt;panic!&lt;&#x2F;code&gt; if something goes wrong. We&#x27;ll
clean that up a bit later.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;std&#x2F;fs&#x2F;struct.File.html&quot;&gt;Searching the standard library for
file&lt;&#x2F;a&gt; quickly finds
&lt;a href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;std&#x2F;fs&#x2F;struct.File.html&quot;&gt;&lt;code&gt;std::fs::File&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;,
which seems promising. It also seems like the &lt;code&gt;create&lt;&#x2F;code&gt; function will
be the easiest way to get started. We&#x27;ll write to &lt;code&gt;mylog.txt&lt;&#x2F;code&gt;. The example at the top of that page shows &lt;code&gt;write_all&lt;&#x2F;code&gt; (thanks Rust for awesome API docs!). First, try out this bit of code:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; file = std::fs::File::create(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;mylog.txt&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
file.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;write_all&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;b&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;I was clicked.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#dc322f;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;After addressing exercise 6 below, you&#x27;ll see this error message:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;error[E0599]: no method named `write_all` found for type `std::fs::File` in the current scope
  --&amp;gt; src&#x2F;main.rs:27:14
   |
27 |         file.write_all(b&amp;quot;I was clicked.\n&amp;quot;);
   |              ^^^^^^^^^
   |
   = help: items from traits can only be used if the trait is in scope
help: the following trait is implemented but not in scope, perhaps add a `use` for it:
   |
3  | use std::io::Write;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Oh, that&#x27;s something new. In order to use items from a trait, the
trait has to be in scope. Easy enough, we can just add &lt;code&gt;use std::io::Write;&lt;&#x2F;code&gt; to our closure:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;std::io::Write;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; file = std::fs::File::create(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;mylog.txt&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
file.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;write_all&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;b&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;I was clicked.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#dc322f;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;strong&gt;Exercise 6&lt;&#x2F;strong&gt; If you&#x27;re following along with the code like you should
be, you probably got a different error message above, and the code
I&#x27;ve provided here doesn&#x27;t actually fix everything. You need to add an
extra method call to convert a &lt;code&gt;Result&amp;lt;File, Error&amp;gt;&lt;&#x2F;code&gt; into a
&lt;code&gt;File&lt;&#x2F;code&gt;. Hint: I mentioned it above.&lt;&#x2F;p&gt;
&lt;p&gt;Go ahead and run this program (via &lt;code&gt;cargo run&lt;&#x2F;code&gt;), click the button a
few times, and close the window. Then look at the contents of
&lt;code&gt;mylog.txt&lt;&#x2F;code&gt;. No matter how many times you clicked, you&#x27;ll only get one
line of output.&lt;&#x2F;p&gt;
&lt;p&gt;The problem is that each time the callback is called, we call &lt;code&gt;create&lt;&#x2F;code&gt;
from &lt;code&gt;File&lt;&#x2F;code&gt;, which overwrites the old file. One approach here would be
to create an appending file handle (awesome bonus exercise for anyone
who wants to take it on). We&#x27;re going to take another approach.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;share-the-file&quot;&gt;Share the file&lt;&#x2F;h2&gt;
&lt;p&gt;Let&#x27;s move our &lt;code&gt;create&lt;&#x2F;code&gt; call to &lt;em&gt;outside&lt;&#x2F;em&gt; of the closure
definition. We&#x27;ll open the file in the &lt;code&gt;main&lt;&#x2F;code&gt; function itself, the
closure can capture a mutable reference to the &lt;code&gt;file&lt;&#x2F;code&gt;, and all will be
well in the world.&lt;&#x2F;p&gt;
&lt;p&gt;Unfortunately, the compiler really dislikes this:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;error[E0596]: cannot borrow `file` as mutable, as it is a captured variable in a `Fn` closure
  --&amp;gt; src&#x2F;main.rs:28:9
   |
28 |         file.write_all(b&amp;quot;I was clicked.\n&amp;quot;);
   |         ^^^^ cannot borrow as mutable
   |
help: consider changing this to accept closures that implement `FnMut`
  --&amp;gt; src&#x2F;main.rs:26:28
   |
26 |       button.connect_clicked(|_| {
   |  ____________________________^
27 | |         use std::io::Write;
28 | |         file.write_all(b&amp;quot;I was clicked.\n&amp;quot;);
29 | |     });
   | |_____^

error[E0597]: `file` does not live long enough
  --&amp;gt; src&#x2F;main.rs:28:9
   |
26 |     button.connect_clicked(|_| {
   |                            --- value captured here
27 |         use std::io::Write;
28 |         file.write_all(b&amp;quot;I was clicked.\n&amp;quot;);
   |         ^^^^ borrowed value does not live long enough
...
32 | }
   | - `file` dropped here while still borrowed
   |
   = note: borrowed value must be valid for the static lifetime...

error: aborting due to 2 previous errors

Some errors occurred: E0596, E0597.
For more information about an error, try `rustc --explain E0596`.
error: Could not compile `clicky`.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Or more briefly:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;cannot borrow `file` as mutable, as it is a captured variable in a `Fn` closure
    help: consider changing this to accept closures that implement `FnMut`
`file` does not live long enough
    note: borrowed value must be valid for the static lifetime...
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We can understand both of these by looking at the signature for &lt;a href=&quot;http:&#x2F;&#x2F;gtk-rs.org&#x2F;docs&#x2F;gtk&#x2F;trait.ButtonExt.html#tymethod.connect_clicked&quot;&gt;&lt;code&gt;connect_clicked&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;connect_clicked&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;F: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Fn&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;+ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&amp;#39;static&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: F) -&amp;gt; SignalHandlerId
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;code&gt;connect_clicked&lt;&#x2F;code&gt; is a method which takes some function &lt;code&gt;f&lt;&#x2F;code&gt; of type
&lt;code&gt;F&lt;&#x2F;code&gt; and returns a &lt;code&gt;SignalHandlerId&lt;&#x2F;code&gt;. We&#x27;re not using that return
value, so just ignore it. The function is a &lt;code&gt;Fn&lt;&#x2F;code&gt;. Therefore, we&#x27;re
&lt;em&gt;not&lt;&#x2F;em&gt; allowed to pass in a &lt;code&gt;FnMut&lt;&#x2F;code&gt; or an &lt;code&gt;FnOnce&lt;&#x2F;code&gt;. GTK must be allowed
to call that function multiple times without the restrictions of a
mutable context. So keeping a mutable reference won&#x27;t work.&lt;&#x2F;p&gt;
&lt;p&gt;The other interesting thing is &lt;code&gt;+ &#x27;static&lt;&#x2F;code&gt;. We briefly mentioned
lifetime parameters above. &lt;code&gt;&#x27;static&lt;&#x2F;code&gt; is a special lifetime parameter,
which means &amp;quot;can live for the entire lifetime of the program.&amp;quot; As one
nice example of this, all string literals have type &lt;code&gt;&amp;amp;&#x27;static str&lt;&#x2F;code&gt;,
though we usually just write &lt;code&gt;&amp;amp;str&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;The problem is that our &lt;code&gt;file&lt;&#x2F;code&gt; does &lt;em&gt;not&lt;&#x2F;em&gt; have &lt;code&gt;&#x27;static&lt;&#x2F;code&gt; lifetime. It
is created in the &lt;code&gt;main&lt;&#x2F;code&gt; function, remains in the &lt;code&gt;main&lt;&#x2F;code&gt; function, and
only lives as long as the &lt;code&gt;main&lt;&#x2F;code&gt; function. You may argue that the
&lt;code&gt;main&lt;&#x2F;code&gt; function lives the entire length of the program, but that&#x27;s not
exactly true. In our example above, &lt;code&gt;button&lt;&#x2F;code&gt; will outlive &lt;code&gt;file&lt;&#x2F;code&gt; when
calling &lt;code&gt;drop&lt;&#x2F;code&gt;s (since &lt;code&gt;drop&lt;&#x2F;code&gt;s are performed in FILO order). If the
&lt;code&gt;drop&lt;&#x2F;code&gt; for a button decided to call the click
callback one more time, we&#x27;d have memory unsafety.&lt;&#x2F;p&gt;
&lt;p&gt;So what we&#x27;re left with is: we need a closure which does not have a
mutable reference to local data. How do we do that?&lt;&#x2F;p&gt;
&lt;h2 id=&quot;move-it&quot;&gt;Move it&lt;&#x2F;h2&gt;
&lt;p&gt;We can get the compiler to stop complaining about the lifetime by
moving the variable into the closure. Now we&#x27;re guaranteed that the
&lt;code&gt;file&lt;&#x2F;code&gt; will live as long as the closure itself, meeting the guarantees
demanded by &lt;code&gt;&#x27;static&lt;&#x2F;code&gt;. Do accomplish this, stick &lt;code&gt;move&lt;&#x2F;code&gt; in front of
the closure.&lt;&#x2F;p&gt;
&lt;p&gt;This still doesn&#x27;t solve our &lt;code&gt;Fn&lt;&#x2F;code&gt; issue, however. How can we allow our
callback to be called multiple times after moving the value in?&lt;&#x2F;p&gt;
&lt;h2 id=&quot;reference-counting-hint-nope&quot;&gt;Reference counting (hint: nope)&lt;&#x2F;h2&gt;
&lt;p&gt;We&#x27;ve reached a point where the normal borrow rules of Rust simply
aren&#x27;t enough. We cannot prove to the compiler that our callback will
obey the mutable reference rules: exactly one mutable reference will
exist at a given time. These kinds of situations occur often enough
that the standard library provides built in support for reference
counted types.&lt;&#x2F;p&gt;
&lt;p&gt;Add the following statement to the top of your &lt;code&gt;main.rs&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;std::rc::Rc;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;An &lt;code&gt;Rc&lt;&#x2F;code&gt; is a single threaded reference counted value. There&#x27;s also an
&lt;code&gt;Arc&lt;&#x2F;code&gt; type, which is atomic, and can be used in multithreaded
applications. Since GTK is a single-threaded library, we&#x27;re safe using
an &lt;code&gt;Rc&lt;&#x2F;code&gt; instead of an &lt;code&gt;Arc&lt;&#x2F;code&gt;. One really awesome thing about Rust is
that if you make a mistake about this, the compiler can catch
you. This is because &lt;code&gt;Rc&lt;&#x2F;code&gt; does not implement the &lt;code&gt;Sync&lt;&#x2F;code&gt; and &lt;code&gt;Send&lt;&#x2F;code&gt;
traits. See more in the
&lt;a href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;std&#x2F;marker&#x2F;trait.Send.html&quot;&gt;&lt;code&gt;Send&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;
documentation.&lt;&#x2F;p&gt;
&lt;p&gt;Anyway, back to our example. We can wrap up our original &lt;code&gt;file&lt;&#x2F;code&gt; with
reference counting with this:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; file = std::fs::File::create(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;mylog.txt&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;();
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; file = Rc::new(file);
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;How do we then get access to the underlying &lt;code&gt;File&lt;&#x2F;code&gt; to use it? Turns
out: we don&#x27;t need to do anything special. Keeping our original
&lt;code&gt;file.write_all&lt;&#x2F;code&gt; does what we want. This is because &lt;code&gt;Rc&lt;&#x2F;code&gt; implements
the &lt;code&gt;Deref&lt;&#x2F;code&gt; trait:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;impl&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;T&amp;gt; Deref &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;Rc&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;T&amp;gt; {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;Target &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= T;
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;...
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This means that you can get a reference to a &lt;code&gt;T&lt;&#x2F;code&gt; from a &lt;code&gt;Rc&amp;lt;T&amp;gt;&lt;&#x2F;code&gt;. Since
method call syntax automatically takes a reference, everything
works. Nice.&lt;&#x2F;p&gt;
&lt;p&gt;Well, almost everything:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;error[E0596]: cannot borrow data in a `&amp;amp;` reference as mutable
  --&amp;gt; src&#x2F;main.rs:32:9
   |
32 |         file.write_all(b&amp;quot;I was clicked.\n&amp;quot;);
   |         ^^^^ cannot borrow as mutable
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Reference counting allows us to have multiple references to a value,
but they&#x27;re all &lt;em&gt;immutable&lt;&#x2F;em&gt; references. Looks like we haven&#x27;t actually
made our situation any better than before, where we had ensured that
the single owner of our data was the closure.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;refcell&quot;&gt;RefCell&lt;&#x2F;h2&gt;
&lt;p&gt;&lt;code&gt;RefCell&lt;&#x2F;code&gt; is designed to exactly solve this problem. I&#x27;m not going to
go into detail explaining it, because the &lt;a href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;std&#x2F;cell&#x2F;index.html&quot;&gt;API docs for &lt;code&gt;std::cell&lt;&#x2F;code&gt; do
that better than I
could&lt;&#x2F;a&gt;. I recommend you
go read that intro now, come back and work on this code, and then go
read the docs again. Personally, I had to read that explanation about
4 or 5 times and bash my head against some broken code before it
finally sank in correctly.&lt;&#x2F;p&gt;
&lt;p&gt;Anyway, add &lt;code&gt;use std::cell::RefCell;&lt;&#x2F;code&gt;, and then wrap a &lt;code&gt;RefCell&lt;&#x2F;code&gt;
around the original &lt;code&gt;File&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; file = std::fs::File::create(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;mylog.txt&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;();
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; file = RefCell::new(file);
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now our code will fail to compile with a different message:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;error[E0599]: no method named `write_all` found for type `std::cell::RefCell&amp;lt;std::fs::File&amp;gt;` in the current scope
  --&amp;gt; src&#x2F;main.rs:29:14
   |
29 |         file.write_all(b&amp;quot;I was clicked.\n&amp;quot;).unwrap();
   |
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Unlike &lt;code&gt;Rc&lt;&#x2F;code&gt;, with &lt;code&gt;RefCell&lt;&#x2F;code&gt; we cannot rely on the &lt;code&gt;Deref&lt;&#x2F;code&gt;
implementation to get us a &lt;code&gt;File&lt;&#x2F;code&gt;. Instead, we&#x27;ll need to use a method
on &lt;code&gt;RefCell&lt;&#x2F;code&gt; to get a reference to the &lt;code&gt;File&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;file.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;borrow&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;().&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;write_all&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;b&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;I was clicked.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#dc322f;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;But that doesn&#x27;t quite work:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;error[E0596]: cannot borrow data in a `&amp;amp;` reference as mutable
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Fortunately, that fix is as easy as using &lt;code&gt;borrow_mut()&lt;&#x2F;code&gt; instead. And
now our program works, hurray!&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;NOTE&lt;&#x2F;strong&gt; Often, reference counting (&lt;code&gt;Rc&lt;&#x2F;code&gt; or &lt;code&gt;Arc&lt;&#x2F;code&gt;) and cells (&lt;code&gt;Cell&lt;&#x2F;code&gt;,
&lt;code&gt;RefCell&lt;&#x2F;code&gt;, or &lt;code&gt;Mutex&lt;&#x2F;code&gt;) go hand in hand, which is why my first instinct
in writing this lesson was to use both an &lt;code&gt;Rc&lt;&#x2F;code&gt; and a
&lt;code&gt;RefCell&lt;&#x2F;code&gt;. However, in this case, it turns out that just the &lt;code&gt;RefCell&lt;&#x2F;code&gt;
is sufficient.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;exercise-7&quot;&gt;Exercise 7&lt;&#x2F;h2&gt;
&lt;p&gt;The error handling in this program is lackluster. There are three problems:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;If &lt;code&gt;gtk::init()&lt;&#x2F;code&gt; fails, the exit code of our program is still &lt;code&gt;0&lt;&#x2F;code&gt;
(indicating success).&lt;&#x2F;li&gt;
&lt;li&gt;If opening &lt;code&gt;mylog.txt&lt;&#x2F;code&gt; fails, we panic.&lt;&#x2F;li&gt;
&lt;li&gt;If writing to the file fails, we panic.&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;To fix this, have &lt;code&gt;main&lt;&#x2F;code&gt; return a value of type &lt;code&gt;Result&amp;lt;(), Box&amp;lt;std::error::Error&amp;gt;&amp;gt;&lt;&#x2F;code&gt;. Most other errors can be automatically
coerced via &lt;code&gt;From::from&lt;&#x2F;code&gt; into &lt;code&gt;Box&amp;lt;std::error::Error&amp;gt;&lt;&#x2F;code&gt;. For problems
(1) and (2), use the standard error handling mechanisms we discussed
back in lesson 3. For problem (3), print an error message with
&lt;code&gt;eprintln!&lt;&#x2F;code&gt; when an error occurs.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;fearless-concurrency&quot;&gt;Fearless concurrency!&lt;&#x2F;h2&gt;
&lt;p&gt;It&#x27;s finally time to do some fearless concurrency. We&#x27;re going to
write a program which will:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Allocate a string containing the word &amp;quot;Fearless&amp;quot;&lt;&#x2F;li&gt;
&lt;li&gt;Fork a thread every second for 10 iterations&lt;&#x2F;li&gt;
&lt;li&gt;In the forked thread:
&lt;ul&gt;
&lt;li&gt;Add another exclamation point to the string&lt;&#x2F;li&gt;
&lt;li&gt;Print the string&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;Before we begin, you can probably identify some complex pieces of
ownership that are going to go on here:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Multiple threads will have access to some mutable data&lt;&#x2F;li&gt;
&lt;li&gt;We need to ensure only one writer at a time&lt;&#x2F;li&gt;
&lt;li&gt;We need to ensure that the data is released when everyone is done
with it&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;Instead of trying to design a great solution to this from the
beginning, we&#x27;ll treat this like a proper crash course. We&#x27;ll do the
most naive stuff possible, look at the error messages, and try to
improve. If you think you can implement the complete program yourself
now, definitely give it a shot! Even if you don&#x27;t think you can
implement it yourself, it&#x27;s worth trying. The effort will make the
explanation below more helpful.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;introducing-the-functions&quot;&gt;Introducing the functions&lt;&#x2F;h3&gt;
&lt;p&gt;We&#x27;re going to use the following three functions:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;std&#x2F;thread&#x2F;fn.spawn.html&quot;&gt;&lt;code&gt;std::thread::spawn&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;
to spawn a thread. It has an interesting signature:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;pub &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;spawn&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;F, T&amp;gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: F) -&amp;gt; JoinHandle&amp;lt;T&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;    F: FnOnce() -&amp;gt; T,
    F: Send + &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&amp;#39;static&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;,
    T: Send + &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;&amp;#39;static&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;,
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The &lt;code&gt;Send&lt;&#x2F;code&gt; trait means that both the provided function and its return
value must be values which can be sent to a different thread. The
&lt;code&gt;&#x27;static&lt;&#x2F;code&gt; bit says that we cannot retain any references to local
variables. And the &lt;code&gt;FnOnce()&lt;&#x2F;code&gt; bit says that &lt;em&gt;any&lt;&#x2F;em&gt; closure will work.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;std&#x2F;thread&#x2F;fn.sleep.html&quot;&gt;&lt;code&gt;std::thread::sleep&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;
to have the main thread sleep. It takes a value of type &lt;code&gt;Duration&lt;&#x2F;code&gt;,
which brings us to our last function:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;std&#x2F;time&#x2F;struct.Duration.html&quot;&gt;&lt;code&gt;std::time::Duration::new&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;
takes the number of seconds and nanoseconds in a duration.&lt;&#x2F;p&gt;
&lt;p&gt;Before we introduce the great fun which is spawning a new thread,
let&#x27;s try a single threaded version:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;std::thread::sleep;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;std::time::Duration;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; msg: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::from(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Fearless&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for _ in &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;..&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;11 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
        msg.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;push&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, msg);
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;sleep&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(Duration::new(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;));
    }
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We can even wrap up that &lt;code&gt;msg.push&lt;&#x2F;code&gt; and &lt;code&gt;println!&lt;&#x2F;code&gt; in a closure to get
a bit closer to the call to &lt;code&gt;spawn&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;std::thread::sleep;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;std::time::Duration;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; msg: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::from(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Fearless&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for _ in &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;..&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;11 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;inner &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;|| {
            msg.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;push&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, msg);
        };
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;inner&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;();
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;sleep&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(Duration::new(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;));
    }
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;That gives us an error message:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;error[E0596]: cannot borrow immutable local variable `inner` as mutable
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Go ahead and fix that and make this compile.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;introducing-spawn&quot;&gt;Introducing spawn&lt;&#x2F;h3&gt;
&lt;p&gt;The simplest way to introduce spawn is to replace the &lt;code&gt;inner()&lt;&#x2F;code&gt; call
with &lt;code&gt;spawn(inner)&lt;&#x2F;code&gt;. Replace:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;std::thread::sleep;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;with&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;std::thread::{sleep, spawn};
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And add the &lt;code&gt;spawn&lt;&#x2F;code&gt; call. We get the error message:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;error[E0373]: closure may outlive the current function, but it borrows `msg`, which is owned by the current function
 --&amp;gt; main.rs:7:25
  |
7 |         let mut inner = || {
  |                         ^^ may outlive borrowed value `msg`
8 |             msg.push(&amp;#39;!&amp;#39;);
  |             --- `msg` is borrowed here
help: to force the closure to take ownership of `msg` (and any other referenced variables), use the `move` keyword
  |
7 |         let mut inner = move || {
  |                         ^^^^^^^

error: aborting due to previous error
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Seems simple enough: we have to have a self contained closure to pass
to &lt;code&gt;spawn&lt;&#x2F;code&gt;, which can&#x27;t refer to values from the parent thread. Let&#x27;s
just add a &lt;code&gt;move&lt;&#x2F;code&gt; in front of the closure. We get an error message:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;error[E0382]: capture of moved value: `msg`
 --&amp;gt; main.rs:8:13
  |
7 |         let mut inner = move || {
  |                         ------- value moved (into closure) here
8 |             msg.push(&amp;#39;!&amp;#39;);
  |             ^^^ value captured here after move
  |
  = note: move occurs because `msg` has type `std::string::String`, which does not implement the `Copy` trait
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;I still don&#x27;t find these error messages particularly enlightening. But
it&#x27;s telling us that we&#x27;re trying to capture a moved value. This is
happening because we&#x27;re moving the value into the closure in the first
iteration of the loop, and then trying to move it in again. That
clearly won&#x27;t work!&lt;&#x2F;p&gt;
&lt;h3 id=&quot;a-broken-solution&quot;&gt;A broken solution&lt;&#x2F;h3&gt;
&lt;p&gt;Let&#x27;s just cheat and create a new copy of the string for each
iteration. That&#x27;s easy enough: add the following above &lt;code&gt;let mut inner&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; msg = msg.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;clone&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;();
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This will compile (with a warning) and run, but it has the wrong
output. We aren&#x27;t adding extra exclamation points each time. We&#x27;re not
actually dealing with shared mutable data. Darn.&lt;&#x2F;p&gt;
&lt;p&gt;But that cloning gives me another idea...&lt;&#x2F;p&gt;
&lt;h3 id=&quot;reference-counting&quot;&gt;Reference counting&lt;&#x2F;h3&gt;
&lt;p&gt;Maybe we can throw in that reference counting we mentioned previously,
and let each thread keep a pointer to the same piece of data.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;std::thread::{sleep, spawn};
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;std::time::Duration;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;std::rc::Rc;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; msg = Rc::new(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::from(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Fearless&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;));
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for _ in &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;..&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;11 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; msg = msg.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;clone&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;();
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; inner = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;move &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;|| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
            msg.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;push&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, msg);
        };
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;spawn&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(inner);
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;sleep&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(Duration::new(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;));
    }
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Well, &lt;em&gt;that&#x27;s&lt;&#x2F;em&gt; a new one:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;error[E0277]: `std::rc::Rc&amp;lt;std::string::String&amp;gt;` cannot be sent between threads safely
  --&amp;gt; main.rs:13:9
   |
13 |         spawn(inner);
   |         ^^^^^ `std::rc::Rc&amp;lt;std::string::String&amp;gt;` cannot be sent between threads safely
   |
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;There&#x27;s that fearless concurrency we&#x27;ve heard so much about! The
compiler is preventing us from sending an &lt;code&gt;Rc&lt;&#x2F;code&gt; value between
threads. It would be nice if the compiler mentioned it, but we already
know that for multithreaded applications, we need an atomic reference
counter, or &lt;code&gt;std::sync::Arc&lt;&#x2F;code&gt;. Go ahead and switch over to that. You
should get a new error message:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;error[E0596]: cannot borrow immutable borrowed content as mutable
  --&amp;gt; main.rs:10:13
   |
10 |             msg.push(&amp;#39;!&amp;#39;);
   |             ^^^ cannot borrow as mutable

error: aborting due to previous error
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;inner-mutability&quot;&gt;Inner mutability&lt;&#x2F;h3&gt;
&lt;p&gt;Above, I mentioned that &lt;code&gt;Rc&lt;&#x2F;code&gt; and &lt;code&gt;RefCell&lt;&#x2F;code&gt; usually go together. The
&lt;code&gt;Rc&lt;&#x2F;code&gt; provides reference counting, and the &lt;code&gt;RefCell&lt;&#x2F;code&gt; provides
mutability. Maybe we can combine &lt;code&gt;Arc&lt;&#x2F;code&gt; and &lt;code&gt;RefCell&lt;&#x2F;code&gt; too?&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;std::thread::{sleep, spawn};
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;std::time::Duration;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;std::sync::Arc;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;std::cell::RefCell;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; msg = Arc::new(RefCell::new(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::from(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Fearless&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)));
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for _ in &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;..&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;11 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; msg = msg.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;clone&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;();
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; inner = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;move &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;|| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; msg = msg.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;borrow_mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;();
            msg.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;push&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, msg);
        };
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;spawn&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(inner);
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;sleep&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(Duration::new(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;));
    }
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;More fearless concurrency:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;error[E0277]: `std::cell::RefCell&amp;lt;std::string::String&amp;gt;` cannot be shared between threads safely
  --&amp;gt; main.rs:15:9
   |
15 |         spawn(inner);
   |         ^^^^^ `std::cell::RefCell&amp;lt;std::string::String&amp;gt;` cannot be shared between threads safely
   |
   = help: the trait `std::marker::Sync` is not implemented for `std::cell::RefCell&amp;lt;std::string::String&amp;gt;`
   = note: required because of the requirements on the impl of `std::marker::Send` for `std::sync::Arc&amp;lt;std::cell::RefCell&amp;lt;std::string::String&amp;gt;&amp;gt;`
   = note: required because it appears within the type `[closure@main.rs:10:25: 14:10 msg:std::sync::Arc&amp;lt;std::cell::RefCell&amp;lt;std::string::String&amp;gt;&amp;gt;]`
   = note: required by `std::thread::spawn`
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You could go search for more info, but the normal way to have a
mutable, multithreaded cell is a &lt;code&gt;Mutex&lt;&#x2F;code&gt;. Instead of &lt;code&gt;borrow_mut()&lt;&#x2F;code&gt;,
we have a &lt;code&gt;lock()&lt;&#x2F;code&gt; method, which ensures that only one thread at a
time is using the mutex. Let&#x27;s try that out:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;std::thread::{sleep, spawn};
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;std::time::Duration;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;std::sync::{Arc, Mutex};

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; msg = Arc::new(Mutex::new(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::from(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Fearless&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)));
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for _ in &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;..&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;11 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; msg = msg.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;clone&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;();
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; inner = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;move &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;|| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; msg = msg.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;lock&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;();
            msg.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;push&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, msg);
        };
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;spawn&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(inner);
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;sleep&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(Duration::new(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;));
    }
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We get the error:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;error[E0599]: no method named `push` found for type `std::result::Result&amp;lt;std::sync::MutexGuard&amp;lt;&amp;#39;_, std::string::String&amp;gt;, std::sync::PoisonError&amp;lt;std::sync::MutexGuard&amp;lt;&amp;#39;_, std::string::String&amp;gt;&amp;gt;&amp;gt;` in the current scope
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Oh, right. &lt;code&gt;lock&lt;&#x2F;code&gt;ing can fail, due to something called
poisoning. (&lt;a href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;std&#x2F;sync&#x2F;struct.Mutex.html#poisoning&quot;&gt;Check out the
docs&lt;&#x2F;a&gt;
for more information.) To quote the docs:&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;Most usage of a mutex will simply &lt;code&gt;unwrap()&lt;&#x2F;code&gt; these results,
propagating panics among threads to ensure that a possibly invalid
invariant is not witnessed.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;This is the closest to runtime exceptions I&#x27;ve seen the Rust docs
mention, nice. If we add that &lt;code&gt;.unwrap()&lt;&#x2F;code&gt;, we get told that &lt;code&gt;msg&lt;&#x2F;code&gt;
needs to be mutable. And if we add &lt;code&gt;mut&lt;&#x2F;code&gt;, we&#x27;ve written our first
multithreaded Rust application using shared mutable state.&lt;&#x2F;p&gt;
&lt;p&gt;Notice how the compiler prevented us from making some serious
concurrency mistakes? That&#x27;s pretty awesome.&lt;&#x2F;p&gt;
&lt;p&gt;As a final step, see which &lt;code&gt;mut&lt;&#x2F;code&gt;s and &lt;code&gt;move&lt;&#x2F;code&gt;s you can and cannot
remove from the final program. Make sure you can justify to yourself
why the compiler does or does not accept each change.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;next-time&quot;&gt;Next time&lt;&#x2F;h2&gt;
&lt;p&gt;You&#x27;re now deep into the hard parts of Rust. What comes now is getting
more comfortable with the hairy bits of ownership and closures, and to
get more comfortable with the library ecosystem. We&#x27;re ready to get
much more real world next time, and learn about tokio, the de facto
standard async I&#x2F;O framework in Rust.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;www.fpcomplete.com&#x2F;rust&quot;&gt;Rust at FP Complete&lt;&#x2F;a&gt; | &lt;a href=&quot;https:&#x2F;&#x2F;www.snoyman.com&#x2F;blog&#x2F;2018&#x2F;10&#x2F;introducing-rust-crash-course&quot;&gt;Introduction&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
</description>
        </item>
        <item>
            <title>Crates and more iterators - Rust Crash Course lesson 4 - exercise solutions</title>
            <pubDate>Wed, 14 Nov 2018 00:00:00 +0000</pubDate>
            <link>https://www.snoyman.com/blog/2018/11/rust-crash-course-04-crates-more-iterators-solutions/</link>
            <guid>https://www.snoyman.com/blog/2018/11/rust-crash-course-04-crates-more-iterators-solutions/</guid>
            <description>&lt;p&gt;&lt;strong&gt;Heads up&lt;&#x2F;strong&gt; This blog post series has been updated and published as an eBook by FP Complete. I&#x27;d recommend reading that version instead of these posts. If you&#x27;re interested, please check out the &lt;a href=&quot;https:&#x2F;&#x2F;www.fpcomplete.com&#x2F;rust&#x2F;crash-course&#x2F;&quot;&gt;Rust Crash Course eBook&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Below are the solutions to the exercises from the last Rust Crash
Course lesson, &amp;quot;Crates, I&#x2F;O, and more iterators.&amp;quot;&lt;&#x2F;p&gt;
&lt;p&gt;This post is part of a series based on &lt;a href=&quot;https:&#x2F;&#x2F;www.fpcomplete.com&#x2F;rust&quot;&gt;teaching Rust at FP
Complete&lt;&#x2F;a&gt;. If you&#x27;re reading this post outside
of the blog, you can find links to all posts in the series &lt;a href=&quot;https:&#x2F;&#x2F;www.snoyman.com&#x2F;blog&#x2F;2018&#x2F;10&#x2F;introducing-rust-crash-course&quot;&gt;at the top of the
introduction
post&lt;&#x2F;a&gt;. You
can also &lt;a href=&quot;https:&#x2F;&#x2F;www.snoyman.com&#x2F;feed&#x2F;rust-crash-course&quot;&gt;subscribe to the RSS
feed&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;exercise-1&quot;&gt;Exercise 1&lt;&#x2F;h2&gt;
&lt;p&gt;You can find my complete solution &lt;a href=&quot;https:&#x2F;&#x2F;gist.github.com&#x2F;snoyberg&#x2F;936ecefd7b6fabc2438e51f02bfe36cb&quot;&gt;as a Github
Gist&lt;&#x2F;a&gt;. If
your solution looks a bit different than mine, don&#x27;t worry. Also, see
if there&#x27;s anything interesting you can learn from my implementation,
or some improvements you&#x27;d like to make to it.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;exercise-2&quot;&gt;Exercise 2&lt;&#x2F;h2&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;struct &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;TheAnswer&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;impl &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;Iterator &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;TheAnswer &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;Item &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;

    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;next&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Option&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;gt; {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;42&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
    }
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;exercise-3&quot;&gt;Exercise 3&lt;&#x2F;h2&gt;
&lt;p&gt;Let&#x27;s start with the simpler solution:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;struct &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;Fibs &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;x&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;,
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;y&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;,
}

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;fibs&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() -&amp;gt; Fibs {
    Fibs {
        x: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;,
        y: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;,
    }
}

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;impl &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;Iterator &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;Fibs &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;Item &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;

    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;next&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Option&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;gt; {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; orig_x = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;.x;
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; orig_y = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;.y;

        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;.x = orig_y;
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;.y = orig_x + orig_y;

        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(orig_x)
    }
}

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; i &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;in fibs&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;().&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;take&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;10&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, i);
    }
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;However, if you bump that &lt;code&gt;take(10)&lt;&#x2F;code&gt; to &lt;code&gt;take(47)&lt;&#x2F;code&gt;, the end of your
output will look like:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;701408733
1134903170
thread &amp;#39;main&amp;#39; panicked at &amp;#39;attempt to add with overflow&amp;#39;, foo.rs:21:18
note: Run with `RUST_BACKTRACE=1` for a backtrace.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;One solution would be to bump to a &lt;code&gt;u64&lt;&#x2F;code&gt;, but that&#x27;s just delaying the
problem. Instead, we can use Rust&#x27;s checked addition method:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;next&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Option&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;gt; {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; orig_x = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;.x;
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; orig_y = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;.y;

    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;match&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; orig_x.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;checked_add&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(orig_y) {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;&#x2F;&#x2F; overflow
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;None =&amp;gt; None&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;,

        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;&#x2F;&#x2F; no overflow
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(new_y) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;.x = orig_y;
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;.y = new_y;

            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(orig_x)
        }
    }
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now our stream will stop as soon as overflow occurs.&lt;&#x2F;p&gt;
&lt;p&gt;If you want to get &lt;em&gt;really&lt;&#x2F;em&gt; advanced here, you could actually output
two more values. To do so, we need to assign to a &lt;em&gt;derefenced&lt;&#x2F;em&gt; value
and use an &lt;code&gt;enum&lt;&#x2F;code&gt; to track our state:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;next&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Option&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;gt; {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;Fibs::*;
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;match &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;self &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
        Done &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt; None&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;,
        OneLeft(x) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
            *&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;self &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= Done;
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(x)
        }
        Running(orig_x, orig_y) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
            *&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;self &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;match&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; orig_x.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;checked_add&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(orig_y) {
                &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;&#x2F;&#x2F; overflow
                &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;None =&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; OneLeft(orig_y),
                &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(new_y) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; Running(orig_y, new_y),
            };

            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(orig_x)
        }
    }
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;exercise-4&quot;&gt;Exercise 4&lt;&#x2F;h2&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;impl&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;I&amp;gt; Iterator &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;Doubler&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;I&amp;gt;
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;    I: Iterator,
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;I::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;Item: std::ops::Add&amp;lt;Output=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;I::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;Item&amp;gt; + Copy,
{
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;Item &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= I::Item;
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;next&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Option&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Self::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;Item&amp;gt; {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;match &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;.iter.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;next&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;None =&amp;gt; None&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;,
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(x) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt; Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(x + x),
        }
    }
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;exercise-5&quot;&gt;Exercise 5&lt;&#x2F;h2&gt;
&lt;p&gt;The &lt;code&gt;fold&lt;&#x2F;code&gt; method takes two parameters: the initial value, and a
function for adding the running total with the next value. One
approach using closures is:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; res = (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;..&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;11&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;fold&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, |&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;x&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;y&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| x + y);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, res);
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Another approach is to directly refer to the addition
function. Remember how there was a &lt;code&gt;Mul&lt;&#x2F;code&gt; trait for the &lt;code&gt;*&lt;&#x2F;code&gt; operator?
There&#x27;s also an &lt;code&gt;Add&lt;&#x2F;code&gt; trait for addition:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; res = (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;..&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;11&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;fold&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, std::ops::Add::add);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, res);
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;As for writing our own &lt;code&gt;sum&lt;&#x2F;code&gt; function: we&#x27;ll end up back in the
situation where things are generic and we have to provide appropriate
traits. We&#x27;ll follow a similar approach with using &lt;code&gt;From&lt;&#x2F;code&gt; and a &lt;code&gt;u8&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;sum&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;I&amp;gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;iter&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: I) -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;I::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;Item
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;    I: Iterator,
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;I::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;Item: std::ops::Add&amp;lt;Output=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;I::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;Item&amp;gt; + &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;From&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u8&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;gt;,
{
    iter.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;fold&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;From&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::from(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u8&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;), std::ops::Add::add)
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;www.fpcomplete.com&#x2F;rust&quot;&gt;Rust at FP Complete&lt;&#x2F;a&gt; | &lt;a href=&quot;https:&#x2F;&#x2F;www.snoyman.com&#x2F;blog&#x2F;2018&#x2F;10&#x2F;introducing-rust-crash-course&quot;&gt;Introduction&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
</description>
        </item>
        <item>
            <title>Crates and more iterators - Rust Crash Course lesson 4</title>
            <pubDate>Mon, 12 Nov 2018 00:00:00 +0000</pubDate>
            <link>https://www.snoyman.com/blog/2018/11/rust-crash-course-04-crates-more-iterators/</link>
            <guid>https://www.snoyman.com/blog/2018/11/rust-crash-course-04-crates-more-iterators/</guid>
            <description>&lt;p&gt;&lt;strong&gt;Heads up&lt;&#x2F;strong&gt; This blog post series has been updated and published as an eBook by FP Complete. I&#x27;d recommend reading that version instead of these posts. If you&#x27;re interested, please check out the &lt;a href=&quot;https:&#x2F;&#x2F;www.fpcomplete.com&#x2F;rust&#x2F;crash-course&#x2F;&quot;&gt;Rust Crash Course eBook&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;I&#x27;m getting bored with bouncy, this is our third week talking about this program. It&#x27;s time to finish this off! How do we do double buffering? It&#x27;s time to learn about external libraries in Rust, or &lt;em&gt;crates&lt;&#x2F;em&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;This post is part of a series based on &lt;a href=&quot;https:&#x2F;&#x2F;www.fpcomplete.com&#x2F;rust&quot;&gt;teaching Rust at FP
Complete&lt;&#x2F;a&gt;. If you&#x27;re reading this post outside
of the blog, you can find links to all posts in the series &lt;a href=&quot;https:&#x2F;&#x2F;www.snoyman.com&#x2F;blog&#x2F;2018&#x2F;10&#x2F;introducing-rust-crash-course&quot;&gt;at the top of the
introduction
post&lt;&#x2F;a&gt;. You
can also &lt;a href=&quot;https:&#x2F;&#x2F;www.snoyman.com&#x2F;feed&#x2F;rust-crash-course&quot;&gt;subscribe to the RSS
feed&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;We still haven&#x27;t fixed our double buffering problem, let&#x27;s finally do that.
We&#x27;re also going to introduce some more error handling from
the standard library. And then we&#x27;ll get to play a bit more with
iterators as well.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;finding-a-crate&quot;&gt;Finding a crate&lt;&#x2F;h2&gt;
&lt;p&gt;To do double buffering in the terminal, we&#x27;re going to want some kind
of a curses library. We&#x27;re going to look for a crate on
&lt;a href=&quot;https:&#x2F;&#x2F;crates.io&#x2F;&quot;&gt;crates.io&lt;&#x2F;a&gt;. On that page, &lt;a href=&quot;https:&#x2F;&#x2F;crates.io&#x2F;search?q=curses&quot;&gt;search for
&amp;quot;curses&amp;quot;&lt;&#x2F;a&gt;. Looking through the
download counts and the descriptions,
&lt;a href=&quot;https:&#x2F;&#x2F;crates.io&#x2F;crates&#x2F;pancurses&quot;&gt;pancurses&lt;&#x2F;a&gt; looks nice, especially
since it supports Unix and Windows. Let&#x27;s use it!&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;Side note&lt;&#x2F;strong&gt; If you&#x27;re wondering, this is &lt;em&gt;exactly&lt;&#x2F;em&gt; the process I
went through when writing up bouncy. I had no prior knowledge to tell
me pancurses was going to be the right choice. Also, this program
happens to be the first time I&#x27;ve ever used a curses library.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;starting-a-project&quot;&gt;Starting a project&lt;&#x2F;h2&gt;
&lt;p&gt;We&#x27;re going to create a new project for this using &lt;code&gt;cargo new&lt;&#x2F;code&gt;. We&#x27;re
going to pull in the bouncy code from the end of week 2 (before the
introduction of command line parsing), since as we&#x27;ll see later, we
won&#x27;t need that parsing anymore.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;$ cargo new bouncy4
$ cd bouncy4
$ curl https:&#x2F;&#x2F;gist.githubusercontent.com&#x2F;snoyberg&#x2F;5307d493750d7b48c1c5281961bc31d0&#x2F;raw&#x2F;8f467e87f69a197095bda096cbbb71d8d813b1d7&#x2F;main.rs &amp;gt; src&#x2F;main.rs
$ cargo run
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The ball should be bouncing around your terminal, right? Great!&lt;&#x2F;p&gt;
&lt;h2 id=&quot;adding-the-crate&quot;&gt;Adding the crate&lt;&#x2F;h2&gt;
&lt;p&gt;The configuration for your project lives in the file &lt;code&gt;Cargo.toml&lt;&#x2F;code&gt;,
known as the &lt;em&gt;manifest&lt;&#x2F;em&gt;. On my system, it looks like this:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-toml&quot; data-lang=&quot;toml&quot;&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;package&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;name &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;bouncy4&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;version &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;0.1.0&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;authors &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Michael Snoyman &amp;lt;michael@snoyman.com&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;]

[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;dependencies&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;It will look a bit different on your machine (unless your name is also
Michael Snoyman). Notice that &lt;code&gt;[dependencies]&lt;&#x2F;code&gt; section at the end?
That&#x27;s where we add information on external crates. If you go back to
&lt;a href=&quot;https:&#x2F;&#x2F;crates.io&#x2F;crates&#x2F;pancurses&quot;&gt;the pancurses page on
crates.io&lt;&#x2F;a&gt;, you&#x27;ll see:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;Cargo.toml  pancurses = &amp;quot;0.16.0&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;It may be different when you&#x27;re reading this. That&#x27;s telling us what
we can add to the dependencies section to depend on the library. There
are lots of details about how to specify dependencies, which we won&#x27;t
get into here (and probably never will in the crash course, I&#x27;ve spent
enough of my life discussing dependency management already :) ). You
can read more in &lt;a href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;cargo&#x2F;reference&#x2F;manifest.html&quot;&gt;the Cargo book
reference&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Anyway, go ahead and add &lt;code&gt;pancurses = &amp;quot;0.16.0&amp;quot;&lt;&#x2F;code&gt; to the end of your
&lt;code&gt;Cargo.toml&lt;&#x2F;code&gt; and run &lt;code&gt;cargo build&lt;&#x2F;code&gt;. &lt;code&gt;cargo&lt;&#x2F;code&gt; should run off and do some
updating, downloading, compiling, and end up with an executable that
does exactly the same thing as it did before. Perfect, time to use it!&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;NOTE&lt;&#x2F;strong&gt; It seems like, at the time of writing, &lt;code&gt;pancurses&lt;&#x2F;code&gt; does not build
against the latest version of the &lt;code&gt;ncurses&lt;&#x2F;code&gt; package. If you get a build
failure, you may need to also add &lt;code&gt;ncurses = &amp;quot;= 5.94.0&amp;quot;&lt;&#x2F;code&gt; to your &lt;code&gt;dependencies&lt;&#x2F;code&gt;
to force Cargo to use an older, compatible version.&lt;&#x2F;p&gt;
&lt;p&gt;With the current version of the Rust, you&#x27;ll also need to add the following to
the top of &lt;code&gt;src&#x2F;main.rs&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;extern crate&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; pancurses;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This requirement is disappearing with Rust 2018. If you&#x27;d like play around with
that, you&#x27;ll need to switch to a nightly compiler and add &lt;code&gt;edition = &amp;quot;2018&amp;quot;&lt;&#x2F;code&gt; to
your &lt;code&gt;Cargo.toml&lt;&#x2F;code&gt;. But we&#x27;re going to stick to the stable compiler and Rust
2015.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;library-docs&quot;&gt;Library docs&lt;&#x2F;h2&gt;
&lt;p&gt;On the crates.io page, there&#x27;s a &lt;a href=&quot;https:&#x2F;&#x2F;docs.rs&#x2F;pancurses&#x2F;0.16.0&#x2F;pancurses&#x2F;&quot;&gt;link to the
documentation&lt;&#x2F;a&gt; for
pancurses. Open that in a new tab. Also, reading down the crates page,
we get a nice usage example. In &lt;code&gt;main()&lt;&#x2F;code&gt;, the first call is to
&lt;code&gt;initscr&lt;&#x2F;code&gt;. Jump over to the API documentation and search for
&lt;code&gt;initscr&lt;&#x2F;code&gt;, and you&#x27;ll end up at &lt;a href=&quot;https:&#x2F;&#x2F;docs.rs&#x2F;pancurses&#x2F;0.16.0&#x2F;pancurses&#x2F;fn.initscr.html&quot;&gt;the &lt;code&gt;initscr&lt;&#x2F;code&gt;
function&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s try this out. Add the following line to the top of your &lt;code&gt;main&lt;&#x2F;code&gt;
function and compile:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; window = pancurses::initscr();
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Great! We&#x27;re going to use that returned &lt;code&gt;Window&lt;&#x2F;code&gt; struct to interact
with pancurses. Go ahead and &lt;a href=&quot;https:&#x2F;&#x2F;docs.rs&#x2F;pancurses&#x2F;0.16.0&#x2F;pancurses&#x2F;struct.Window.html&quot;&gt;open its
documentation&lt;&#x2F;a&gt;
and start browsing through.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;getting-the-frame-size&quot;&gt;Getting the frame size&lt;&#x2F;h2&gt;
&lt;p&gt;We&#x27;re currently just assigning an arbitrary frame size. Ideally, we&#x27;d
like to base it off of the actual terminal size. &lt;code&gt;Window&lt;&#x2F;code&gt; provides a
method for this,
&lt;a href=&quot;https:&#x2F;&#x2F;docs.rs&#x2F;pancurses&#x2F;0.16.0&#x2F;pancurses&#x2F;struct.Window.html#method.get_max_yx&quot;&gt;&lt;code&gt;get_max_yx&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;First, a step for you dear reader: modify the &lt;code&gt;new&lt;&#x2F;code&gt; method of &lt;code&gt;Game&lt;&#x2F;code&gt;
to take a &lt;code&gt;Frame&lt;&#x2F;code&gt; as input. Then, let&#x27;s jump back to our &lt;code&gt;main&lt;&#x2F;code&gt;. We
can capture the maximum &lt;code&gt;y&lt;&#x2F;code&gt; and &lt;code&gt;x&lt;&#x2F;code&gt; values with:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(max_y, max_x) = window.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;get_max_yx&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;();
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And now let&#x27;s create a &lt;code&gt;Frame&lt;&#x2F;code&gt; value out of those.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; frame = Frame {
    width: max_x,
    height: max_y,
};
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Then pass that value into &lt;code&gt;Game::new()&lt;&#x2F;code&gt;. This will only work if you already
modified &lt;code&gt;new&lt;&#x2F;code&gt; to take an extra &lt;code&gt;Frame&lt;&#x2F;code&gt; argument, go back a few paragraphs if
you missed that.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;Challenge&lt;&#x2F;strong&gt; Before you hit compile, can you figure out what error
message we&#x27;re about to encounter?&lt;&#x2F;p&gt;
&lt;p&gt;When I run &lt;code&gt;cargo build&lt;&#x2F;code&gt;, I get the following error:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;error[E0308]: mismatched types
   --&amp;gt; src&#x2F;main.rs:109:16
    |
109 |         width: max_x,
    |                ^^^^^ expected u32, found i32

error[E0308]: mismatched types
   --&amp;gt; src&#x2F;main.rs:110:17
    |
110 |         height: max_y,
    |                 ^^^^^ expected u32, found i32
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If you remember, &lt;code&gt;width&lt;&#x2F;code&gt; and &lt;code&gt;height&lt;&#x2F;code&gt; are &lt;code&gt;u32&lt;&#x2F;code&gt;s, but pancurses gives
us &lt;code&gt;i32&lt;&#x2F;code&gt;s for the maximum x and y values. How do we convert? One easy
way to handle this is with &lt;code&gt;as u32&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;width: max_x &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;as &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;,
height: max_y &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;as &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;,
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This is a totally unchecked cast. To demonstrate, try running this
program:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; i &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;i32&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;..&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;6&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;i32 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt; -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, i, i &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;as &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
    }
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;(Yes, there&#x27;s syntax in Rust for following a number with its type like
that, it&#x27;s really nice.)&lt;&#x2F;p&gt;
&lt;p&gt;Which results in:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;-5 -&amp;gt; 4294967291
-4 -&amp;gt; 4294967292
-3 -&amp;gt; 4294967293
-2 -&amp;gt; 4294967294
-1 -&amp;gt; 4294967295
0 -&amp;gt; 0
1 -&amp;gt; 1
2 -&amp;gt; 2
3 -&amp;gt; 3
4 -&amp;gt; 4
5 -&amp;gt; 5
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We&#x27;re going to just accept this bad behavior for now. Don&#x27;t worry,
it&#x27;ll get worse.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;carriage-return-and-the-border&quot;&gt;Carriage return and the border&lt;&#x2F;h2&gt;
&lt;p&gt;If you run this program, you&#x27;ll get some really weird looking
output. If you don&#x27;t believe me, go run it yourself now. I&#x27;ll wait.&lt;&#x2F;p&gt;
&lt;p&gt;The first problem is that our newlines aren&#x27;t working as expected. We
previously used &lt;code&gt;\n&lt;&#x2F;code&gt; to create a newline. However, with curses
enabled, this create a &lt;em&gt;line feed&lt;&#x2F;em&gt;, moving the cursor down one row,
without a &lt;em&gt;carriage return&lt;&#x2F;em&gt;, moving the cursor to the beginning of the
line. Go ahead and replace the &lt;code&gt;\n&lt;&#x2F;code&gt; usages with &lt;code&gt;\r\n&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;That&#x27;s better, but there&#x27;s still a problem: the grid doesn&#x27;t fit in
the terminal! That&#x27;s because we didn&#x27;t take the size of the border
into account. Try subtracing 4 from the maximum x and y values and see
if that fixes things. (Note: there&#x27;s a bit of a trick to where you put
the &lt;code&gt;- 4&lt;&#x2F;code&gt;. Think about what value you&#x27;re trying to cast.)&lt;&#x2F;p&gt;
&lt;h2 id=&quot;double-buffering&quot;&gt;Double buffering&lt;&#x2F;h2&gt;
&lt;p&gt;We still haven&#x27;t done double buffering, but we&#x27;re in a much better
position to do so now! The trick is to replace our &lt;code&gt;println!&lt;&#x2F;code&gt; call in
the &lt;code&gt;main&lt;&#x2F;code&gt; function&#x27;s &lt;code&gt;loop&lt;&#x2F;code&gt; with calls to &lt;code&gt;window&lt;&#x2F;code&gt; methods. If you
want the challenge, go read through the docs and try to figure out how
to make it work. One hint: you&#x27;ll need to revert the addition of the
&lt;code&gt;\r&lt;&#x2F;code&gt; carriage returns we added above.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;loop &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
    window.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;clear&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;&#x2F;&#x2F; get rid of old content
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;    window.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;printw&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(game.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;to_string&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;&#x2F;&#x2F; write to the buffer
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;    window.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;refresh&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;&#x2F;&#x2F; update the screen
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;    game.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;step&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;();
    std::thread::sleep(sleep_duration);
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Hurrah, we have double buffering in place! We can finally be done with
these bouncing balls.&lt;&#x2F;p&gt;
&lt;p&gt;Or can we?&lt;&#x2F;p&gt;
&lt;h2 id=&quot;exercise-1&quot;&gt;Exercise 1&lt;&#x2F;h2&gt;
&lt;p&gt;It&#x27;s time to get more comfortable with exploring API docs, and writing
some Rust code with less training wheels. There are a few problems
with our implementation:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;We don&#x27;t need to generate an entire string for the whole
grid. Instead, we can use some &lt;code&gt;Window&lt;&#x2F;code&gt; methods for moving the
cursor around and adding characters. Use that instead.&lt;&#x2F;li&gt;
&lt;li&gt;Drawing the border as we have is harder than it should be. There&#x27;s a
method on &lt;code&gt;Window&lt;&#x2F;code&gt; that will help significantly.&lt;&#x2F;li&gt;
&lt;li&gt;We have a number of assumptions about numbers, in particular that
the maximum x and y are positive, and large enough to hold the
ball&#x27;s starting position. Put in some sane limits: both values must
be at least 10.&lt;&#x2F;li&gt;
&lt;li&gt;More advanced, but: pancurses has some built in support for input
handling with timeouts. Instead of using &lt;code&gt;std::thread::sleep&lt;&#x2F;code&gt;, we
can set a timeout on input handling and add that to our main
loop. You can then also respond to two specific kinds of input:
&lt;ul&gt;
&lt;li&gt;Exit the program on a &lt;code&gt;q&lt;&#x2F;code&gt; press&lt;&#x2F;li&gt;
&lt;li&gt;Reset the game when the size of the window changes&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h2 id=&quot;more-iterators&quot;&gt;More iterators!&lt;&#x2F;h2&gt;
&lt;p&gt;Alright, I&#x27;m sufficiently bored with bouncing balls. Let&#x27;s talk about
something far more interesting: streaming data. Personally I found it
easiest to understand iterators by writing a few of them myself, so
that&#x27;s where we&#x27;re going to start.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s do some compiler-guided programming. We discussed previously
that there&#x27;s an &lt;code&gt;Iterator&lt;&#x2F;code&gt; trait. So a fair bet is that we need to
create a new data type and provide an implementation of the &lt;code&gt;Iterator&lt;&#x2F;code&gt;
trait. Let&#x27;s start off with something simple: an iterator that
produces no values at all.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;struct &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;Empty&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; i &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;in&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; Empty {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;panic!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;Wait, this shouldn&amp;#39;t happen!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
    }
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;All done!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;code&gt;panic!&lt;&#x2F;code&gt;ing is a way to cause the current thread to exit due to an
impossible situation. It&#x27;s &lt;em&gt;kind of&lt;&#x2F;em&gt; like runtime exceptions in other
languages, except you can&#x27;t recover from them. They are only
intended to be used for impossible situations.&lt;&#x2F;p&gt;
&lt;p&gt;OK, compile that and you&#x27;ll get a helpful error message:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;error[E0277]: the trait bound `Empty: std::iter::Iterator` is not satisfied
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Cool, let&#x27;s add an empty implementation (no pun intended):&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;impl &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;Iterator &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;Empty &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;More help from the compiler!&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;error[E0046]: not all trait items implemented, missing: `Item`, `next`
 --&amp;gt; foo.rs:3:1
  |
3 | impl Iterator for Empty {
  | ^^^^^^^^^^^^^^^^^^^^^^^ missing `Item`, `next` in implementation
  |
  = note: `Item` from trait: `type Item;`
  = note: `next` from trait: `fn(&amp;amp;mut Self) -&amp;gt; std::option::Option&amp;lt;&amp;lt;Self as std::iter::Iterator&amp;gt;::Item&amp;gt;`
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;So we need to provide two things: &lt;code&gt;Item&lt;&#x2F;code&gt; and &lt;code&gt;next&lt;&#x2F;code&gt;. &lt;code&gt;next&lt;&#x2F;code&gt; is a
function, so we&#x27;ll get to that in a second. What about that &lt;code&gt;type Item;&lt;&#x2F;code&gt;? It&#x27;s what we call an &lt;em&gt;associated type&lt;&#x2F;em&gt;. It tells us what type
of values will be produced by this iterator. Since we&#x27;re not producing
anything, we can use any type here. I&#x27;ll use a &lt;code&gt;u32&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;Item &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now we need to add in a &lt;code&gt;next&lt;&#x2F;code&gt;. Above, it gives the type of that
function as:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;fn(&amp;amp;mut Self) -&amp;gt; std::option::Option&amp;lt;&amp;lt;Self as std::iter::Iterator&amp;gt;::Item&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Let&#x27;s simplify this bit by bit. The &lt;em&gt;type&lt;&#x2F;em&gt; of the input is &lt;code&gt;&amp;amp;mut Self&lt;&#x2F;code&gt;. However, the correct syntax will be &lt;code&gt;&amp;amp;mut self&lt;&#x2F;code&gt;. Find that
confusing? Remember that &lt;code&gt;&amp;amp;mut self&lt;&#x2F;code&gt; is a shortcut for &lt;code&gt;self: &amp;amp;mut Self&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;fn(&amp;amp;mut self) -&amp;gt; std::option::Option&amp;lt;&amp;lt;Self as std::iter::Iterator&amp;gt;::Item&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Next, we can remove the module qualifiers for &lt;code&gt;Option&lt;&#x2F;code&gt; and &lt;code&gt;Iterator&lt;&#x2F;code&gt;,
since they&#x27;re already in our namespace:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;fn(&amp;amp;mut self) -&amp;gt; Option&amp;lt;&amp;lt;Self as Iterator&amp;gt;::Item&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This &lt;code&gt;Self as Iterator&lt;&#x2F;code&gt; is interesting. It&#x27;s saying &amp;quot;take the current
type, and look at its &lt;code&gt;Iterator&lt;&#x2F;code&gt; implementation. The reason we care
about specifying the implementation is because of what comes next:
&lt;code&gt;::Item&lt;&#x2F;code&gt;. What we&#x27;re &lt;em&gt;really&lt;&#x2F;em&gt; saying is &amp;quot;we want the &lt;code&gt;Item&lt;&#x2F;code&gt; associated
type related to the &lt;code&gt;Iterator&lt;&#x2F;code&gt; implementation.&amp;quot; It&#x27;s possible that
other traits will &lt;em&gt;also&lt;&#x2F;em&gt; have an associated type with the same name,
so this is an unambiguous way to refer to it.&lt;&#x2F;p&gt;
&lt;p&gt;Anyway, let&#x27;s see if this type signature works. Include the name of
the function and give it a dummy function body:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;next&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Option&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Self &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;as Iterator&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;gt;::Item&amp;gt; {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;unimplemented!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;code&gt;unimplemented!()&lt;&#x2F;code&gt; is a macro that uses &lt;code&gt;panic!&lt;&#x2F;code&gt; under the surface,
and is a convenient way to stub out implementations while under active
development. If you compile this, it will succeed. Yay! Then it
crashes at runtime due to the &lt;code&gt;unimplemented!&lt;&#x2F;code&gt;. Cool.&lt;&#x2F;p&gt;
&lt;p&gt;We can simplify the signature a bit by removing the &lt;code&gt;as Iterator&lt;&#x2F;code&gt;,
which isn&#x27;t necessary:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;next&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Option&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Self::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;Item&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You can &lt;em&gt;also&lt;&#x2F;em&gt; replace the &lt;code&gt;Self::Item&lt;&#x2F;code&gt; directly with &lt;code&gt;u32&lt;&#x2F;code&gt; if you
want. The upside is, in this case, it&#x27;s shorter. The downside is that
if you change the &lt;code&gt;Item&lt;&#x2F;code&gt; in the future, you&#x27;ll have to change it in
two places. This is really a subjective point, your call.&lt;&#x2F;p&gt;
&lt;p&gt;Alright, let&#x27;s provide an implementation. We return an &lt;code&gt;Option&lt;&#x2F;code&gt;, which
is an &lt;code&gt;enum&lt;&#x2F;code&gt; with two variants: &lt;code&gt;None&lt;&#x2F;code&gt; or &lt;code&gt;Some&lt;&#x2F;code&gt;. The former means &amp;quot;we
don&#x27;t have anything,&amp;quot; the latter means &amp;quot;we have something.&amp;quot; Given that
we&#x27;re implementing the empty iterator, returning &lt;code&gt;None&lt;&#x2F;code&gt; seems like the
right move:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;next&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Option&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;gt; {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;None
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And just like that, we have our first &lt;code&gt;Iterator&lt;&#x2F;code&gt; implementation!&lt;&#x2F;p&gt;
&lt;h2 id=&quot;exercise-2&quot;&gt;Exercise 2&lt;&#x2F;h2&gt;
&lt;p&gt;Create an iterator that infinitely produces the number 42. Here&#x27;s a
&lt;code&gt;main&lt;&#x2F;code&gt; function to test it out:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;&#x2F;&#x2F; only take 10 to avoid looping forever
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; i &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;in&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; TheAnswer.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;take&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;10&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;The answer to life, the universe, and everything is &lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, i);
    }
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#2aa198;&quot;&gt;All done!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;mutable-state&quot;&gt;Mutable state&lt;&#x2F;h2&gt;
&lt;p&gt;The signature for &lt;code&gt;next&lt;&#x2F;code&gt; involves taking a mutable reference to
&lt;code&gt;self&lt;&#x2F;code&gt;. Let&#x27;s use it! We&#x27;re going to create an iterator that counts
from 1 to 10. (If you&#x27;re feeling brave, try to implement it yourself
before reading my solution.)&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;struct &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;OneToTen&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;one_to_ten&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() -&amp;gt; OneToTen {
    OneToTen(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)
}

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;impl &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;Iterator &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;OneToTen &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;Item &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;

    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;next&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Option&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;gt; {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;0 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;10 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;None
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;} &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;else &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; res = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;0 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;+= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
            res
        }
    }
}

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; i &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;in one_to_ten&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, i);
    }
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;strong&gt;Exercise 3&lt;&#x2F;strong&gt; Create an iterator that produces the Fibonacci
sequence. (Anyone who&#x27;s heard me bemoaning this exercise in the
functional programming world should have a hearty chuckle right now.)&lt;&#x2F;p&gt;
&lt;h2 id=&quot;iterator-adapters&quot;&gt;Iterator adapters&lt;&#x2F;h2&gt;
&lt;p&gt;We can also write an iterator that will modify an existing
iterator. Let&#x27;s write &lt;code&gt;Doubler&lt;&#x2F;code&gt;, which will double the values produced
by a previous iterator. In order to make this work, we&#x27;re going to
capture the underlying iterator inside our new data type, which will
also necessitate parameterizing our &lt;code&gt;Doubler&lt;&#x2F;code&gt; data type on the
contained iterator:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;struct &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;Doubler&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;I&amp;gt; {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;iter&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: I,
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Let&#x27;s throw in a &lt;code&gt;main&lt;&#x2F;code&gt; function to show how this is used:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; orig_iter = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;..&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;11&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;&#x2F;&#x2F; array indices start at 1
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; doubled_iter = Doubler {
        iter: orig_iter,
    };
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; i &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;in&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; doubled_iter {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, i);
    }
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Great. If we compile this, we get an error about the missing
&lt;code&gt;Iterator&lt;&#x2F;code&gt; implementation. Let&#x27;s try to write something for this:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;impl &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;Iterator &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;Doubler &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;When we compile this, we get the error message:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;error[E0107]: wrong number of type arguments: expected 1, found 0
 --&amp;gt; foo.rs:5:19
  |
5 | impl Iterator for Doubler {
  |                   ^^^^^^^ expected 1 type argument
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;OK, that makes sense. &lt;code&gt;Doubler&lt;&#x2F;code&gt; itself isn&#x27;t a type until we give it
its parameters. Let&#x27;s do that:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;impl &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;Iterator &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;Doubler&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;I&amp;gt; {
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We get two error messages. Feel free to look at the second if you
like, but it&#x27;s not terribly helpful. (Recommendation: always look at
the first error message first and try to solve that before moving on.)
The first error message is:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;error[E0412]: cannot find type `I` in this scope
 --&amp;gt; foo.rs:5:27
  |
5 | impl Iterator for Doubler&amp;lt;I&amp;gt; {
  |                           ^ not found in this scope
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;OK, what&#x27;s happening? When we provide an implementation, we need to
state all of the type variables we want upfront. So let&#x27;s do this:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;impl&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;I&amp;gt; Iterator &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;Doubler&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;I&amp;gt; {
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;That may look a bit redundant (it did to me at first), but eventually
you&#x27;ll get to cases where things are more complicated and the two sets
of angle brackets don&#x27;t look identical. (For Haskellers or PureScript
users, this is kind of like requiring an explicit &lt;code&gt;forall&lt;&#x2F;code&gt;.)&lt;&#x2F;p&gt;
&lt;p&gt;Alright, now we&#x27;ve got something closer, and the compiler is upset
that we haven&#x27;t given &lt;code&gt;type Item&lt;&#x2F;code&gt; and &lt;code&gt;next&lt;&#x2F;code&gt;. Let&#x27;s go ahead and
return a &lt;code&gt;u32&lt;&#x2F;code&gt; again:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;Item &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;next&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Option&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;gt; {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;unimplemented!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The compiles and runs, and then crashes because of the
&lt;code&gt;unimplemented!&lt;&#x2F;code&gt;. Great, progress! The trick here is we want to ask
the underlying &lt;code&gt;Iterator&lt;&#x2F;code&gt; for its next value. We&#x27;ll do this with some
explicit pattern matching (functional programmers: yes, there&#x27;s a
&lt;code&gt;map&lt;&#x2F;code&gt; method on &lt;code&gt;Option&lt;&#x2F;code&gt; we could use here).&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;next&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Option&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;gt; {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;match &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;.iter.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;next&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;None =&amp;gt; None&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;,
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(x) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt; Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(x * &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;),
    }
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Nice enough, but when we compile it, we get told:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;error[E0599]: no method named `next` found for type `I` in the current scope
 --&amp;gt; foo.rs:8:25
  |
8 |         match self.iter.next() {
  |                         ^^^^
  |
  = help: items from traits can only be used if the trait is implemented and in scope
  = note: the following traits define an item `next`, perhaps you need to implement one of them:
          candidate #1: `std::iter::Iterator`
          candidate #2: `std::str::pattern::Searcher`
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The compiler knows that we &lt;em&gt;might&lt;&#x2F;em&gt; mean the &lt;code&gt;next&lt;&#x2F;code&gt; method from
&lt;code&gt;Iterator&lt;&#x2F;code&gt;. But it doesn&#x27;t use it. Why, you might ask? Because &lt;em&gt;we
never told the compiler that the implementation exists&lt;&#x2F;em&gt;! We need to
indicate that the &lt;code&gt;I&lt;&#x2F;code&gt; parameter must have an &lt;code&gt;Iterator&lt;&#x2F;code&gt;
implementation.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;impl&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;I: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Iterator&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;gt; Iterator &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;Doubler&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;I&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;That&#x27;s some new syntax, but pretty straightforward: &lt;code&gt;I&lt;&#x2F;code&gt; must have an
implementation of &lt;code&gt;Iterator&lt;&#x2F;code&gt;. Unfortunately, we&#x27;re not out of the
woods quite yet:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;error[E0369]: binary operation `*` cannot be applied to type `&amp;lt;I as std::iter::Iterator&amp;gt;::Item`
  --&amp;gt; foo.rs:10:29
   |
10 |             Some(x) =&amp;gt; Some(x * 2),
   |                             ^^^^^
   |
   = note: an implementation of `std::ops::Mul` might be missing for `&amp;lt;I as std::iter::Iterator&amp;gt;::Item`
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Let&#x27;s talk this through. &lt;code&gt;I&lt;&#x2F;code&gt; is some &lt;code&gt;Iterator&lt;&#x2F;code&gt;, we&#x27;ve already
established that. And we know that the &lt;code&gt;x&lt;&#x2F;code&gt; value we use in &lt;code&gt;x * 2&lt;&#x2F;code&gt;
will be of whatever type the &lt;code&gt;Item&lt;&#x2F;code&gt; associated type for that &lt;code&gt;I&lt;&#x2F;code&gt;
is. The problem is: we have no idea what it is, or whether or not it
can be multiplied!&lt;&#x2F;p&gt;
&lt;p&gt;We&#x27;ve already said we&#x27;re going to produce &lt;code&gt;u32&lt;&#x2F;code&gt;s here, so can we just
enforce that the &lt;code&gt;Item&lt;&#x2F;code&gt; is a &lt;code&gt;u32&lt;&#x2F;code&gt;? Yes!&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;impl&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;I: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Iterator&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;Item=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;gt;&amp;gt; Iterator &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;Doubler&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;I&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Whew, our code works!&lt;&#x2F;p&gt;
&lt;h2 id=&quot;alternative-syntax-where&quot;&gt;Alternative syntax: where&lt;&#x2F;h2&gt;
&lt;p&gt;As our constraints get more complex, shoving them all into the
parameters at the beginning of &lt;code&gt;impl&lt;&#x2F;code&gt; starts to feel crowded. You can
alternatively use &lt;code&gt;where&lt;&#x2F;code&gt; for this:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;impl&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;I&amp;gt; Iterator &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;Doubler&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;I&amp;gt;
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;    I: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Iterator&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;Item=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;There&#x27;s a subjective point at which people decide to make that
transition. Personally, I prefer consistency over brevity, and almost
always use &lt;code&gt;where&lt;&#x2F;code&gt;. Your mileage may vary. You should be aware of both
syntaxes for reading other people&#x27;s code.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;not-just-u32&quot;&gt;Not just u32&lt;&#x2F;h2&gt;
&lt;p&gt;It&#x27;s a bit weird that we&#x27;re tied down to &lt;code&gt;u32&lt;&#x2F;code&gt;s. What if we change our
&lt;code&gt;main&lt;&#x2F;code&gt; funciton to have:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; orig_iter = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;..&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;11&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u64&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We&#x27;ll get a compiler error:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;error[E0271]: type mismatch resolving `&amp;lt;std::ops::Range&amp;lt;u64&amp;gt; as std::iter::Iterator&amp;gt;::Item == u32`
  --&amp;gt; foo.rs:23:14
   |
23 |     for i in doubled_iter {
   |              ^^^^^^^^^^^^ expected u64, found u32
   |
   = note: expected type `u64`
              found type `u32`
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;It&#x27;s possible to relax this, but it starts to get more
complicated. But let&#x27;s do it! We&#x27;ll need to remove all of the
references to &lt;code&gt;u32&lt;&#x2F;code&gt; in our implementation. Here&#x27;s a first stab at
this:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;impl&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;I&amp;gt; Iterator &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;Doubler&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;I&amp;gt;
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;    I: Iterator
{
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;Item &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;???&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;next&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Option&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Self::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;Item&amp;gt; {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;match &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;.iter.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;next&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;None =&amp;gt; None&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;,
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(x) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt; Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(x * &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;),
        }
    }
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;I&#x27;ve replaced &lt;code&gt;Option&amp;lt;u32&amp;gt;&lt;&#x2F;code&gt; with &lt;code&gt;Option&amp;lt;Self::Item&amp;gt;&lt;&#x2F;code&gt;, and removed the
&lt;code&gt;&amp;lt;Item = u32&amp;gt;&lt;&#x2F;code&gt; on the &lt;code&gt;I: Iterator&lt;&#x2F;code&gt;. But what should we use for &lt;code&gt;type Item =&lt;&#x2F;code&gt;? We want it to be the same type as the underlying iterator&#x27;s
&lt;code&gt;Item&lt;&#x2F;code&gt;... so let&#x27;s just say that!&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;Item &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= I::Item;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;That works! We&#x27;re still not compiling though, because Rust doesn&#x27;t
know that it can perform multiplication on &lt;code&gt;I::Item&lt;&#x2F;code&gt;. Fortunately,
there&#x27;s a trait for things that can be multiplied called &lt;code&gt;Mul&lt;&#x2F;code&gt;. We can
add in:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;I: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Iterator&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;,
I::Item: std::ops::Mul,
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;New error message:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;error[E0308]: mismatched types
  --&amp;gt; foo.rs:14:29
   |
14 |             Some(x) =&amp;gt; Some(x * From::from(2u8)),
   |                             ^^^^^^^^^^^^^^^^^^^ expected std::iter::Iterator::Item, found std::ops::Mul::Output
   |
   = note: expected type `&amp;lt;I as std::iter::Iterator&amp;gt;::Item`
              found type `&amp;lt;&amp;lt;I as std::iter::Iterator&amp;gt;::Item as std::ops::Mul&amp;gt;::Output`
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;It turns out that &lt;code&gt;Mul&lt;&#x2F;code&gt; has an associated type for its output. This is
useful for expressing more complicated relationships at the type
level. For example, we can define types for &lt;code&gt;Force&lt;&#x2F;code&gt;, &lt;code&gt;Mass&lt;&#x2F;code&gt;, and
&lt;code&gt;Acceleration&lt;&#x2F;code&gt;, and then define a &lt;code&gt;Mul&lt;&#x2F;code&gt; implementation that says
&lt;code&gt;Mass&lt;&#x2F;code&gt; times &lt;code&gt;Acceleration&lt;&#x2F;code&gt; produces a &lt;code&gt;Force&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;That&#x27;s a wonderful feature, but it&#x27;s getting in our way here. We want
to say &amp;quot;the output should be the same as the item itself.&amp;quot; Fair
enough:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;impl&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;I&amp;gt; Iterator &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;Doubler&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;I&amp;gt;
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;    I: Iterator,
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;I::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;Item: std::ops::Mul&amp;lt;Output=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;I::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;Item&amp;gt;,
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And now:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;error[E0308]: mismatched types
  --&amp;gt; foo.rs:14:33
   |
14 |             Some(x) =&amp;gt; Some(x * 2),
   |                                 ^ expected associated type, found integral variable
   |
   = note: expected type `&amp;lt;I as std::iter::Iterator&amp;gt;::Item`
              found type `{integer}`
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Ugh. We have &lt;code&gt;2&lt;&#x2F;code&gt;, which can be &lt;em&gt;some&lt;&#x2F;em&gt; integral type. But we don&#x27;t know
that &lt;code&gt;Item&lt;&#x2F;code&gt; is an integral type! I&#x27;m not aware of a way to give a
constraint that allows this code to work (if someone knows better,
please let me know and I&#x27;ll update this text). One trick that &lt;em&gt;does&lt;&#x2F;em&gt;
work is to upcast from a &lt;code&gt;u8&lt;&#x2F;code&gt; using the &lt;code&gt;From&lt;&#x2F;code&gt; trait, which performs
safe numeric conversions (which cannot over or underflow):&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;impl&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;I&amp;gt; Iterator &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;Doubler&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;I&amp;gt;
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;    I: Iterator,
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;I::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;Item: std::ops::Mul&amp;lt;Output=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;I::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;Item&amp;gt; + &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;From&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u8&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;gt;,
{
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;Item &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;= I::Item;
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;next&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Option&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;Self::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;Item&amp;gt; {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;match &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;.iter.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;next&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;None =&amp;gt; None&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;,
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(x) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt; Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(x * &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;From&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;::from(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u8&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;)),
        }
    }
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Whew, that&#x27;s finally over!&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;Exercise 4&lt;&#x2F;strong&gt; An easier approach to the above is to use &lt;code&gt;x + x&lt;&#x2F;code&gt;
instead of &lt;code&gt;x * 2&lt;&#x2F;code&gt;. Rewrite the iterator to do that. One hint: the
compiler won&#x27;t know that it&#x27;s allowed to make copies of that type
unless you tell it via the appropriately-named trait.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;recap&quot;&gt;Recap&lt;&#x2F;h2&gt;
&lt;p&gt;That was a lot! But hopefully it gets the idea across of how iterators
work. We can write highly generic adapters that work with many kinds
of input. You could apply &lt;code&gt;Doubler&lt;&#x2F;code&gt; to the range iterator as we
did. You could apply it to the &lt;code&gt;Empty&lt;&#x2F;code&gt; we defined earlier. Or to
dozens of other things.&lt;&#x2F;p&gt;
&lt;p&gt;You may notice that the types of these iterators seem to grow as you
add more things to them. That&#x27;s absolutely true, and it&#x27;s also by
design. By representing the full type statically, the compiler is able
to see all of the actions that are being performed in a pipeline of
iterator operations, and optimize things very well.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;more-idiomatic&quot;&gt;More idiomatic&lt;&#x2F;h2&gt;
&lt;p&gt;The &lt;code&gt;Doubler&lt;&#x2F;code&gt; we wrote was not idiomatic at all. Let&#x27;s do it the
&lt;em&gt;real&lt;&#x2F;em&gt; way:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; i &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;..&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;11&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;map&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;x&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| x * &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, i);
    }
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The &lt;code&gt;Iterator&lt;&#x2F;code&gt; trait includes many helper methods, so you can chain up
large sets of actions like that:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; i &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;..&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;11&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;skip&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;map&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;x&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| x + &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;filter&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;x&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;| x % &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;2 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;== &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, i);
    }
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You could of course write something like this as a for loop in C&#x2F;C++,
but:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;It would be harder to see the logic&lt;&#x2F;li&gt;
&lt;li&gt;It would be harder to extend in the future&lt;&#x2F;li&gt;
&lt;li&gt;It wouldn&#x27;t be faster: the Rust compiler will optimize case like
this down to the same hand-rolled loop you may write&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h2 id=&quot;collecting-results&quot;&gt;Collecting results&lt;&#x2F;h2&gt;
&lt;p&gt;You can collect the results from an iterator in a vector:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; my_vec: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Vec&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;gt; = (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;..&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;11&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;collect&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;();
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{:?}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, my_vec);
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The type annotation is necessary here, since &lt;code&gt;collect&lt;&#x2F;code&gt; can work on
many different datatypes.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;Exercise 5&lt;&#x2F;strong&gt; Use the &lt;code&gt;fold&lt;&#x2F;code&gt; method to get the sum of the numbers
from 1 to 10. Extra credit: write a helper &lt;code&gt;sum&lt;&#x2F;code&gt; function.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;next-time&quot;&gt;Next time&lt;&#x2F;h2&gt;
&lt;p&gt;We&#x27;ve been dancing around closures for quite a while now, including in
that last exercise. We now know enough to attack them head on. We&#x27;ll
do so next time.&lt;&#x2F;p&gt;
&lt;p&gt;You&#x27;re now at a point where you can write some real Rust applications
and start cutting your teeth. I&#x27;d recommend finding a few play tasks
you want to experiment with. &lt;a href=&quot;https:&#x2F;&#x2F;exercism.io&quot;&gt;Exercism&lt;&#x2F;a&gt; may be a
good choice if you&#x27;re looking for some challenges.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;www.fpcomplete.com&#x2F;rust&quot;&gt;Rust at FP Complete&lt;&#x2F;a&gt; | &lt;a href=&quot;https:&#x2F;&#x2F;www.snoyman.com&#x2F;blog&#x2F;2018&#x2F;10&#x2F;introducing-rust-crash-course&quot;&gt;Introduction&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
</description>
        </item>
        <item>
            <title>Iterators and Errors - Rust Crash Course lesson 3 - exercise solutions</title>
            <pubDate>Wed, 07 Nov 2018 00:00:00 +0000</pubDate>
            <link>https://www.snoyman.com/blog/2018/11/rust-crash-course-03-iterators-and-errors-solutions/</link>
            <guid>https://www.snoyman.com/blog/2018/11/rust-crash-course-03-iterators-and-errors-solutions/</guid>
            <description>&lt;p&gt;&lt;strong&gt;Heads up&lt;&#x2F;strong&gt; This blog post series has been updated and published as an eBook by FP Complete. I&#x27;d recommend reading that version instead of these posts. If you&#x27;re interested, please check out the &lt;a href=&quot;https:&#x2F;&#x2F;www.fpcomplete.com&#x2F;rust&#x2F;crash-course&#x2F;&quot;&gt;Rust Crash Course eBook&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Below are the solutions to the exercises from the last Rust Crash
Course lesson, &amp;quot;Iterators and Errors.&amp;quot;&lt;&#x2F;p&gt;
&lt;p&gt;This post is part of a series based on &lt;a href=&quot;https:&#x2F;&#x2F;www.fpcomplete.com&#x2F;rust&quot;&gt;teaching Rust at FP
Complete&lt;&#x2F;a&gt;. If you&#x27;re reading this post outside
of the blog, you can find links to all posts in the series &lt;a href=&quot;https:&#x2F;&#x2F;www.snoyman.com&#x2F;blog&#x2F;2018&#x2F;10&#x2F;introducing-rust-crash-course&quot;&gt;at the top of the
introduction
post&lt;&#x2F;a&gt;. You
can also &lt;a href=&quot;https:&#x2F;&#x2F;www.snoyman.com&#x2F;feed&#x2F;rust-crash-course&quot;&gt;subscribe to the RSS
feed&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;exercise-1&quot;&gt;Exercise 1&lt;&#x2F;h2&gt;
&lt;p&gt;The trick here is to &amp;quot;wrap&amp;quot; the &lt;code&gt;Args&lt;&#x2F;code&gt; data type with the &lt;code&gt;Skip&lt;&#x2F;code&gt; data
type:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;std::env::{args, Args};
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;std::iter::Skip;

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; args: Skip&amp;lt;Args&amp;gt; = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;args&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;().&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;skip&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; arg &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;in&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; args {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, arg);
    }
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You may have noticed that we didn&#x27;t need to mark &lt;code&gt;args&lt;&#x2F;code&gt; as
mutable. That&#x27;s because we are moving the &lt;code&gt;args&lt;&#x2F;code&gt; value into the &lt;code&gt;for&lt;&#x2F;code&gt;
loop, meaning that any mutation to it by the &lt;code&gt;for&lt;&#x2F;code&gt; loop cannot be seen
in the &lt;code&gt;main&lt;&#x2F;code&gt; function.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;exercise-2&quot;&gt;Exercise 2&lt;&#x2F;h2&gt;
&lt;p&gt;When we call &lt;code&gt;parse&lt;&#x2F;code&gt; in the context of that example, type inference
tells us that the &lt;code&gt;width&lt;&#x2F;code&gt; and &lt;code&gt;height&lt;&#x2F;code&gt; results must be &lt;code&gt;u32&lt;&#x2F;code&gt;s, since
they are used as fields in &lt;code&gt;Frame&lt;&#x2F;code&gt;. Rust is able to determine what
implementation to use based on the needed return type. Cool!&lt;&#x2F;p&gt;
&lt;p&gt;Yet again, Haskellers are rolling their eyes and saying &amp;quot;old news.&amp;quot;&lt;&#x2F;p&gt;
&lt;h2 id=&quot;exercise-3&quot;&gt;Exercise 3&lt;&#x2F;h2&gt;
&lt;p&gt;Complete source file:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;#[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;derive&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(Debug)]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;struct &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;Frame &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;width&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;,
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;height&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;,
}

#[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;derive&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(Debug)]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;enum &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;ParseError &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
    TooFewArgs,
    TooManyArgs,
    InvalidInteger(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;),
}

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;struct &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;ParseArgs&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(std::env::Args);

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;impl &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;ParseArgs &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() -&amp;gt; ParseArgs {
        ParseArgs(std::env::args())
    }


    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;require_arg&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Result&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, ParseError&amp;gt; {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;match &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;0.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;next&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;None =&amp;gt; Err&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(ParseError::TooFewArgs),
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(s) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt; Ok&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(s),
        }
    }

    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;require_no_args&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Result&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;(), ParseError&amp;gt; {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;match &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d33682;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;0.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;next&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Some&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt; Err&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(ParseError::TooManyArgs),
            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;None =&amp;gt; Ok&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(()),
        }
    }
}

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;parse_u32&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;s&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;: String) -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Result&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, ParseError&amp;gt; {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;match&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; s.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;parse&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Err&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt; Err&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(ParseError::InvalidInteger(s)),
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Ok&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(x) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;=&amp;gt; Ok&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(x),
    }
}

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;parse_args&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Result&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;Frame, ParseError&amp;gt; {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; args = ParseArgs::new();

    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#93a1a1;&quot;&gt;&#x2F;&#x2F; skip the command name
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;    args.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;require_arg&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;

    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; width_str = args.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;require_arg&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; height_str = args.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;require_arg&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
    args.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;require_no_args&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; width = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;parse_u32&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(width_str)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; height = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;parse_u32&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(height_str)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;

    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Ok&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(Frame { width, height })
}

&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{:?}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;parse_args&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;());
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;exercise-4&quot;&gt;Exercise 4&lt;&#x2F;h2&gt;
&lt;p&gt;We want to ensure a minimum size for the width and height. First,
let&#x27;s add two more variants to the &lt;code&gt;ParseError&lt;&#x2F;code&gt; enum:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;WidthTooSmall(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;),
HeightTooSmall(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;),
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Then add the following to the &lt;code&gt;parse_args&lt;&#x2F;code&gt; function, just before the &lt;code&gt;Ok&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#859900;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; width &amp;lt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;2 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;return Err&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(WidthTooSmall(width));
}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; height &amp;lt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;2 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;return Err&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(HeightTooSmall(height));
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;exercise-5&quot;&gt;Exercise 5&lt;&#x2F;h2&gt;
&lt;p&gt;This is another perfect time to pull out our trailing question mark!&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#fdf6e3;&quot;&gt;
&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b58900;&quot;&gt;main &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;() -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;Result&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;&amp;lt;(), self::parse_args::ParseError&amp;gt; {
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; frame = parse_args::parse_args()&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;;
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#586e75;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; game = Game::new(frame);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#268bd2;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt; sleep_duration = std::time::Duration::from_millis(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6c71c4;&quot;&gt;33&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;);
    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;loop &lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;{
        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;println!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#cb4b16;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#839496;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;, game);
        game.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#859900;&quot;&gt;step&lt;&#x2F;span&gt;&lt;span style=&quot;color:#657b83;&quot;&gt;();
        std::thread::sleep(sleep_duration);
    }
}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;www.fpcomplete.com&#x2F;rust&quot;&gt;Rust at FP Complete&lt;&#x2F;a&gt; | &lt;a href=&quot;https:&#x2F;&#x2F;www.snoyman.com&#x2F;blog&#x2F;2018&#x2F;10&#x2F;introducing-rust-crash-course&quot;&gt;Introduction&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
</description>
        </item>
    </channel>
</rss>
