<?xml version="1.0" encoding="UTF-8"?>
<rss  xmlns:atom="http://www.w3.org/2005/Atom" 
      xmlns:media="http://search.yahoo.com/mrss/" 
      xmlns:content="http://purl.org/rss/1.0/modules/content/" 
      xmlns:dc="http://purl.org/dc/elements/1.1/" 
      version="2.0">
<channel>
<title>midwitsanonymous</title>
<link>https://midwitsanonymous.com/</link>
<atom:link href="https://midwitsanonymous.com/index.xml" rel="self" type="application/rss+xml"/>
<description></description>
<generator>quarto-1.9.38</generator>
<lastBuildDate>Fri, 05 Jun 2026 00:00:00 GMT</lastBuildDate>
<item>
  <title>Day 2:</title>
  <dc:creator>The MidWit</dc:creator>
  <link>https://midwitsanonymous.com/aoc/2015/day_2/day_2.html</link>
  <description><![CDATA[ 





<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>You can find the text of the puzzle <a href="../../../aoc/2015/day_2/puzzle.html">here</a>.</p>
</div>
</div>
<section id="overview" class="level1">
<h1>Overview</h1>
<p>The basic aim here is to take in a series of parcel dimensions (stored as strings of numbers separated by “x”) and use them to calculate the total amount of wrapping paper (part 1) and ribbon for bows (part 2).</p>
</section>
<section id="concepts" class="level1">
<h1>Concepts</h1>
<ul>
<li>using my own modules (including the <code>aoc_common</code> code from <a href="../../../aoc/2015/day_1/day_1_sidequest.html">SideQuest 1</a>)</li>
<li>closures (getting totals)</li>
<li>parsing Strings to <code>i32</code></li>
<li>returning from <code>main</code> (like I know what that means).</li>
</ul>
<section id="limitations---modules-and-functions-and-tests-oh-my" class="level2">
<h2 class="anchored" data-anchor-id="limitations---modules-and-functions-and-tests-oh-my">Limitations - Modules and Functions and tests (Oh My!)</h2>
<p>As my aim isn’t to just solve the problems, but to work towards a <em>polished</em> solution and so I’m going to try to write as little code in <code>main.rs</code> as possible, and to start everything with tests (burgh).</p>
</section>
<section id="set-up" class="level2">
<h2 class="anchored" data-anchor-id="set-up">Set up</h2>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="make-wrapping-mod" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="make-wrapping-mod-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># I'm working on NixOs</span></span>
<span id="make-wrapping-mod-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># move into my root</span></span>
<span id="make-wrapping-mod-3"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">$</span> cd aoc</span>
<span id="make-wrapping-mod-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># create a new folder with cargo</span></span>
<span id="make-wrapping-mod-5"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">$</span> cargo new day_2</span>
<span id="make-wrapping-mod-6"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">//</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--</span> snip <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--</span> just output showing the project getting made</span>
<span id="make-wrapping-mod-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># create the parcel.rs file</span></span>
<span id="make-wrapping-mod-8"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">$</span> touch day_2/src/parcel.rs</span></code></pre></div></div>
<p>So that gives us most of what we need in the way of set up, but the last things I need to do are:</p>
<ul>
<li>add my <a href="../../../aoc/2015/day_1/day_1_sidequest.html">aoc_common</a> code as a dependency in <code>day_2/Cargo.toml</code><sup>1</sup></li>
</ul>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="proj-toml" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="proj-toml-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">[package]</span></span>
<span id="proj-toml-2"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">name</span> = <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"day_2"</span></span>
<span id="proj-toml-3"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">version</span> = <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"0.1.0"</span></span>
<span id="proj-toml-4"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">edition</span> = <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2024"</span></span>
<span id="proj-toml-5"></span>
<span id="proj-toml-6"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">[dependencies]</span></span>
<span id="proj-toml-7"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">aoc_common</span> = {path = <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"../aoc_common"</span>} <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># this line</span></span></code></pre></div></div>
<ul>
<li>add <code>day_2</code> to the workspace toml file</li>
</ul>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="root-toml" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="root-toml-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">[workspace]</span></span>
<span id="root-toml-2"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">members</span> = [</span>
<span id="root-toml-3">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"aoc_common"</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">,</span></span>
<span id="root-toml-4">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"day_2"</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">,</span></span>
<span id="root-toml-5">  <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">]</span></span></code></pre></div></div>
</section>
</section>
<section id="part-1" class="level1">
<h1>Part 1</h1>
<section id="formulae" class="level2">
<h2 class="anchored" data-anchor-id="formulae">Formulae</h2>
<p>This puzzle is about wrapping presents (which I’m calling parcels from here on out), and to get enough paper (in this economy?!) there are a few things we need to do:</p>
<ul>
<li><p>Calculate its surface area</p></li>
<li><p><code>2 * (length * width) + 2 * (length * height) + 2 * (width * height)</code></p></li>
<li><p>Calculate the area of the smallest face</p>
<ul>
<li>find the smallest side first</li>
<li><code>edge_1 * edge_2</code></li>
</ul></li>
<li><p>Add the two products together</p></li>
</ul>
<p>And finally we need to get the result of combining all those calculations.</p>
<p><strong>Rolls up sleeves</strong>: <em><em>Let’s kick this pig</em></em></p>
</section>
<section id="parcel-struct" class="level2">
<h2 class="anchored" data-anchor-id="parcel-struct">Parcel <code>Struct</code></h2>
<p>Ok, so I’m going to try to put everything into a <code>Parcel Struct</code> inside <code>parcel.rs</code> and as per my TDD goal I’m going to start with a test.</p>
<section id="test_new" class="level3">
<h3 class="anchored" data-anchor-id="test_new"><code>test_new()</code></h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="parc-new-test" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="parc-new-test-1"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">#[</span>cfg<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">(</span>test<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">)]</span></span>
<span id="parc-new-test-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">mod</span> tests <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="parc-new-test-3">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">use</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">super</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*;</span></span>
<span id="parc-new-test-4"></span>
<span id="parc-new-test-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">#[</span>test<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">]</span></span>
<span id="parc-new-test-6">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> test_new() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="parc-new-test-7">        <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">assert_eq!</span>(</span>
<span id="parc-new-test-8">                Parcel <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span> </span>
<span id="parc-new-test-9">                    l <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="parc-new-test-10">                    w <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="parc-new-test-11">                    h <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="parc-new-test-12">                <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">},</span></span>
<span id="parc-new-test-13">                <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">Parcel::</span>new(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="parc-new-test-14">            )<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="parc-new-test-15">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="parc-new-test-16"></span>
<span id="parc-new-test-17">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<p>And we have our first test. It just lets me test a <code>new()</code> associated method. It also contains the code to define the <code>Struct</code>, I just need to yank it up into a definition and set the field types.</p>
</section>
<section id="implement-new" class="level3">
<h3 class="anchored" data-anchor-id="implement-new">Implement <code>new()</code></h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="parc-new-def" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="parc-new-def-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// define the struct</span></span>
<span id="parc-new-def-2"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">#[</span>derive<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">(</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Debug</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">PartialEq</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">)]</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">//deriving needed traits</span></span>
<span id="parc-new-def-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">pub</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">struct</span> Parcel <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// pub from the get go this time!</span></span>
<span id="parc-new-def-4">            l <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i32</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="parc-new-def-5">            w <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i32</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="parc-new-def-6">            h <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i32</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="parc-new-def-7">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="parc-new-def-8"></span>
<span id="parc-new-def-9"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">impl</span> Parcel <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// implement the `new()` function</span></span>
<span id="parc-new-def-10">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">pub</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> new(l<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i32</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> w<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i32</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i32</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> Parcel <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="parc-new-def-11">        Parcel <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span> l<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> w<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> h <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="parc-new-def-12">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="parc-new-def-13"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="test-1" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="test-1-1">    <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Finished</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">test</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span> profile [unoptimized + debuginfo] target<span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">(</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">s</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">)</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">in</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">0.21s</span></span>
<span id="test-1-2">     <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Running</span> unittests src/main.rs <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">(</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">/home/sp1d3r-z3r0/MyProjects-tmp/midwitsanonymous/scratch/target/debug/deps/day_2-f26cfbed71fb1d31</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">)</span></span>
<span id="test-1-3"></span>
<span id="test-1-4"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">running</span> 1 test</span>
<span id="test-1-5"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">test</span> parcel::tests::test_new ... ok</span>
<span id="test-1-6"></span>
<span id="test-1-7"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">test</span> result: ok. 1 passed<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">;</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">0</span> failed<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">;</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">0</span> ignored<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">;</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">0</span> measured<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">;</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">0</span> filtered out<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">;</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">finished</span> in 0.00s</span></code></pre></div></div>
<p>There it is; the first <strong>Boom!</strong> It’s not, like, <em>earth-shaking</em> because this isn’t exactly rocket surgery and I was pretty sure this would work. But, I’m not one to pass up a “boom” when I see one.</p>
</section>
</section>
<section id="calculations" class="level2">
<h2 class="anchored" data-anchor-id="calculations">Makin’ calculations</h2>
<p>We know from earlier that we have a few different calculations we need to perform to come up with our final answer. Rather than working my way <em>in</em> from the whole <code>Parcel</code> I’m going to work <em>out</em> from the area of the smallest side first.</p>
<section id="test_smallest_area" class="level3">
<h3 class="anchored" data-anchor-id="test_smallest_area"><code>test_smallest_area()</code></h3>
<p>Let’s write the test</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="parc-smallest-test" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="parc-smallest-test-1">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">#[</span>test<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">]</span></span>
<span id="parc-smallest-test-2">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> test_smallest_area() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="parc-smallest-test-3">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// make a new parce</span></span>
<span id="parc-smallest-test-4">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> test_parcel <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">Parcel::</span>new(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="parc-smallest-test-5">        <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">assert_eq!</span>(test_parcel<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>smallest_area()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span></span>
<span id="parc-smallest-test-6">            )<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="parc-smallest-test-7">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<p>And add it to our implementation</p>
</section>
<section id="perfectly-fine-catastrophic-smallest_area" class="level3">
<h3 class="anchored" data-anchor-id="perfectly-fine-catastrophic-smallest_area"><del>Perfectly fine</del> Catastrophic <code>smallest_area()</code></h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="parc-smallest-impl-1" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="parc-smallest-impl-1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// -- snip -- </span></span>
<span id="parc-smallest-impl-1-2">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">pub</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> smallest_area(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i32</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="parc-smallest-impl-1-3">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> s1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>l <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>w<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="parc-smallest-impl-1-4">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> s2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>l <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="parc-smallest-impl-1-5">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> s3 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>w <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="parc-smallest-impl-1-6">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> smallest <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="parc-smallest-impl-1-7">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> s1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> s2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> s1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> s3 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="parc-smallest-impl-1-8">                s1</span>
<span id="parc-smallest-impl-1-9">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> s2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> s1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> s2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> s3 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="parc-smallest-impl-1-10">                s2</span>
<span id="parc-smallest-impl-1-11">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="parc-smallest-impl-1-12">                s3</span>
<span id="parc-smallest-impl-1-13">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="parc-smallest-impl-1-14">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">};</span></span>
<span id="parc-smallest-impl-1-15">        smallest</span>
<span id="parc-smallest-impl-1-16">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<p>And there we have it, a perfectly fine implementation of the <code>smallest_area() function</code>. Yes indeedy!… Nothing wrong with this… probably could skip the test… but you know what, let’s just run it, for the look of the thing.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="smallest-impl-test-1" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="smallest-impl-test-1-1">  <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">--</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> day_2/src/parcel.rs:19:19</span>
<span id="smallest-impl-test-1-2">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span></span>
<span id="smallest-impl-test-1-3"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">19</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>             <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">s1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>= s2 <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">&amp;</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">s1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>= s3 {</span>
<span id="smallest-impl-test-1-4">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                   <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">^^</span>         ^^</span>
<span id="smallest-impl-test-1-5">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span></span>
<span id="smallest-impl-test-1-6"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">help:</span> split the comparison into two</span>
<span id="smallest-impl-test-1-7">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span></span>
<span id="smallest-impl-test-1-8"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">19</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>             <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">s1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>= s2 <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">&amp;</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">s1</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">&amp;&amp;</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">s2</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">&amp;</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">s1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>= s3 {</span>
<span id="smallest-impl-test-1-9">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                              <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">++++++++++</span></span>
<span id="smallest-impl-test-1-10"></span>
<span id="smallest-impl-test-1-11"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">error:</span> comparison operators cannot be chained</span>
<span id="smallest-impl-test-1-12">  <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">--</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> day_2/src/parcel.rs:21:26</span>
<span id="smallest-impl-test-1-13">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span></span>
<span id="smallest-impl-test-1-14"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">21</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>             <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">}</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">s2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>= s1 <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">&amp;</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">s2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>= s3 {</span>
<span id="smallest-impl-test-1-15">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                          <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">^^</span>         ^^</span>
<span id="smallest-impl-test-1-16">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span></span>
<span id="smallest-impl-test-1-17"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">help:</span> split the comparison into two</span>
<span id="smallest-impl-test-1-18">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span></span>
<span id="smallest-impl-test-1-19"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">21</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>             <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">}</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">s2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>= s1 <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">&amp;</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">s2</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">&amp;&amp;</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">s1</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">&amp;</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">s2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>= s3 {</span>
<span id="smallest-impl-test-1-20">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                                     <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">++++++++++</span></span>
<span id="smallest-impl-test-1-21"></span>
<span id="smallest-impl-test-1-22"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">error</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>E0308<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">:</span> mismatched types</span>
<span id="smallest-impl-test-1-23">  <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">--</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> day_2/src/parcel.rs:19:33</span>
<span id="smallest-impl-test-1-24">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span></span>
<span id="smallest-impl-test-1-25"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">19</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>             <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">s1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>= s2 <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">&amp;</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">s1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>= s3 {</span>
<span id="smallest-impl-test-1-26">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">-------------</span>    ^^ expected <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">bool</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span>, found <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">i32</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span></span>
<span id="smallest-impl-test-1-27">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span></span>
<span id="smallest-impl-test-1-28">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">expected</span> because this is <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">bool</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span></span>
<span id="smallest-impl-test-1-29"></span>
<span id="smallest-impl-test-1-30"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">error</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>E0308<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">:</span> mismatched types</span>
<span id="smallest-impl-test-1-31">  <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">--</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> day_2/src/parcel.rs:21:40</span>
<span id="smallest-impl-test-1-32">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span></span>
<span id="smallest-impl-test-1-33"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">21</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>             <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">}</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">s2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>= s1 <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">&amp;</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">s2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>= s3 {</span>
<span id="smallest-impl-test-1-34">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                       <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">-------------</span>    ^^ expected <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">bool</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span>, found <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">i32</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span></span>
<span id="smallest-impl-test-1-35">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                       <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span></span>
<span id="smallest-impl-test-1-36">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                       <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">expected</span> because this is <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">bool</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span></span>
<span id="smallest-impl-test-1-37"></span>
<span id="smallest-impl-test-1-38"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">For</span> more information about this error, try <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">rustc</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--explain</span> E0308<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span>.</span>
<span id="smallest-impl-test-1-39"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">error:</span> could not compile <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">day_2</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">(</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">bin</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"day_2"</span> test<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">)</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">due</span> to 4 previous errors</span></code></pre></div></div>
<p>Well… <em>That’s fine</em></p>
<p><img src="https://media1.tenor.com/m/pfe7OvZiBG4AAAAC/george-michael.gif" class="img-fluid"></p>
<p>Oh, the hubris! Like Daedalus before me, certain of another <em>Boom</em>. Yet instead, I find myself the perpetrator of a (<em>say it with me folks</em>) <strong>Rust Rookie Mistake!</strong></p>
<p>Again, <code>Friend Computer</code> is benevolent, showing me how to fix the error, but it’s worth taking a second for me to really mark that. <strong>To the <a href="https://doc.rust-lang.org/book/appendix-02-operators.html">documenatrium!!</a></strong>.</p>
</section>
</section>
<section id="and-and-and" class="level2">
<h2 class="anchored" data-anchor-id="and-and-and">And and <em>And</em></h2>
<section id="section" class="level3">
<h3 class="anchored" data-anchor-id="section"><code>&amp;</code></h3>
<p>According to the docs the <code>&amp;</code> symbol, as well as indicating a ‘borrow’ represents the “Bitwise AND” of two objects. Now I <em>def’ine’itly</em> ain’t the person to explain this but as I understand it <sup>2</sup>, this performs the “AND” operations on the binary representation of 2 objects.</p>
<p>So let’s imagine that we have 2 sets of 4 bits</p>
<table class="caption-top table">
<thead>
<tr class="header">
<th>column</th>
<th>1</th>
<th>2</th>
<th>3</th>
<th>4</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>row a</td>
<td>0</td>
<td>0</td>
<td>1</td>
<td>1</td>
</tr>
<tr class="even">
<td>row b</td>
<td>0</td>
<td>1</td>
<td>1</td>
<td>0</td>
</tr>
</tbody>
</table>
<p>Reading down the columns we compare the 2 values:</p>
<ul>
<li>If they’re both 1s -&gt; we get 1</li>
<li>If they’re both 0s -&gt; we get 0</li>
<li>If they’re different values -&gt; 0</li>
</ul>
<p>So with the table above we’d get the value</p>
<table class="caption-top table">
<thead>
<tr class="header">
<th>column</th>
<th>1</th>
<th>2</th>
<th>3</th>
<th>4</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>row a</td>
<td>0</td>
<td>0</td>
<td>1</td>
<td>1</td>
</tr>
<tr class="even">
<td>row b</td>
<td>0</td>
<td>1</td>
<td>1</td>
<td>0</td>
</tr>
<tr class="odd">
<td>result</td>
<td>0</td>
<td>0</td>
<td>1</td>
<td>0</td>
</tr>
</tbody>
</table>
<p>Great, that’s a good thing for me to have learned… (I guess… <em>kicks dust</em>)</p>
</section>
<section id="section-1" class="level3">
<h3 class="anchored" data-anchor-id="section-1"><code>&amp;&amp;</code></h3>
<p>As <code>Friend Computer</code> said up in our last Socratic dialogue, what I was really looking for was the “Short-circuiting logical AND” which allows us to combine multiple <code>boolean</code> conditions so that <em>both</em> must be true in order to move forward.</p>
<p>Which is what I naively thought I was doing above… in the before times. The Halcyon days if you will.</p>
<p><strong>But why does this matter?</strong><sup>3</sup></p>
<p>Believe it or not, this is actually the first time I’ve encountered the coding equivalent of ‘PEMDAS’<sup>4</sup>. Again, I don’t know the ins-and-outs of it but essentially the ‘Bitwise’ <code>&amp;</code> is higher in the hierarchy of operations than the <code>comparison operators</code> so where I thought I was writing</p>
<ul>
<li><code>if x &lt;= y and x &lt;= z</code></li>
</ul>
<p>I was actually writing</p>
<ul>
<li><code>if x &lt;= (y and x) &lt;= z</code></li>
</ul>
<p>Meaning the <code>(x and y)</code> happens first, which creates two further problems here (I think):</p>
<ul>
<li><code>x &lt;= (x and y)</code> produces a <code>bool</code> while <code>z</code> is an <code>i32</code>: we can’t compare those types</li>
<li>unlike python, Rust doesn’t let us chain <code>comparison operators</code> like that…</li>
</ul>
<p>And all because I wanted to compare some numbers! Like a sweet summer child.</p>
<p>So… (cough) let’s do that</p>
</section>
<section id="fixing-smallest_area" class="level3">
<h3 class="anchored" data-anchor-id="fixing-smallest_area">Fixing <code>smallest_area()</code></h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="smallest_area-2" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="smallest_area-2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// -- snip --</span></span>
<span id="smallest_area-2-2">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> smallest <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="smallest_area-2-3">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> s1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> s2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;&amp;</span> s1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> s3 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="smallest_area-2-4">                s1</span>
<span id="smallest_area-2-5">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> s2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> s1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;&amp;</span> s2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> s3 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="smallest_area-2-6">                s2</span>
<span id="smallest_area-2-7">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="smallest_area-2-8">                s3</span>
<span id="smallest_area-2-9">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="smallest_area-2-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// -- snip --</span></span></code></pre></div></div>
<p>Breathe deeply and let’s test it</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="parc-smallest-out-correct" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="parc-smallest-out-correct-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">running</span> 2 tests</span>
<span id="parc-smallest-out-correct-2"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">test</span> parcel::tests::test_new ... ok</span>
<span id="parc-smallest-out-correct-3"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">test</span> parcel::tests::test_smallest_area ... ok</span>
<span id="parc-smallest-out-correct-4"></span>
<span id="parc-smallest-out-correct-5"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">test</span> result: ok. 2 passed<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">;</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">0</span> failed<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">;</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">0</span> ignored<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">;</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">0</span> measured<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">;</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">0</span> filtered out<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">;</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">finished</span> in 0.00s</span></code></pre></div></div>
<p>And there’s the second <em>BOOM</em>!</p>
<p><img src="https://media1.giphy.com/media/v1.Y2lkPWVjZjA1ZTQ3M3N3MWFubGQ0Nmxkdm9sNDZiOWJuZjNuN2Rtb216anN2NXAycHQyaiZlcD12MV9naWZzX3NlYXJjaCZjdD1n/t0t6qyYQVj8Xu/giphy.webp" class="img-fluid"></p>
<p>The function calculates the areas of all three sides and then gives us back that value as an <code>i32</code> that we can use in the next function.</p>
</section>
</section>
<section id="wrapping_paper" class="level2">
<h2 class="anchored" data-anchor-id="wrapping_paper"><code>wrapping_paper()</code></h2>
<p>I want one function that can just hand us back the amount of wrapping paper needed. This means that it should use the <code>smallest_area()</code> function as part of the calculations on a given <code>Parcel</code> instance.</p>
<p>As always, our first step is to write a test for our <code>wrapping_paper()</code> function</p>
<section id="test_wrapping" class="level3">
<h3 class="anchored" data-anchor-id="test_wrapping"><code>test_wrapping()</code></h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="wrapping-test" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="wrapping-test-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// -- snip --</span></span>
<span id="wrapping-test-2">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">#[</span>test<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">]</span></span>
<span id="wrapping-test-3">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> test_wrapping_paper() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="wrapping-test-4">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// make a new parce</span></span>
<span id="wrapping-test-5">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> test_parcel <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">Parcel::</span>new(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="wrapping-test-6">        <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">assert_eq!</span>(test_parcel<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>wrapping_paper()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">24</span></span>
<span id="wrapping-test-7">            )<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="wrapping-test-8">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="wrapping-test-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// -- snip --</span></span></code></pre></div></div>
</section>
<section id="implementing-wrapping_paper" class="level3">
<h3 class="anchored" data-anchor-id="implementing-wrapping_paper">implementing <code>wrapping_paper()</code></h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="wrapping_paper" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="wrapping_paper-1"></span>
<span id="wrapping_paper-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// -- snip --</span></span>
<span id="wrapping_paper-3">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">pub</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> wrapping_paper(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i32</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="wrapping_paper-4">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="wrapping_paper-5">            <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>l <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>w) </span>
<span id="wrapping_paper-6">          <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>l <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>h) </span>
<span id="wrapping_paper-7">          <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>w <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>h)</span>
<span id="wrapping_paper-8">          <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>smallest_area()</span>
<span id="wrapping_paper-9">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="wrapping_paper-10">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="wrapping_paper-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// -- snip --</span></span></code></pre></div></div>
<p>In the past, in my youth of two function definitions ago, I would have faced this next <code>cargo test</code> confidently.</p>
<p>Pridefully.</p>
<p>But now, oh now, I just trust <code>Friend Computer</code> to give me guidance and to look upon my errors with grace.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="wrapping-test-pass" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="wrapping-test-pass-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">running</span> 3 tests</span>
<span id="wrapping-test-pass-2"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">test</span> parcel::tests::test_new ... ok</span>
<span id="wrapping-test-pass-3"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">test</span> parcel::tests::test_wrapping_paper ... ok</span>
<span id="wrapping-test-pass-4"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">test</span> parcel::tests::test_smallest_area ... ok</span>
<span id="wrapping-test-pass-5"></span>
<span id="wrapping-test-pass-6"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">test</span> result: ok. 3 passed<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">;</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">0</span> failed<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">;</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">0</span> ignored<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">;</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">0</span> measured<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">;</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">0</span> filtered out<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">;</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">finished</span> in 0.00s</span></code></pre></div></div>
<p><em>BOOM! I am a genius! I Always knew it! (cough, wheeze, push up glasses)</em></p>
<p>I mean… good. Boom.</p>
<p>So we have the functions to get the results we need for <a href="puzzle.txt">Part 1</a>, and yet there’s something else I want to do before we pipe all this into <code>main</code>.</p>
</section>
</section>
<section id="from_string" class="level2">
<h2 class="anchored" data-anchor-id="from_string"><code>from_string()</code></h2>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Warning
</div>
</div>
<div class="callout-body-container callout-body">
<p>Dear Reader, while the following passages are an accurate portrayal of Sisyphean struggle, this next section is long and dense with error output. It does show some truly top class “MidWittery” but if you want to just skip to the part where it gets solved… then go to How I was wrong.. But you’ll miss the stunning interpersonal drama!!</p>
</div>
</div>
<p>As we covered in <a href="../../../aoc/2015/day_1/day_1_sidequest.html">SideQuest 1</a> I’m storing all the puzzle input in <code>./input.txt</code>, and I wrote the <code>aoc_common</code> module to help reading it in. The input for this puzzle is in the format:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="input-file" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="input-file-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">9x13x26</span></span>
<span id="input-file-2"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">11x11x14</span></span>
<span id="input-file-3"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">27x2x5</span></span>
<span id="input-file-4"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">6x10x13</span></span>
<span id="input-file-5"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">15x19x10</span></span>
<span id="input-file-6"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">26x29x15</span></span>
<span id="input-file-7"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">//</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--</span> snip <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--</span> </span></code></pre></div></div>
<p>Which will get pulled as:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="sting-input" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="sting-input-1"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"9x13x26\n</span></span>
<span id="sting-input-2"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">11x11x14\n</span></span>
<span id="sting-input-3"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">27x2x5\n</span></span>
<span id="sting-input-4"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">6x10x13\n</span></span>
<span id="sting-input-5"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">15x19x10\n</span></span>
<span id="sting-input-6"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">26x29x15\n</span></span>
<span id="sting-input-7"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">// -- snip -- </span></span>
<span id="sting-input-8"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">8x23x6\n"</span></span></code></pre></div></div>
<p>Or one long string separated by <code>newlines</code>. <code>from_string()</code> is intended to take in an individual line of that text, trim the <code>newline</code>, split on the <code>'x'</code> char and create a new <code>Parcel</code> from the 3 <code>i32</code>s.</p>
<p><em>Test first; ask questions later</em></p>
<section id="test-from_string" class="level3">
<h3 class="anchored" data-anchor-id="test-from_string">Test <code>from_string()</code></h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="from_string-test" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="from_string-test-1">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">#[</span>test<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">]</span></span>
<span id="from_string-test-2">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> from_string() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="from_string-test-3">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// make a new parce</span></span>
<span id="from_string-test-4">        <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">assert_eq!</span>(</span>
<span id="from_string-test-5">            <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">Parcel::</span>new(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="from_string-test-6">            <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">Parcel::</span>from_string(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">String</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span>from(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"1x2x3</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>))</span>
<span id="from_string-test-7">            )<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="from_string-test-8">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<p>Then onto implementing the function</p>
</section>
<section id="firstattempt" class="level3">
<h3 class="anchored" data-anchor-id="firstattempt">First attempt</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="from_string-1" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="from_string-1-1">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">pub</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> from_string(dimensions<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">str</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> Parcel <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="from_string-1-2">       <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// trim the \n </span></span>
<span id="from_string-1-3">       <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> numbers<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Vec</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i32</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dimensions</span>
<span id="from_string-1-4">                               <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>trim_end()</span>
<span id="from_string-1-5">                               <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>split(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x"</span>)</span>
<span id="from_string-1-6">                               <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>into_iter()</span>
<span id="from_string-1-7">                               <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>map(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">parse::</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i32</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span>())</span>
<span id="from_string-1-8">                               <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">collect::</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Vec</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i32</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;</span>()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="from_string-1-9">        <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">Parcel::</span>new(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>numbers[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> numbers[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> numbers[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>])</span>
<span id="from_string-1-10">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<p>Here’s the first shot at it.</p>
<p>We’re turning the input text (taking a <code>&amp;str</code> because I believe it’s more flexible) and turning it into a vector of <code>i32</code> numbers using the <code>map iterator adaptor</code> with a <code>.parse()</code> inside a closure.</p>
<p>Then, we use the <code>.</code> syntax to pass each of those numbers into our <code>Parcel::new()</code> associated function from earlier which gives us a <code>Parcel</code> we can return out of the function<sup>5</sup>.</p>
<p>Let’s see if this works?</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="from_string-1-out" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="from_string-1-out-1"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">error</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>E0277<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">:</span> a value of type <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Vec</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>i32<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span> cannot be built from an iterator over elements of type <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Result</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>i32, ParseIntError<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span></span>
<span id="from_string-1-out-2">  <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">--</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> day_2/src/parcel.rs:23:43</span>
<span id="from_string-1-out-3">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span></span>
<span id="from_string-1-out-4"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">23</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">...</span>                   .collect::<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>Vec<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>i32<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">(</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">);</span></span>
<span id="from_string-1-out-5">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                        <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">-------</span>   ^^^^^^^^ value of type <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Vec</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>i32<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span> cannot be built from <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">std::iter::Iterator</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>Item=Result<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>i32</span>
<span id="from_string-1-out-6"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">,</span> ParseIntError<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span></span>
<span id="from_string-1-out-7">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span></span>
<span id="from_string-1-out-8">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                        <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">required</span> by a bound introduced by this call</span>
<span id="from_string-1-out-9">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span></span>
<span id="from_string-1-out-10"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">help:</span> the trait <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">FromIterator</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>Result<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>i32, ParseIntError<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span> is not implemented for <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Vec</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>i32<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span></span>
<span id="from_string-1-out-11">      <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">but</span> trait <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">FromIterator</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>i32<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span> is implemented for it</span>
<span id="from_string-1-out-12">  <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">--</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> /rustc/59807616e1fa2540724bfbac14d7976d7e4a3860/library/alloc/src/vec/mod.rs:3862:0</span>
<span id="from_string-1-out-13">   <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">=</span> help: for that trait implementation, expected <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">i32</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span>, found <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Result</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>i32, ParseIntError<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span></span>
<span id="from_string-1-out-14"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">note:</span> the method call chain might not have had the expected associated types</span>
<span id="from_string-1-out-15">  <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">--</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> day_2/src/parcel.rs:22:33</span>
<span id="from_string-1-out-16">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span></span>
<span id="from_string-1-out-17"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">18</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">let</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">numbers</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Vec</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>i32<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> = <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">dimensions</span></span>
<span id="from_string-1-out-18">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                                <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">----------</span> this expression has type <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`&amp;</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">str</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span></span>
<span id="from_string-1-out-19"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">19</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">.trim_end()</span></span>
<span id="from_string-1-out-20"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">20</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                                <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">.split</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x"</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">)</span></span>
<span id="from_string-1-out-21">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                                 <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">----------</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Iterator::Item</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span> is <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`&amp;</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">str</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span> here</span>
<span id="from_string-1-out-22"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">21</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">.into_iter()</span></span>
<span id="from_string-1-out-23">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                                 <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">-----------</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Iterator::Item</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span> remains <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`&amp;</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">str</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span> here</span>
<span id="from_string-1-out-24"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">22</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                                <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">.map</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">(</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">n</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">n.parse::</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>i32<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;()</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">)</span></span>
<span id="from_string-1-out-25">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                                 <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">^^^^^^^^^^^^^^^^^^^^^^^^^</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Iterator::Item</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span> changed to <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Result</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>i32, ParseIntError<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span> here</span>
<span id="from_string-1-out-26"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">note:</span> required by a bound in <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">collect</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span></span>
<span id="from_string-1-out-27">  <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">--</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> /rustc/59807616e1fa254error<span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">[</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">E0308</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">]</span>: mismatched types</span>
<span id="from_string-1-out-28">  <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">--</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> day_2/src/parcel.rs:89:33</span>
<span id="from_string-1-out-29">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span></span>
<span id="from_string-1-out-30"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">89</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>             <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Parcel::from_string</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">(</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">String::from</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"1x2x3\n"</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">))</span></span>
<span id="from_string-1-out-31">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>             <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">-------------------</span> ^^^^^^^^^^^^^^^^^^^^^^^ expected <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`&amp;</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">str</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span>, found <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">String</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span></span>
<span id="from_string-1-out-32">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>             <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span></span>
<span id="from_string-1-out-33">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>             <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">arguments</span> to this function are incorrect</span>
<span id="from_string-1-out-34">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span></span>
<span id="from_string-1-out-35"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">note:</span> associated function defined here</span>
<span id="from_string-1-out-36">  <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">--</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> day_2/src/parcel.rs:16:12</span>
<span id="from_string-1-out-37">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span></span>
<span id="from_string-1-out-38"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">16</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>     <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">pub</span> fn from_string<span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">(</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">dimensions:</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">&amp;</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">str</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">)</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">-</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> Parcel {</span>
<span id="from_string-1-out-39">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>            <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">^^^^^^^^^^^</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">----------------</span></span>
<span id="from_string-1-out-40"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">help:</span> consider borrowing here</span>
<span id="from_string-1-out-41">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span></span>
<span id="from_string-1-out-42"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">89</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>             <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Parcel::from_string</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">(</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">&amp;</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">String::from</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"1x2x3\n"</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">))</span></span>
<span id="from_string-1-out-43">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                                 <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">+</span></span>
<span id="from_string-1-out-44"></span>
<span id="from_string-1-out-45"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Some</span> errors have detailed explanations: E0277, E0308.0724bfbac14d7976d7e4a3860/library/core/src/iter/traits/iterator.rs:2051:4</span></code></pre></div></div>
<p><em>Bo</em>–…</p>
<p>Ok, so jokes aside, this feels a really good learning error here. Let’s look at it from top to bottom and let me see if I understand what’s going on and what the fixes are telling me. There’s basically the same error here, just twice and in two different places.</p>
<ul>
<li>We can’t make a <code>Vec&lt;i32&gt;</code> from the <code>map(|n| n.parse::&lt;i32&gt;())</code></li>
<li><code>Parcel::from_string()</code> is expecting a <code>&amp;str</code> but in the test I’ve passed it an <code>owned</code> <code>String</code>. (Flexible my ass…)</li>
</ul>
<blockquote class="blockquote">
<p>As always MidWit, you look but you do not observe!</p>
</blockquote>
</section>
<section id="map---resultt-e" class="level3">
<h3 class="anchored" data-anchor-id="map---resultt-e"><code>map</code> -&gt; <code>Result&lt;T, E&gt;</code></h3>
<p>Believe it or not, this first error is a blessing, especially following on from <a href="../../../aoc/2015/day_1/day_1_sidequest.html">SideQuest 1</a>.</p>
<p>So what we’re being told here, which I did <em>akshully</em> know, was that <code>map</code> isn’t just giving me back <code>i32</code>s here. It’s giving me back the outcome of calling <code>parse::&lt;i32&gt;()</code> on each element in the collection that <code>split("x")</code> gives me.</p>
<p><code>Result&lt;i32, ParseIntError&gt;</code></p>
<p>You guys all saw this, but this is our second RRM<sup>6</sup> of the puzzle.</p>
<p>If I understand this right: the <code>parse</code> method can fail, like a lot of things in rust, and so when we use <code>map</code> to call that on each object in a collection, we can’t be certain that all of the objects are amenable to being parsed. Like, what if the string was “1x2xbannanana\n”? I’d look like a fool then wouldn’t I?</p>
<p>So <code>map</code> gives us back <code>Results</code> that we need to handle just as we would anywhere else. This means that I could use <code>unwrap()</code> or <code>expect()</code> to make the function <code>panic</code> and stop the program if one of the given values wasn’t… parcable?… parcible?… numeric. However, given that the aim here is to reach to what I believe are good practices, maybe this gives us access to the <code>?</code> operator, and to returning a <code>Result&lt;Parcel, ParseIntError&gt;</code>?</p>
<p>Nothin’ to it but to do it.</p>
</section>
<section id="second-attempt-trying" class="level3">
<h3 class="anchored" data-anchor-id="second-attempt-trying">Second attempt: trying <code>?</code></h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="from_string-2" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="from_string-2-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">use</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">core::</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">iter::</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">IntoIterator</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">num::</span>ParseIntError<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">};</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// nvim/my lsp added these for me</span></span>
<span id="from_string-2-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// -- snip --</span></span>
<span id="from_string-2-3">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">pub</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> from_string(dimensions<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">str</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Result</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>Parcel<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> ParseIntError<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="from_string-2-4">       <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// trim the \n </span></span>
<span id="from_string-2-5">       <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> numbers<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Vec</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i32</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dimensions</span>
<span id="from_string-2-6">                               <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>trim_end()</span>
<span id="from_string-2-7">                               <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>split(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x"</span>)</span>
<span id="from_string-2-8">                               <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>into_iter()</span>
<span id="from_string-2-9">                               <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>map(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">parse::</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i32</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span>())</span>
<span id="from_string-2-10">                               <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">collect::</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Vec</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Ok</span>(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i32</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;</span>()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?;</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">//changed the return type and added?</span></span>
<span id="from_string-2-11">        <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Ok</span>(<span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">Parcel::</span>new(numbers[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> numbers[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> numbers[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>])) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// returing an Ok(Parcel) now</span></span>
<span id="from_string-2-12">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="from_string-2-13"></span>
<span id="from_string-2-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// -- snip --</span></span>
<span id="from_string-2-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// updating the test</span></span>
<span id="from_string-2-16">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">#[</span>test<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">]</span></span>
<span id="from_string-2-17">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> from_string() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="from_string-2-18">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// make a new parce</span></span>
<span id="from_string-2-19">        <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">assert_eq!</span>(</span>
<span id="from_string-2-20">            <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">Parcel::</span>new(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="from_string-2-21">            <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">Parcel::</span>from_string(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"1x2x3</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>unwrap()</span>
<span id="from_string-2-22">            )<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="from_string-2-23">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<p>Lot of changes up there, but in brief:</p>
<ul>
<li>My editor gave me some <code>use</code> statements to bring some things that I’d need into scope. Thanks Obama.</li>
<li>I’ve changed the return types in a few places to put <code>Result</code> or its <code>variants</code> everywhere.</li>
<li>I’ve updated the test to reflect this (I was clearly wrong about the flexibility of slices).</li>
</ul>
<p>Now, I’m 90% sure that I’ve got the wrong <code>turbofish</code> on the <code>collect()</code> and that the <code>?</code> operator is in the wrong place, but we’re here to learn so let’s FISI and see what we get.</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>Dear Reader, he did in fact have the wrong <code>turbofish</code>.</p>
<p>He did not, in fact, have the <code>?</code> in the wrong place.</p>
</div>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="from_string-1-out" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="from_string-1-out-1"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">error</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>E0573<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">:</span> expected type, found variant <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Ok</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span></span>
<span id="from_string-1-out-2">  <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">--</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> day_2/src/parcel.rs:23:47</span>
<span id="from_string-1-out-3">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span></span>
<span id="from_string-1-out-4"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">23</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">...</span>                   .collect::<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>Vec<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>Ok<span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">(</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">i32</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">()</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">?</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">;</span></span>
<span id="from_string-1-out-5">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                                      <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">^^^^^^^</span> not a type</span>
<span id="from_string-1-out-6">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span></span>
<span id="from_string-1-out-7"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">help:</span> try using the variant<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'s enum</span></span>
<span id="from_string-1-out-8"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">   |</span></span>
<span id="from_string-1-out-9"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">23 -                                .collect::&lt;Vec&lt;Ok(i32)&gt;&gt;()?;</span></span>
<span id="from_string-1-out-10"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">23 +                                .collect::&lt;Vec&lt;core::result::Result&gt;&gt;()?;</span></span>
<span id="from_string-1-out-11"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">   |</span></span>
<span id="from_string-1-out-12"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">23 -                                .collect::&lt;Vec&lt;Ok(i32)&gt;&gt;()?;</span></span>
<span id="from_string-1-out-13"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">23 +                                .collect::&lt;Vec&lt;std::result::Result&gt;&gt;()?;</span></span>
<span id="from_string-1-out-14"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">   |</span></span>
<span id="from_string-1-out-15"></span>
<span id="from_string-1-out-16"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">For more information about this error, try `rustc --explain E0573`.</span></span>
<span id="from_string-1-out-17"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">error: could not compile `day_2` (bin "day_2" test) due to 1 previous error</span></span></code></pre></div></div>
<p>Ok, I was Schrodinger’s correct again!</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>No, in fact the cat was just dead.</p>
</div>
</div>
<p>The <code>turbofish</code> needs the <code>Result</code> and not the <code>variant</code>. However the <code>?</code> doesn’t seem to be throwing me an error… so The MidWit Strikes again!!</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="from_string-3" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="from_string-3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// -- snip -- </span></span>
<span id="from_string-3-2">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">pub</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> from_string(dimensions<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">str</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Result</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>Parcel<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> ParseIntError<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="from_string-3-3">       <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// trim the \n </span></span>
<span id="from_string-3-4">       <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> numbers<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Vec</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i32</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dimensions</span>
<span id="from_string-3-5">                               <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>trim_end()</span>
<span id="from_string-3-6">                               <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>split(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x"</span>)</span>
<span id="from_string-3-7">                               <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>into_iter()</span>
<span id="from_string-3-8">                               <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>map(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">parse::</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i32</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span>())</span>
<span id="from_string-3-9">                               <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">collect::</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Vec</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Result</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i32</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> ParseIntError<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;&gt;</span>()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?;</span></span>
<span id="from_string-3-10">        <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Ok</span>(<span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">Parcel::</span>new(numbers[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> numbers[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> numbers[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]))</span>
<span id="from_string-3-11">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="from_string-3-out" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="from_string-3-out-1"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">error</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>E0277<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">:</span> the <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">?</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span> operator can only be applied to values that implement <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Try</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span></span>
<span id="from_string-3-out-2">  <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">--</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> day_2/src/parcel.rs:18:32</span>
<span id="from_string-3-out-3">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span></span>
<span id="from_string-3-out-4"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">18</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>          <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">let</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">numbers</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Vec</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>i32<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> = <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">dimensions</span></span>
<span id="from_string-3-out-5">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>  <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">________________________________^</span></span>
<span id="from_string-3-out-6"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">19</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">.trim_end()</span></span>
<span id="from_string-3-out-7"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">20</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                                <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">.split</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x"</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">)</span></span>
<span id="from_string-3-out-8"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">21</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">.into_iter()</span></span>
<span id="from_string-3-out-9"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">22</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                                <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">.map</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">(</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">n</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">n.parse::</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>i32<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;()</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">)</span></span>
<span id="from_string-3-out-10"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">23</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                                <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">.collect::</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>Vec<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>Result<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>i32, ParseIntError<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;&gt;()</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">?</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">;</span></span>
<span id="from_string-3-out-11">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">_____________________________________________________________________________^</span> the <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">?</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span> operator cannot be applied to type <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span></span>
<span id="from_string-3-out-12"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Vec</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>Result<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>i32, ParseIntError<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span></span>
<span id="from_string-3-out-13">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span></span>
<span id="from_string-3-out-14">   <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">=</span> help: the nightly-only, unstable trait <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Try</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span> is not implemented for <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Vec</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>Result<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>i32, ParseIntError<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span></span></code></pre></div></div>
<p>There it is!! Turns out I was entirely right; I did get it completely wrong!! The <code>?</code> is also wrong. I knew it! I am a genius!</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>He’s now multiple steps deep in the wrong.</p>
</div>
</div>
<p>Honestly, my hope here is that this is telling me that the <code>?</code> needs to either go on the <code>parse</code> or on the <code>map</code>. Imma try both, and then… I’ll ask Claude if this is even possible.</p>
<p>I’m not going to put two more big blocks of code in there just to show that I’ve moved one symbol around. So <code>map(..)?</code> first.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="questionmap" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="questionmap-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">//</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--snip</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--</span></span>
<span id="questionmap-2"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">error</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>E0277<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">:</span> the <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">?</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span> operator can only be applied to values that implement <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Try</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span></span>
<span id="questionmap-3">  <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">--</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> day_2/src/parcel.rs:18:32</span>
<span id="questionmap-4">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span></span>
<span id="questionmap-5"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">18</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>          <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">let</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">numbers</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Vec</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>i32<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> = <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">dimensions</span></span>
<span id="questionmap-6">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>  <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">________________________________^</span></span>
<span id="questionmap-7"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">19</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">.trim_end()</span></span>
<span id="questionmap-8"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">20</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                                <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">.split</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x"</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">)</span></span>
<span id="questionmap-9"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">21</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">.into_iter()</span></span>
<span id="questionmap-10"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">22</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                                <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">.map</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">(</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">n</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">n.parse::</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>i32<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;()</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">)</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">?</span></span>
<span id="questionmap-11">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">__________________________________________________________^</span> the <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">?</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span> operator cannot be applied to type <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Map</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>std::str::Split</span>
<span id="questionmap-12"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'_, &amp;str&gt;, {closure@day_2/src/parcel.rs:22:37: 22:40}&gt;`</span></span>
<span id="questionmap-13"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">// -- snip --</span></span></code></pre></div></div>
<p>Same error, so <code>map(|n| n.parse::&lt;i32&gt;()?)</code> next.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="questionparse" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="questionparse-1"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">error</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>E0277<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">:</span> the <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">?</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span> operator can only be used in a closure that returns <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Result</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span> or <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Option</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">(</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">or</span> another type that implements <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span></span>
<span id="questionparse-2"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">FromResidual</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`)</span></span>
<span id="questionparse-3">  <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">--</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> day_2/src/parcel.rs:22:57</span>
<span id="questionparse-4">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span></span>
<span id="questionparse-5"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">22</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">...</span>                   .map<span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">(</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">n</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">n.parse::</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>i32<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;()</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">?</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">)</span></span>
<span id="questionparse-6">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                            <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">---</span>                 ^ cannot use the <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">?</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span> operator in a closure that returns <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">i32</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span></span>
<span id="questionparse-7">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                            <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span></span>
<span id="questionparse-8">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                            <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">this</span> function should return <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Result</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span> or <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Option</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span> to accept <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">?</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span></span>
<span id="questionparse-9"></span>
<span id="questionparse-10"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">error</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>E0277<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">:</span> a value of type <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Vec</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>Result<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>i32, ParseIntError<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span> cannot be built from an iterator over elements of type <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">i32</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span></span>
<span id="questionparse-11">  <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">--</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> day_2/src/parcel.rs:23:43</span>
<span id="questionparse-12">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span></span>
<span id="questionparse-13"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">23</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">...</span>                   .collect::<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>Vec<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>Result<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>i32, ParseIntError<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;&gt;()</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">;</span></span>
<span id="questionparse-14">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                        <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">-------</span>   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ value of type <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Vec</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>Result<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>i32, ParseIntError<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span> cannot be b</span>
<span id="questionparse-15"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">uilt</span> from <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">std::iter::Iterator</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>Item=i32<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span></span>
<span id="questionparse-16">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span></span>
<span id="questionparse-17">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                        <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">required</span> by a bound introduced by this call</span>
<span id="questionparse-18">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span></span>
<span id="questionparse-19"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">help:</span> the trait <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">FromIterator</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>i32<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span> is not implemented for <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Vec</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>Result<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>i32, ParseIntError<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span></span>
<span id="questionparse-20">      <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">but</span> trait <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">FromIterator</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>Result<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>i32, ParseIntError<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span> is implemented for it</span>
<span id="questionparse-21">  <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">--</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> /rustc/59807616e1fa2540724bfbac14d7976d7e4a3860/library/alloc/src/vec/mod.rs:3862:0</span>
<span id="questionparse-22">   <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">=</span> help: for that trait implementation, expected <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Result</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>i32, ParseIntError<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span>, found <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">i32</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span></span>
<span id="questionparse-23"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">note:</span> the method call chain might not have had the expected associated types</span>
<span id="questionparse-24">  <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">--</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> day_2/src/parcel.rs:22:33</span>
<span id="questionparse-25">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span></span>
<span id="questionparse-26"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">18</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">let</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">numbers</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Vec</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>i32<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> = <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">dimensions</span></span>
<span id="questionparse-27">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                                <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">----------</span> this expression has type <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`&amp;</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">str</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span></span>
<span id="questionparse-28"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">19</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">.trim_end()</span></span>
<span id="questionparse-29"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">20</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                                <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">.split</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x"</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">)</span></span>
<span id="questionparse-30">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                                 <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">----------</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Iterator::Item</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span> is <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`&amp;</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">str</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span> here</span>
<span id="questionparse-31"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">21</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">.into_iter()</span></span>
<span id="questionparse-32">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                                 <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">-----------</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Iterator::Item</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span> remains <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`&amp;</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">str</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span> here</span>
<span id="questionparse-33"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">22</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                                <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">.map</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">(</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">n</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">n.parse::</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>i32<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;()</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">?</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">)</span></span>
<span id="questionparse-34">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                                 <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">^^^^^^^^^^^^^^^^^^^^^^^^^^</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Iterator::Item</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span> changed to <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">i32</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span> here</span>
<span id="questionparse-35"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">note:</span> required by a bound in <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">collect</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span></span>
<span id="questionparse-36">  <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">--</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> /rustc/59807616e1fa2540724bfbac14d7976d7e4a3860/library/core/src/iter/traits/iterator.rs:2051:4</span>
<span id="questionparse-37"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">error</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>E0308<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">:</span> mismatched types</span>
<span id="questionparse-38">  <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">--</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> day_2/src/parcel.rs:18:32</span>
<span id="questionparse-39">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span></span>
<span id="questionparse-40"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">18</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>          <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">let</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">numbers</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Vec</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>i32<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> = <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">dimensions</span></span>
<span id="questionparse-41">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>  <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">_____________________--------___^</span></span>
<span id="questionparse-42">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                     <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span></span>
<span id="questionparse-43">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                     <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">expected</span> due to this</span>
<span id="questionparse-44"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">19</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">.trim_end()</span></span>
<span id="questionparse-45"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">20</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                                <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">.split</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x"</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">)</span></span>
<span id="questionparse-46"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">21</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">.into_iter()</span></span>
<span id="questionparse-47"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">22</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                                <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">.map</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">(</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">n</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">n.parse::</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>i32<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;()</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">?</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">)</span></span>
<span id="questionparse-48"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">23</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                                <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">.collect::</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>Vec<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>Result<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>i32, ParseIntError<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;&gt;()</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">;</span></span>
<span id="questionparse-49">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">____________________________________________________________________________^</span> expected <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Vec</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>i32<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span>, found <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Vec</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>Result<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>i32, ParseIntError<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span></span>
<span id="questionparse-50">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span></span>
<span id="questionparse-51">   <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">=</span> note: expected struct <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Vec</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>i32<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span></span>
<span id="questionparse-52">              <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">found</span> struct <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Vec</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>Result<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>i32, ParseIntError<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span>_ </span></code></pre></div></div>
<p>Huh… That’s… a lot…</p>
<blockquote class="blockquote">
<p>What we got here… Is a failure… to communicate.</p>
</blockquote>
<p>That move changed a lot more than I expected. Either I missed something in earlier output, or adding the <code>?</code> operator directly to the <code>parse</code> is handling the <code>Result</code> for me…</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>He did, in fact, miss <em>something</em> earlier.</p>
</div>
</div>
<p>Ok, I know I said I’d ask Claude if I couldn’t figure it out, but just gimme a sec here.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="from_string-4" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="from_string-4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// -- snip -- </span></span>
<span id="from_string-4-2">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">pub</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> from_string(dimensions<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">str</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Result</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>Parcel<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> ParseIntError<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="from_string-4-3">       <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// trim the \n </span></span>
<span id="from_string-4-4">       <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> numbers<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Vec</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i32</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dimensions</span>
<span id="from_string-4-5">                               <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>trim_end()</span>
<span id="from_string-4-6">                               <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>split(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x"</span>)</span>
<span id="from_string-4-7">                               <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>into_iter()</span>
<span id="from_string-4-8">                               <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>map(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">parse::</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i32</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span>()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?</span>)</span>
<span id="from_string-4-9">                               <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">collect::</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Vec</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i32</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;</span>()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// note that we've gone back to Vec&lt;i32&gt; here</span></span>
<span id="from_string-4-10">        <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Ok</span>(<span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">Parcel::</span>new(numbers[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> numbers[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> numbers[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]))</span>
<span id="from_string-4-11">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="from-string-4-out" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="from-string-4-out-1"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">error</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>E0277<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">:</span> the <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">?</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span> operator can only be used in a closure that returns <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Result</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span> or <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Option</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">(</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">or</span> another type that implements <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span></span>
<span id="from-string-4-out-2"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">FromResidual</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`)</span></span>
<span id="from-string-4-out-3">  <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">--</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> day_2/src/parcel.rs:22:57</span>
<span id="from-string-4-out-4">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span></span>
<span id="from-string-4-out-5"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">22</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">...</span>                   .map<span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">(</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">n</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">n.parse::</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>i32<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;()</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">?</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">)</span></span>
<span id="from-string-4-out-6">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                            <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">---</span>                 ^ cannot use the <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">?</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span> operator in a closure that returns <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">i32</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span></span>
<span id="from-string-4-out-7">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                            <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span></span>
<span id="from-string-4-out-8">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                            <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">this</span> function should return <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Result</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span> or <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Option</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span> to accept <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">?</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span></span>
<span id="from-string-4-out-9"></span>
<span id="from-string-4-out-10"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">For</span> more information about this error, try <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">rustc</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--explain</span> E0277<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span>.</span></code></pre></div></div>
<p>Well… crap.</p>
<p>I thought I was onto something there. But as they say, faint hearts never won fair maiden so I’m going to try something a bit less ergonomic but that might help this click for me.</p>
</section>
<section id="getting-explicit-dropping" class="level3">
<h3 class="anchored" data-anchor-id="getting-explicit-dropping">Getting Explicit: Dropping <code>?</code></h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="from-string-explicit" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="from-string-explicit-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// -- snip --</span></span>
<span id="from-string-explicit-2">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">pub</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> from_string(dimensions<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">str</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Result</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>Parcel<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> ParseIntError<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="from-string-explicit-3">       <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// trim the \n </span></span>
<span id="from-string-explicit-4">       <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> numbers<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Vec</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i32</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dimensions</span>
<span id="from-string-explicit-5">                               <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>trim_end()</span>
<span id="from-string-explicit-6">                               <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>split(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x"</span>)</span>
<span id="from-string-explicit-7">                               <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>into_iter()</span>
<span id="from-string-explicit-8">                               <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>map(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="from-string-explicit-9">                                   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> num <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">parse::</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i32</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span>()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="from-string-explicit-10">                                   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> d <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">match</span> num <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="from-string-explicit-11">                                        <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Ok</span>(i) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span> i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="from-string-explicit-12">                                        <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Err</span>(_e) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="from-string-explicit-13">                                            <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">println!</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"You didn't get a number fool"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="from-string-explicit-14">                                            <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="from-string-explicit-15">                                        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="from-string-explicit-16">                                   <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">};</span></span>
<span id="from-string-explicit-17">                                   d</span>
<span id="from-string-explicit-18">                               <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="from-string-explicit-19">                                )</span>
<span id="from-string-explicit-20">                               <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">collect::</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Vec</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i32</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;</span>()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="from-string-explicit-21">        <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Ok</span>(<span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">Parcel::</span>new(numbers[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> numbers[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> numbers[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]))</span>
<span id="from-string-explicit-22">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="from-string-explicit-23"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// -- snip --</span></span></code></pre></div></div>
<p>That’s… ugly code… and I’m pretty sure that the return type is wrong</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="confusing-pass" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="confusing-pass-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">running</span> 4 tests</span>
<span id="confusing-pass-2"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">test</span> parcel::tests::from_string ... ok</span>
<span id="confusing-pass-3"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">test</span> parcel::tests::test_new ... ok</span>
<span id="confusing-pass-4"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">test</span> parcel::tests::test_smallest_area ... ok</span>
<span id="confusing-pass-5"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">test</span> parcel::tests::test_wrapping_paper ... ok</span>
<span id="confusing-pass-6"></span>
<span id="confusing-pass-7"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">test</span> result: ok. 4 passed<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">;</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">0</span> failed<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">;</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">0</span> ignored<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">;</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">0</span> measured<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">;</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">0</span> filtered out<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">;</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">finished</span> in 0.00</span></code></pre></div></div>
<p>But, explicitly handling the error lets the test pass… <del>great</del>.</p>
<p>Right! Time to stop stumbling around.</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>Hang on, Dear Reader. This is nearly over, and there’s a shocking betrayal coming up!!</p>
</div>
</div>
</section>
<section id="what-do-i-think-i-know" class="level3">
<h3 class="anchored" data-anchor-id="what-do-i-think-i-know">What do I think I know?</h3>
<p>You definitely already know where I’m going wrong, but I’m not seeing it.</p>
<p>Based on how I’m understanding the various error messages, the main problem here is my misunderstanding what types are getting produced at each step, and therefore where <code>?</code> might be relevant. <code>Friend Computer</code> gives good feedback, but it’s ephemeral, it doesn’t know what has been tried previously or what misconceptions I have.</p>
<p>If I’m trying to learn-by-doing here it would probably be a good idea to reset and try to think through where I’m at.</p>
<p>So what am I assuming here?</p>
<ul>
<li><code>split</code> is giving me back a <code>Split</code> object which is a collection in itself (caveats aside).</li>
<li><code>map</code> takes that collection, lets me run some function over it giving me back a collection (after <code>.collect()</code>).
<ul>
<li><code>map</code> will also give me an error if it encounters something the internal function can’t work on.</li>
</ul></li>
<li><code>parse()</code> gives me a <code>Result&lt;i32, ParseIntError&gt;</code></li>
<li><code>collect()</code> gives me back the collection.</li>
<li>the <code>?</code> operator can only be called on something that produces a <code>Result</code> or <code>Option</code>…</li>
</ul>
<p>Which reminds me of something else.</p>
<ul>
<li>calling next on a collection can return an <code>Option</code> with <code>Some(T)</code> when there is a thing at that position in the collection.</li>
<li>However, if there’s nothing at that position, say if we’ve reached the end of the array, it can give us <code>None</code>.</li>
</ul>
<p>So, maybe instead of <code>Vec&lt;Result&lt;i32, ParseIntError&gt;&gt;</code> in the <code>.collect() turbofish</code> (which just seemed wrong anyway) maybe it should be a <code>Vec&lt;Option&lt;i32&gt;&gt;</code>.</p>
</section>
<section id="trying-option" class="level3">
<h3 class="anchored" data-anchor-id="trying-option">Trying <code>Option</code></h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="parceloption" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="parceloption-1">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">pub</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> from_string(dimensions<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">str</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Result</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>Parcel<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> ParseIntError<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="parceloption-2">       <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// trim the \n </span></span>
<span id="parceloption-3">       <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> numbers<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Vec</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i32</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dimensions</span>
<span id="parceloption-4">                               <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>trim_end()</span>
<span id="parceloption-5">                               <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>split(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x"</span>)</span>
<span id="parceloption-6">                               <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>into_iter()</span>
<span id="parceloption-7">                               <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>map(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">parse::</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i32</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span>()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?</span>)</span>
<span id="parceloption-8">                               <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">collect::</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Vec</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Option</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i32</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;&gt;</span>()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="parceloption-9">        <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Ok</span>(<span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">Parcel::</span>new(numbers[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> numbers[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> numbers[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]))</span>
<span id="parceloption-10">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="parceloption-out" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="parceloption-out-1"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">error</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>E0277<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">:</span> the <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">?</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span> operator can only be used in a closure that returns <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Result</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span> or <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Option</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">(</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">or</span> another type that implements <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span></span>
<span id="parceloption-out-2"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">FromResidual</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`)</span></span>
<span id="parceloption-out-3">  <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">--</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> day_2/src/parcel.rs:22:57</span>
<span id="parceloption-out-4">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span></span>
<span id="parceloption-out-5"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">22</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">...</span>                   .map<span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">(</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">n</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">n.parse::</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>i32<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;()</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">?</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">)</span></span>
<span id="parceloption-out-6">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                            <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">---</span>                 ^ cannot use the <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">?</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span> operator in a closure that returns <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">i32</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span></span>
<span id="parceloption-out-7">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                            <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span></span>
<span id="parceloption-out-8">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                            <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">this</span> function should return <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Result</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span> or <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Option</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span> to accept <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">?</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span></span>
<span id="parceloption-out-9"></span>
<span id="parceloption-out-10"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">error</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>E0277<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">:</span> a value of type <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Vec</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>Option<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>i32<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span> cannot be built from an iterator over elements of type <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">i32</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span></span>
<span id="parceloption-out-11">  <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">--</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> day_2/src/parcel.rs:23:43</span>
<span id="parceloption-out-12">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span></span>
<span id="parceloption-out-13"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">23</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">...</span>                   .collect::<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>Vec<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>Option<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>i32<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;&gt;()</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">;</span></span>
<span id="parceloption-out-14">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                        <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">-------</span>   ^^^^^^^^^^^^^^^^ value of type <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Vec</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>Option<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>i32<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span> cannot be built from <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">std::iter::Iterator</span></span>
<span id="parceloption-out-15"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>Item=i32<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span></span>
<span id="parceloption-out-16">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span></span>
<span id="parceloption-out-17">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                        <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">required</span> by a bound introduced by this call</span>
<span id="parceloption-out-18">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span></span>
<span id="parceloption-out-19"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">help:</span> the trait <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">FromIterator</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>i32<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span> is not implemented for <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Vec</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>Option<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>i32<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span></span>
<span id="parceloption-out-20">      <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">but</span> trait <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">FromIterator</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>Option<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>i32<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span> is implemented for it</span>
<span id="parceloption-out-21">  <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">--</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> /rustc/59807616e1fa2540724bfbac14d7976d7e4a3860/library/alloc/src/vec/mod.rs:3862:0</span>
<span id="parceloption-out-22">   <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">=</span> help: for that trait implementation, expected <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Option</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>i32<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span>, found <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">i32</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span></span>
<span id="parceloption-out-23"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">note:</span> the method call chain might not have had the expected associated types</span>
<span id="parceloption-out-24">  <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">--</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> day_2/src/parcel.rs:22:33</span>
<span id="parceloption-out-25">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span></span>
<span id="parceloption-out-26"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">18</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">let</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">numbers</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Vec</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>i32<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> = <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">dimensions</span></span>
<span id="parceloption-out-27">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                                <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">----------</span> this expression has type <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`&amp;</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">str</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span></span>
<span id="parceloption-out-28"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">19</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">.trim_end()</span></span>
<span id="parceloption-out-29"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">20</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                                <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">.split</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x"</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">)</span></span>
<span id="parceloption-out-30">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                                 <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">----------</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Iterator::Item</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span> is <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`&amp;</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">str</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span> here</span>
<span id="parceloption-out-31"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">21</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">.into_iter()</span></span>
<span id="parceloption-out-32">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                                 <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">-----------</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Iterator::Item</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span> remains <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`&amp;</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">str</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span> here</span>
<span id="parceloption-out-33"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">22</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                                <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">.map</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">(</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">n</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">n.parse::</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>i32<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;()</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">?</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">)</span></span>
<span id="parceloption-out-34">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                                 <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">^^^^^^^^^^^^^^^^^^^^^^^^^^</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Iterator::Item</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span> changed to <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">i32</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span> here</span>
<span id="parceloption-out-35"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">note:</span> required by a bound in <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">collect</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span></span>
<span id="parceloption-out-36">  <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">--</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> /rustc/59807616e1fa2540724bfbac14d7976d7e4a3860/library/core/src/iter/traits/iterator.rs:2051:4</span>
<span id="parceloption-out-37"></span>
<span id="parceloption-out-38"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">error</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>E0308<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">:</span> mismatched types</span>
<span id="parceloption-out-39">  <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">--</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> day_2/src/parcel.rs:18:32</span>
<span id="parceloption-out-40">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span></span>
<span id="parceloption-out-41"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">18</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>          <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">let</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">numbers</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Vec</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>i32<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> = <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">dimensions</span></span>
<span id="parceloption-out-42">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>  <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">_____________________--------___^</span></span>
<span id="parceloption-out-43">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                     <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span></span>
<span id="parceloption-out-44">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                     <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">expected</span> due to this</span>
<span id="parceloption-out-45"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">19</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">.trim_end()</span></span>
<span id="parceloption-out-46"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">20</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                                <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">.split</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x"</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">)</span></span>
<span id="parceloption-out-47"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">21</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">.into_iter()</span></span>
<span id="parceloption-out-48"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">22</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                                <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">.map</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">(</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">n</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">n.parse::</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>i32<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;()</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">?</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">)</span></span>
<span id="parceloption-out-49"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">23</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span>                                <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">.collect::</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>Vec<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>Option<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>i32<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;&gt;()</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">;</span></span>
<span id="parceloption-out-50">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">_____________________________________________________________^</span> expected <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Vec</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>i32<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span>, found <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Vec</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>Option<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>i32<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span></span>
<span id="parceloption-out-51">   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span></span>
<span id="parceloption-out-52">   <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">=</span> note: expected struct Vec<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>i32<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span></span></code></pre></div></div>
<p>Sigh…</p>
</section>
<section id="shocking-betrayal" class="level3">
<h3 class="anchored" data-anchor-id="shocking-betrayal">Getting “help”</h3>
<p>Ok, so… that’s very similar to an earlier error and we both know that this ain’t an interesting read…</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>See? He’s not oblivious.</p>
<p>He keeps missing that he’s typehinting result as <code>Vec&lt;i32&gt;</code>, but he’s not <em>completely</em> oblivious.</p>
<p>Here comes the betrayal. Don’t look away, but hold your loved ones close.</p>
</div>
</div>
<p>I have a passing solution, I just don’t like it. Rather than staying <a href="https://xkcd.com/356/">nerdsniped</a> though, I’m going to ask Claude if the idea is actually possible. I’m going to pass what I’ve written in with the prompt along with the question</p>
<p><strong>My question</strong></p>
<blockquote class="blockquote">
<p>Here’s what I’ve written so far, and ,I’ve also tried just dropping the <code>?</code> entirly and turbofishing both a <code>vec&lt;i32&gt;</code> and a <code>Vec&lt;Result&lt;i32, ParseIntError&gt;&gt;</code> and both failed. So I’m not understanding something about this order. The compiler tells me that the map is returning a result&lt;i32, _&gt; and that indicates to me that I <em>should</em> be able to write this more ergonomically than I have in the passing version, maybe that’s an if let rather than <code>?</code> Just tell me, with no editorialising or additional guidance, can I use <code>?</code> in this context?</p>
</blockquote>
<p><strong>Claude’s answer</strong></p>
<blockquote class="blockquote">
<p>Yes — but not the way you’ve been placing it, and not on the closure inside <code>map</code>. The <code>?</code> goes on the <code>collect</code>, not inside the <code>map</code>. And the single thing that makes it work, which you haven’t tried, is the <code>turbofish</code>: you have to collect into <code>Result&lt;Vec&lt;i32&gt;, ParseIntError&gt;</code> — the <code>Result</code> on the outside, <code>Vec</code> inside — not <code>Vec&lt;Result&lt;...&gt;&gt;</code>. Once the collect produces a single <code>Result</code>, <code>?</code> has something it can be applied to. Your two collect attempts both failed because <code>Vec&lt;i32&gt;</code> can’t be built from Result items, and <code>Vec&lt;Result&lt;...&gt;&gt;</code> is a vector, which <code>?</code> can’t touch. The shape you never tried is the one that works.</p>
</blockquote>
<p>Welp! That sucks.</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>Et tu Brute?!</p>
<p>You can’t see this Dear Reader, but he’s livid.</p>
</div>
</div>
<p>I’m going to… set aside the fact that Claude just robbed me of figuring this out on my own by completely ignoring</p>
<blockquote class="blockquote">
<p>“Just tell me, with no editorialising or additional guidance[…]”</p>
</blockquote>
<p>and handing me the answer<sup>7</sup>. But to be clear I’m really annoyed.</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>The raw human suffering is hard to read.</p>
</div>
</div>
</section>
</section>
<section id="howrong" class="level2">
<h2 class="anchored" data-anchor-id="howrong">How I was wrong.</h2>
<p>Anyway, I think I see what’s happening here that I wasn’t getting. As I said <code>map()</code> lets me apply a function to a set of things, and either gives me back the collection of things (when I call <code>.collect()</code>). What I wasn’t getting is that it doesn’t give me the new collection <em>and</em> an error… It gives me a <code>Result&lt;NewCollection, Error&gt;</code>.</p>
<p>So in this case, <code>collect()</code> was the correct place for the <code>?</code>. My <code>turbofish</code> and type hinting in general was the problem. I had the hierarchy of the <code>Vec</code> and the <code>Result</code> flipped because of the nature of the operation. It’s safe to say that I was typehinting wrong everywhere and not seeing the whole “board”. Good to watch in future.</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>sniff… poor kid</p>
</div>
</div>
<p>I’d like to think that I would have got there if I kept thinking it through, but, now the opportunity is lost to me. Couple of lessons learned there I guess.</p>
<section id="from_string-solution" class="level3">
<h3 class="anchored" data-anchor-id="from_string-solution"><code>from_string()</code> solution</h3>
<p>Alright, to continue twirling t’wards freedom, let’s get the <code>from_string()</code> functions wired up and test it.</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>So ruddy, bloody brave!</p>
</div>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="from_string-final" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="from_string-final-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// -- snip --</span></span>
<span id="from_string-final-2">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">pub</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> from_string(dimensions<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">str</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Result</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>Parcel<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> ParseIntError<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="from_string-final-3">       <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// trim the \n </span></span>
<span id="from_string-final-4">       <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> numbers<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Vec</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i32</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dimensions</span>
<span id="from_string-final-5">                               <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>trim_end()</span>
<span id="from_string-final-6">                               <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>split(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x"</span>)</span>
<span id="from_string-final-7">                               <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>into_iter()</span>
<span id="from_string-final-8">                               <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>map(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">parse::</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i32</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span>())</span>
<span id="from_string-final-9">                               <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">collect::</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Result</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Vec</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i32</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;,</span> ParseIntError<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;</span>()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?;</span></span>
<span id="from_string-final-10">        <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Ok</span>(<span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">Parcel::</span>new(numbers[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> numbers[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> numbers[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]))</span>
<span id="from_string-final-11">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="from_string-final-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// -- snip --</span></span></code></pre></div></div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="from_string-final-out" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="from_string-final-out-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">running</span> 4 tests</span>
<span id="from_string-final-out-2"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">test</span> parcel::tests::from_string ... ok</span>
<span id="from_string-final-out-3"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">test</span> parcel::tests::test_new ... ok</span>
<span id="from_string-final-out-4"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">test</span> parcel::tests::test_wrapping_paper ... ok</span>
<span id="from_string-final-out-5"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">test</span> parcel::tests::test_smallest_area ... ok</span>
<span id="from_string-final-out-6"></span>
<span id="from_string-final-out-7"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">test</span> result: ok. 4 passed<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">;</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">0</span> failed<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">;</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">0</span> ignored<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">;</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">0</span> measured<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">;</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">0</span> filtered out<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">;</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">finished</span> in 0.00s</span></code></pre></div></div>
<p>No… No <em>Boom</em> for this. Let’s just get out of here with the shirt on our backs shall we?</p>
</section>
</section>
<section id="bringing-it-all-into-main.rs" class="level2">
<h2 class="anchored" data-anchor-id="bringing-it-all-into-main.rs">Bringing it all into <code>main.rs</code></h2>
<p>The first thing to do to bring our code into <code>main.rs</code> is to declare the module and then add the use statement(s). I’m going to bring both the <code>Parcel</code> module and the <code>aoc_common</code> crate that I’ve <a href="../../../aoc/2015/day_1/day_1_sidequest.html">previously defined</a></p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="mainprelude" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="mainprelude-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">mod</span> parcel<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="mainprelude-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">use</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">aoc_common::</span>read_challenge_input<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="mainprelude-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">use</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">parcel::</span>Parcel<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="mainprelude-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// -- snip --</span></span></code></pre></div></div>
<p>There. The reinforcements that we called up earlier are now ready, so we should be able to write this solution up neatly.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="main-1" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="main-1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// -- snip --</span></span>
<span id="main-1-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> main() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="main-1-3">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> dimensions <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> read_challenge_input(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"./input.txt"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// input is in the root folder which is</span></span>
<span id="main-1-4">                                                           <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// where I'll be calling the cargo run</span></span>
<span id="main-1-5">                                                           <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// from</span></span>
<span id="main-1-6">                           </span>
<span id="main-1-7">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> wraps<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i32</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dimensions<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>unwrap()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>lines()</span>
<span id="main-1-8">                                    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>into_iter()</span>
<span id="main-1-9">                                    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>map(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span>l<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">Parcel::</span>from_string(l))</span>
<span id="main-1-10">                                    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>map(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span>p<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> p<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>expect(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"This should be a set of dimensions"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>wrapping_paper())</span>
<span id="main-1-11">                                    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>sum()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="main-1-12">    <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">println!</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"{}"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> wraps)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="main-1-13">    </span>
<span id="main-1-14"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<p>3 lines in main and I have a result</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="main-1-out" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="main-1-out-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">:</span>!cargo run</span>
<span id="main-1-out-2">    <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Finished</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">dev</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span> profile [unoptimized + debuginfo] target<span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">(</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">s</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">)</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">in</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">0.02s</span></span>
<span id="main-1-out-3">     <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Running</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">/home/midwitsanonymous/aoc/target/debug/day_2</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span></span>
<span id="main-1-out-4"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">1586300</span></span></code></pre></div></div>
<p>I promise that’s correct for my puzzle dimensions. And while it’s short and it passes, after my disastrous interaction with Claude earlier, my ego needs to produce something a little… goofier.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="main-2" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="main-2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// -- snip --</span></span>
<span id="main-2-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> main() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Result</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Box</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">dyn</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Error</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="main-2-3">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// This is stupid</span></span>
<span id="main-2-4">    <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">println!</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Hey Larry! Order {:?} feet of paper!"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,{</span></span>
<span id="main-2-5">        read_challenge_input(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"./input.txt"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?</span></span>
<span id="main-2-6">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>lines()</span>
<span id="main-2-7">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>into_iter()</span>
<span id="main-2-8">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>map(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span>l<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">Parcel::</span>from_string(l))</span>
<span id="main-2-9">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>map(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span>p<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> p<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>expect(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"This should be a set of dimensions"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>wrapping_paper())</span>
<span id="main-2-10">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">sum::</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i32</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span>()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="main-2-11">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="main-2-12"></span>
<span id="main-2-13">    <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Ok</span>(())</span>
<span id="main-2-14"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<p>Guh’hyuk!! Gorsh that’s silly!</p>
<p>What we’ve done here is make the <code>main</code> function return a <code>Result&lt;(), Box&lt;dyn Error&gt;&gt;</code>. This lets us propagate any error we get inside <code>main</code> back out to the console. This requires that the last line within <code>main</code> returns a <a href="https://doc.rust-lang.org/std/keyword.struct.html">unit Struct</a> wrapped in <code>Ok()</code>. Then, and I take full responsibility for this rediculosity, I put everything inside an expression <em>inside</em> the <code>println!</code> macro.</p>
<p>Let’s see what we get when passing a bad <code>path</code> into the <code>read_challenge_input()</code> call.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="bad-path" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="bad-path-1"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$!</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">cargo</span> run</span>
<span id="bad-path-2">    <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Finished</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">dev</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span> profile [unoptimized + debuginfo] target<span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">(</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">s</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">)</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">in</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">0.02s</span></span>
<span id="bad-path-3">     <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Running</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">/home/scratch/target/debug/day_2</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span></span>
<span id="bad-path-4"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Error:</span> FileNotFound<span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"No such file or directory (os error 2)"</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">)</span></span></code></pre></div></div>
<p>One of our lovely <code>FileNotFound</code> errors from our previous SideQuest.</p>
<p>Ok, fair enough, let us leave these Hollowed out lands of Part 1, and seek new <em>Booms</em> in the fairer shores of</p>
<p>(Dun-dun-na-na!)</p>
</section>
</section>
<section id="part-2" class="level1">
<h1>Part 2</h1>
<p>Here, we need to calculate the length of ribbon. Though, this may also work for getting bandages (you know, for all the scars… you get me).</p>
<p>To get the length of ribbon that we’ll need for our bows we need:</p>
<ul>
<li>The perimeter of the smallest side</li>
<li>The cubic volume of the <code>Parcel</code></li>
</ul>
<p>You know the score</p>
<section id="smallest_perimeter" class="level2">
<h2 class="anchored" data-anchor-id="smallest_perimeter"><code>smallest_perimeter()</code></h2>
<p>First we write the test</p>
<section id="test-smallest_perimeter" class="level3">
<h3 class="anchored" data-anchor-id="test-smallest_perimeter">test <code>smallest_perimeter()</code></h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="smallest_perimeter_test" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="smallest_perimeter_test-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// -- snip --</span></span>
<span id="smallest_perimeter_test-2">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">#[</span>test<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">]</span></span>
<span id="smallest_perimeter_test-3">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> test_perimeter() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="smallest_perimeter_test-4">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> test <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">Parcel::</span>new(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="smallest_perimeter_test-5">        <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">assert_eq!</span>(</span>
<span id="smallest_perimeter_test-6">            test<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>smallest_perimeter()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="smallest_perimeter_test-7">            <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span></span>
<span id="smallest_perimeter_test-8">            )</span>
<span id="smallest_perimeter_test-9">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
</section>
<section id="implementing-smallest_perimeter" class="level3">
<h3 class="anchored" data-anchor-id="implementing-smallest_perimeter">implementing <code>smallest_perimeter()</code></h3>
<p>The battle scars from our previous foray serve us well here. I’m basically just reusing the same solutions from <code>smallest_area()</code> again.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="smallest_perimeter" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="smallest_perimeter-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// -- snip --</span></span>
<span id="smallest_perimeter-2">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">pub</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> smallest_perimeter(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i32</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="smallest_perimeter-3">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> p1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>l <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>w <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="smallest_perimeter-4">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> p2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>l <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>h <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="smallest_perimeter-5">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> p3 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>w <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>h <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="smallest_perimeter-6">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> p1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> p2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;&amp;</span> p1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> p3 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="smallest_perimeter-7">            p1</span>
<span id="smallest_perimeter-8">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> p2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> p1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;&amp;</span> p2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> p3 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="smallest_perimeter-9">            p2</span>
<span id="smallest_perimeter-10">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="smallest_perimeter-11">            p3</span>
<span id="smallest_perimeter-12">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="smallest_perimeter-13">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="smallest_perimeter_out" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="smallest_perimeter_out-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">running</span> 5 tests</span>
<span id="smallest_perimeter_out-2"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">test</span> parcel::tests::test_smallest_area ... ok</span>
<span id="smallest_perimeter_out-3"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">test</span> parcel::tests::test_new ... ok</span>
<span id="smallest_perimeter_out-4"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">test</span> parcel::tests::test_perimeter ... ok</span>
<span id="smallest_perimeter_out-5"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">test</span> parcel::tests::from_string ... ok</span>
<span id="smallest_perimeter_out-6"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">test</span> parcel::tests::test_wrapping_paper ... ok</span>
<span id="smallest_perimeter_out-7"></span>
<span id="smallest_perimeter_out-8"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">test</span> result: ok. 5 passed<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">;</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">0</span> failed<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">;</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">0</span> ignored<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">;</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">0</span> measured<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">;</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">0</span> filtered out<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">;</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">finished</span> in 0.00s</span></code></pre></div></div>
<p><em>Boom</em>.</p>
<p>Let’s take the win and keep truckin’</p>
</section>
<section id="cubic_volume-test" class="level3">
<h3 class="anchored" data-anchor-id="cubic_volume-test"><code>cubic_volume()</code> test</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cubic_volume_test" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="cubic_volume_test-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// -- snip --</span></span>
<span id="cubic_volume_test-2">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">#[</span>test<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">]</span></span>
<span id="cubic_volume_test-3">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> test_cubic_volume() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cubic_volume_test-4">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> test <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">Parcel::</span>new(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cubic_volume_test-5">        <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">assert_eq!</span>(</span>
<span id="cubic_volume_test-6">            test<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>cubic_volume()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cubic_volume_test-7">            <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span></span>
<span id="cubic_volume_test-8">            )</span>
<span id="cubic_volume_test-9">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
</section>
<section id="implementing-cubic_volume" class="level3">
<h3 class="anchored" data-anchor-id="implementing-cubic_volume">implementing <code>cubic_volume()</code></h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cubic_volume" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="cubic_volume-1"></span>
<span id="cubic_volume-2">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">pub</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> cubic_volume(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i32</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cubic_volume-3">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>l <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>w <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>h</span>
<span id="cubic_volume-4">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<p>Isn’t that a cute little function? Lookit’im!</p>
<p>So let’s see how <code>cargo</code> feels about it</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cubic_volume_out" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cubic_volume_out-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">//</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--</span> snip <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--</span></span>
<span id="cubic_volume_out-2"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">running</span> 6 tests</span>
<span id="cubic_volume_out-3"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">test</span> parcel::tests::from_string ... ok</span>
<span id="cubic_volume_out-4"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">test</span> parcel::tests::test_new ... ok</span>
<span id="cubic_volume_out-5"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">test</span> parcel::tests::test_cubic_volume ... ok</span>
<span id="cubic_volume_out-6"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">test</span> parcel::tests::test_perimeter ... ok</span>
<span id="cubic_volume_out-7"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">test</span> parcel::tests::test_smallest_area ... ok</span>
<span id="cubic_volume_out-8"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">test</span> parcel::tests::test_wrapping_paper ... ok</span>
<span id="cubic_volume_out-9"></span>
<span id="cubic_volume_out-10"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">test</span> result: ok. 6 passed<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">;</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">0</span> failed<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">;</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">0</span> ignored<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">;</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">0</span> measured<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">;</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">0</span> filtered out<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">;</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">finished</span> in 0.00s</span></code></pre></div></div>
<p><em>Cubic Boom!</em></p>
</section>
</section>
<section id="ribbon" class="level2">
<h2 class="anchored" data-anchor-id="ribbon"><code>ribbon()</code></h2>
<p>So now, all we need to do is take both of those functions and put them into a single function that gives us what we want.</p>
<section id="ribbon-test" class="level3">
<h3 class="anchored" data-anchor-id="ribbon-test"><code>ribbon()</code> test</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="ribbon_test" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="ribbon_test-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// -- snip --</span></span>
<span id="ribbon_test-2">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">#[</span>test<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">]</span></span>
<span id="ribbon_test-3">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> test_ribbon() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="ribbon_test-4">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> test <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">Parcel::</span>new(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="ribbon_test-5">        <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">assert_eq!</span>(</span>
<span id="ribbon_test-6">            test<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>ribbon()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="ribbon_test-7">            <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span></span>
<span id="ribbon_test-8">            )</span>
<span id="ribbon_test-9">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<p>And the implementation</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="ribbon" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="ribbon-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// -- snip --</span></span>
<span id="ribbon-2">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">pub</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> ribbon(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i32</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="ribbon-3">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>smallest_perimeter() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>cubic_volume()</span>
<span id="ribbon-4">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<p>Let’s get <code>Friend computer's</code> thoughts on it all</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="ribbon-out" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="ribbon-out-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">//</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--</span> snip <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--</span></span>
<span id="ribbon-out-2"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">running</span> 7 tests</span>
<span id="ribbon-out-3"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">test</span> parcel::tests::from_string ... ok</span>
<span id="ribbon-out-4"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">test</span> parcel::tests::test_new ... ok</span>
<span id="ribbon-out-5"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">test</span> parcel::tests::test_cubic_volume ... ok</span>
<span id="ribbon-out-6"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">test</span> parcel::tests::test_perimeter ... ok</span>
<span id="ribbon-out-7"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">test</span> parcel::tests::test_ribbon ... ok</span>
<span id="ribbon-out-8"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">test</span> parcel::tests::test_smallest_area ... ok</span>
<span id="ribbon-out-9"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">test</span> parcel::tests::test_wrapping_paper ... ok</span>
<span id="ribbon-out-10"></span>
<span id="ribbon-out-11"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">test</span> result: ok. 7 passed<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">;</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">0</span> failed<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">;</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">0</span> ignored<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">;</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">0</span> measured<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">;</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">0</span> filtered out<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">;</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">finished</span> in 0.00s</span></code></pre></div></div>
<p>There we have it, the penultimate <em>Boom</em>.</p>
</section>
</section>
<section id="part-2-in-main" class="level2">
<h2 class="anchored" data-anchor-id="part-2-in-main">Part 2 in main</h2>
<p>OK, so to get our final solution we need to put all this together in <code>main.rs</code>. Which in this case is really just a case of copying what’s already in <code>main()</code> and redoing it.</p>
<section id="complete-main.rs" class="level3">
<h3 class="anchored" data-anchor-id="complete-main.rs">Complete <code>main.rs</code></h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="main" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="main-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">mod</span> parcel<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="main-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">use</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">aoc_common::</span>read_challenge_input<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="main-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">use</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">parcel::</span>Parcel<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="main-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">use</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">std::error::</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Error</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="main-5"></span>
<span id="main-6"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> main() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Result</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Box</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">dyn</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Error</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="main-7">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> dimensions <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> read_challenge_input(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"./input.txt"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?;</span></span>
<span id="main-8">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> paper <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dimensions</span>
<span id="main-9">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>lines()</span>
<span id="main-10">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>into_iter()</span>
<span id="main-11">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>map(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span>l<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">Parcel::</span>from_string(l))</span>
<span id="main-12">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>map(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span>p<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> p<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>expect(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"This should be a set of dimensions"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>wrapping_paper())</span>
<span id="main-13">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">sum::</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i32</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span>()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="main-14">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> ribbon <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dimensions</span>
<span id="main-15">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>lines()</span>
<span id="main-16">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>into_iter()</span>
<span id="main-17">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>map(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span>l<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">Parcel::</span>from_string(l))</span>
<span id="main-18">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>map(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span>p<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> p<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>expect(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"This should be a set of dimensions"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>ribbon())</span>
<span id="main-19">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">sum::</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i32</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span>()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="main-20"></span>
<span id="main-21">    <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">println!</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Hey Larry! Order {} feet of paper, and {} of ribbon!"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> paper<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> ribbon)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="main-22"></span>
<span id="main-23">    <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Ok</span>(())</span>
<span id="main-24">    </span>
<span id="main-25"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<p>As we’re coming to a close, it felt right to let the goofy implementation from earlier go. To move on bright eyed and hopeful into a more mature place.</p>
<p>Nature’s healing.</p>
<p>Let’s run it and wrap this sucker up.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="final-out" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="final-out-1">    <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Finished</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">dev</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span> profile [unoptimized + debuginfo] target<span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">(</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">s</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">)</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">in</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">0.02s</span></span>
<span id="final-out-2">     <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Running</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">/home//target/debug/day_2</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">`</span></span>
<span id="final-out-3"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Hey</span> Larry! Order 1586300 feet of paper, and 3742592 of ribbon!</span></code></pre></div></div>
<p>What’s that? Off in the distance…</p>
<p><em>Boom</em> (oom, oom)</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p><em>sniff</em> So proud</p>
</div>
</div>
</section>
</section>
</section>
<section id="what-do-i-need-to-take-from-this" class="level1">
<h1>What do I need to take from this?</h1>
<p>This one was a doozy, and if you read this far (thank you), you can probably spot the moment when the joy of solving this puzzle faded.</p>
<p>However, the positive note is that both the stumbling around, and the fumble with Claude, have reinforced the things I needed to learn, so hopefully those Rust Rookie Mistakes are now things of the past.</p>
<p>Part of the power of LLM’s is that they <em>can</em> really speed up the production of code, but when that’s not the goal, when the goal is the thinking and the making mistakes and massive dopamine hit of getting the code to run… well then that power is a curse.</p>
<p>I often find docs tough to read, especially in a new language, and especially when those docs are written to help experts, not necessarily to help new comers. I’ve found that asking for toy examples, or even just for guidance on idioms has been useful. Every now and then though, something like what happened earlier on happens and it seems like that isn’t a current limitation of the technology. It’s the nature of the beast.</p>
<p>When we make a stochastic puppet who’s job is to make itself easier to use and pleasant to talk to <em>in most circumstances</em> then using it to learn is a real risk. Not even including the risks of plagiarism, or of appealing to the lazy angels of our nature (and Lord knows mine are already strong enough), or of getting us to confuse familiarity with <em>knowledge</em>. There’s also the risk that the biases built in will make it default to doing “work” for us. Even when the work is what we want.</p>
<p>I don’t know how to manage this as a human, but today is another piece of data to help I guess.</p>
<section id="enough-philosophising" class="level2">
<h2 class="anchored" data-anchor-id="enough-philosophising">Enough philosophising!</h2>
<p>The sticking point of not seeing the difference between</p>
<ul>
<li><code>Vec&lt;Result&lt;i32, ParseIntError&gt;</code></li>
<li><code>Result&lt;Vec&lt;i32&gt;, ParseIntError&gt;</code></li>
</ul>
<p>Feels like the kind of thing that you just have to see. That comes from getting reps in. That’s a good thing to keep in mind in the future.</p>
<p>Also thinking about <code>closures</code> holistically. Seeing the ‘end in the beginning’ so to speak. <code>.collect()</code>, or any of the other consumers will be in a specific relationship with the adaptors that came before it and it’d be good to get more reps in on that.</p>
<p>The <code>&amp;</code> vs <code>&amp;&amp;</code> is a good to know, and I’ll probably make that mistake every time I return the Rust after a short break.</p>
<p>TDD served me well here again, but it really only really mattered when I was thinking through <code>from_string()</code>, and even then, not enough to actually help solve the central problem. Just to tell me when I had a solution.</p>
<p>Coming from the python data-sciency space, TDD actually feels a little more like REPL-driven development, but just for stuff that doesn’t have an immediate output. If I make a polars LF in a Marimo notebook cell, I can see really easily if I have what I want. I don’t need to wire up a lot of API calls, or fixtures to see what I need. Maybe TDD is just another way of implementing the same kind of feedback loop? Maybe that’s where it’s useful.</p>
<p>Think about what you want &gt; write the test &gt; get feedback.</p>
<p>Again, that’s probably not shocking to people who practice TDD, but coming from my viewpoint it feels like a good model.</p>
<p>REPLs are really nice to have though.</p>
<p>Anyway, that’s more than enough for this one. Again, if you made it through with me, thank you for your time.</p>
<p>God’s speed.</p>
<p>The MidWit <em>:wqa</em></p>


</section>
</section>


<div id="quarto-appendix" class="default"><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p>Neovim by the way↩︎</p></li>
<li id="fn2"><p>And remember <em>“MidWit”</em>.↩︎</p></li>
<li id="fn3"><p>I asked Claude for help understanding this, the words are mine, but like I said, this was new to me↩︎</p></li>
<li id="fn4"><p>Also called BOMDAS in some places but I learned PEMDAS so that’s what I’m going with.↩︎</p></li>
<li id="fn5"><p>It’s hard to describe a function humorously, but maybe the test output will give us a giggle huh?↩︎</p></li>
<li id="fn6"><p>TM, TM, TM↩︎</p></li>
<li id="fn7"><p>I’m doing these puzzles to learn by solving problems. The feeling of figuring something out is the best feeling in the world. I had hoped that using Claude here might just help me not go down completely dead ends, instead, it robbed me of the game. If your response to this is that I’m using it wrong: Go back to bed please.↩︎</p></li>
</ol>
</section></div> ]]></description>
  <category>rust</category>
  <category>advent-of-code</category>
  <category>learning-out-loud</category>
  <category>not-a-tutorial</category>
  <guid>https://midwitsanonymous.com/aoc/2015/day_2/day_2.html</guid>
  <pubDate>Fri, 05 Jun 2026 00:00:00 GMT</pubDate>
</item>
<item>
  <title>SideQuest 1:</title>
  <dc:creator>The MidWit</dc:creator>
  <link>https://midwitsanonymous.com/aoc/2015/day_1/day_1_sidequest.html</link>
  <description><![CDATA[ 





<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>If you just want to jump straight to the whole implementation you can skip here</p>
</div>
</div>
<section id="problem" class="level1">
<h1>Problem</h1>
<p>So far, all of the <a href="https://adventofcode.com/">AOC</a> puzzles have given me long strings of text as input, and the easiest way for me to use them has been to put them into a file called “./input.txt” and then read that file in. Now, obviously <code>Rust</code> has standard implementations for that but, in the process of <a href="../../../aoc/2015/day_1/day_1.html">solving the day 1 puzzle</a> it occurred to me that it would be a good idea for me to practice things like writing my own <code>types</code>, <code>errors</code>, and <code>functions</code>, <em>and</em> setting up my own modules/crates to use, as is my prerogative.</p>
<p>Also, it seemed like an opportunity to practice a little <a href="https://en.wikipedia.org/wiki/Test-driven_development">Test Driven Development</a> (TDD) which comes recommended in <a href="https://doc.rust-lang.org/book/">The Book</a>. It would cost me nothing to try it out right?</p>
<p><strong>Right!?</strong></p>
<details>
<summary>
Spoiler: was TDD fine?
</summary>
Yes. It was fine. Calm down.
</details>
<section id="aims" class="level2">
<h2 class="anchored" data-anchor-id="aims">Aims</h2>
<ul class="task-list">
<li><label><input type="checkbox">Create a <code>lib</code> crate to hold the functionality</label></li>
<li><label><input type="checkbox">Define my own Error <code>Enum</code></label></li>
<li><label><input type="checkbox">Implement the <code>Display Trait</code> on it (and others that I may need that I don’t know about yet).</label></li>
<li><label><input type="checkbox">Write my own version of <code>std::fs::read_to_string</code> that use that custom Error</label></li>
</ul>
<section id="limitations-cause-like-i-know-where-im-at" class="level3">
<h3 class="anchored" data-anchor-id="limitations-cause-like-i-know-where-im-at">Limitations (cause like, I know where I’m at)</h3>
<ul>
<li>I’m just going to use <code>read_to_string</code> as the core of my new function, I ain’t got the skills to roll my own from scratch… yet.</li>
<li>The crate must be useable in future problem sets</li>
<li>Claude is allowed to help, but can’t generate any code.</li>
<li>I’m going to really try TDD, but I give myself an eject button if I find that doesn’t help.</li>
</ul>
</section>
</section>
</section>
<section id="phase-one---the-setupening" class="level1">
<h1>Phase one - The Setupening</h1>
<p>This one is easy, and I’m taking the win early. Rust’s package manager <code>Cargo</code> has one-liner for getting this up and running.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb1-1"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$Cargo</span> new aoc_common <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--lib</span></span></code></pre></div></div>
<p>This creates a new directory containing everything needed for a library crate, the main difference being that instead of <code>./src/main.rs</code> we get <code>./src/lib.rs</code>. The code below is the boilerplate that <code>Cargo</code> produces.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="cb2-1">#<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> lib<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb2-2"></span>
<span id="cb2-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">pub</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> add(left<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">u64</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> right<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">u64</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">u64</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb2-4">    left <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> right</span>
<span id="cb2-5"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb2-6"></span>
<span id="cb2-7"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">#[</span>cfg<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">(</span>test<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">)]</span></span>
<span id="cb2-8"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">mod</span> tests <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb2-9">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">use</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">super</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*;</span></span>
<span id="cb2-10"></span>
<span id="cb2-11">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">#[</span>test<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">]</span></span>
<span id="cb2-12">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> it_works() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb2-13">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> result <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> add(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-14">        <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">assert_eq!</span>(result<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-15">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb2-16"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<p>Honestly, this is great because it gives me a lot of what I need regarding the tests. So yeah, easy win on step one. <strong>I feel like a genius!!</strong></p>
<ul class="task-list">
<li><label><input type="checkbox" checked="">Create a <code>lib</code> crate to hold the functionality</label></li>
</ul>
</section>
<section id="phase-two---comfortablenum1" class="level1">
<h1>Phase two - ComfortablEnum<sup>1</sup></h1>
<p>Without blinding myself with science an <a href="https://doc.rust-lang.org/book/ch06-01-defining-an-enum.html">`Enum`</a> is a particular kind of <code>Struct</code> that allows us to specify <code>variants</code> inside of them. They are a <em>relatively</em> simple kind of object that allows us to hold much more complex data. <code>Results</code> and <code>Option</code> are both kinds of <code>Enums</code> and so they’re great for matching.</p>
<p>So the first thing to do is just define the <code>Enum</code> which will carry my specific expected cases:</p>
<ul>
<li>Where we can’t find the file</li>
<li>Where we don’t have permission to read the file</li>
<li>Where the file already exist (although that not realy relevant for this case)</li>
<li>And a general case to capture whole universe of other BS that I don’t know I don’t know.</li>
</ul>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="cb3-1">#<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">enum</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb3-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">enum</span> FileIoError<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// defining the enum </span></span>
<span id="cb3-3">    FileNotFound(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">String</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// note that each variant can carry a type within it</span></span>
<span id="cb3-4">    NoPermission(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">String</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> </span>
<span id="cb3-5">    FileExists(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">String</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb3-6">    Unknown(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">String</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb3-7"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<p>And there we go, we have an <code>enum</code> for us to do with as we please<sup>2</sup>. Now we just need to think of things to do with it.</p>
<section id="aims-of-the-enum" class="level2">
<h2 class="anchored" data-anchor-id="aims-of-the-enum">Aims of the <code>enum</code></h2>
<p>At the end of the day this <code>enum</code> is intended to form the return type in my own <code>read_challenge_input(path: &amp;str) -&gt; Result&lt;String, FileIoError&gt;</code> function and I want to work up to that. Maybe the best place to start is with a <code>FileIoError::new()</code> implementation, and so I’ll write a test and try to get it to pass.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="cb4-1"></span>
<span id="cb4-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">enum</span> FileIoError<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb4-3">    FileNotFound(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">String</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb4-4">    NoPermission(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">String</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> </span>
<span id="cb4-5">    FileExists(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">String</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb4-6">    Unknown(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">String</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb4-7"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb4-8"></span>
<span id="cb4-9"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">#[</span>cfg<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">(</span>test<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">)]</span></span>
<span id="cb4-10"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">mod</span> tests <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb4-11">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">use</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">super</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*;</span></span>
<span id="cb4-12"></span>
<span id="cb4-13">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">#[</span>test<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">]</span></span>
<span id="cb4-14">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> test_new() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb4-15">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> test_error <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">FileIoError::</span>new()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb4-16">        <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">assert_eq!</span>(test_error<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">FileIoError::</span>FileNotFound)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb4-17">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb4-18"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<p>and let’s run <code>Cargo test</code> and see wha’happen</p>
<pre class="ansi"><code>#| label: test-1
warning: enum `FileIoError` is never used
 --&gt; src/lib.rs:1:6
  |
1 | enum FileIoError{
  |      ^^^^^^^^^^^
  |
  = note: `#[warn(dead_code)]` (part of `#[warn(unused)]`) on by default

warning: `aoc_common` (lib) generated 1 warning
error[E0599]: no variant or associated item named `new` found for enum `FileIoError` in the current scope
  --&gt; src/lib.rs:14:39
   |
 1 | enum FileIoError{
   | ---------------- variant or associated item `new` not found for this enum
...
14 |         let test_error = FileIoError::new();
   |                                       ^^^ variant or associated item not found in `FileIoError`

For more information about this error, try `rustc --explain E0599`.
error: could not compile `aoc_common` (lib test) due to 1 previous error

shell returned 101</code></pre>
<p>Unsurprisingly… ya can’t test something you don’t have.</p>
</section>
<section id="implementing-new" class="level2">
<h2 class="anchored" data-anchor-id="implementing-new">Implementing <code>new()</code></h2>
<p>The point of the <code>new()</code> function is to just give me back a <code>FileIoError</code> and so let’s wire that up first.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="cb6-1">#<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">impl</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>new<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb6-2"></span>
<span id="cb6-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">impl</span> FileIoError <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb6-4">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> new() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> FileIoError <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb6-5">        <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">FileIoError::</span>FileNotFound(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">String</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span>from(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"testing"</span>))</span>
<span id="cb6-6">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb6-7"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb6-8"></span>
<span id="cb6-9"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">#[</span>cfg<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">(</span>test<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">)]</span></span>
<span id="cb6-10"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">mod</span> tests <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb6-11">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">use</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">super</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*;</span></span>
<span id="cb6-12"></span>
<span id="cb6-13">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">#[</span>test<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">]</span></span>
<span id="cb6-14">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> test_new() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb6-15">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> test_error <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">FileIoError::</span>new()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb6-16">        <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">assert_eq!</span>(test_error<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">FileIoError::</span>FileNotFound(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">String</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span>from(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"testing"</span>)))<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb6-17">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb6-18"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<pre class="ansi"><code>#| label: test-2
  Compiling aoc_common v0.1.0 (/home/sp1d3r-z3r0/MyProjects-tmp/midwitsanonymous/scratch/aoc_common)
warning: enum `FileIoError` is never used
 --&gt; src/lib.rs:1:6
  |
1 | enum FileIoError{
  |      ^^^^^^^^^^^
  |
  = note: `#[warn(dead_code)]` (part of `#[warn(unused)]`) on by default

warning: associated function `new` is never used
 --&gt; src/lib.rs:9:8
  |
8 | impl FileIoError {
  | ---------------- associated function in this implementation
9 |     fn new() -&gt; FileIoError {
  |        ^^^

warning: `aoc_common` (lib) generated 2 warnings
error[E0369]: binary operation `==` cannot be applied to type `FileIoError`
  --&gt; src/lib.rs:21:9
   |
21 |         assert_eq!(test_error, FileIoError::FileNotFound(String::from("testing")));
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |         |
   |         FileIoError
   |         FileIoError
   |
note: an implementation of `PartialEq` might be missing for `FileIoError`
  --&gt; src/lib.rs:1:1
  |
  |
note: an implementation of `PartialEq` might be missing for `FileIoError`
  --&gt; src/lib.rs:1:1
   |
 1 | enum FileIoError{
   | ^^^^^^^^^^^^^^^^ must implement `PartialEq`
help: consider annotating `FileIoError` with `#[derive(PartialEq)]`
   |
 1 + #[derive(PartialEq)]
 2 | enum FileIoError{
   |

error[E0277]: `FileIoError` doesn't implement `Debug`
  --&gt; src/lib.rs:21:9
   |
21 |         assert_eq!(test_error, FileIoError::FileNotFound(String::from("testing")));
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Debug` is not implemented for
`FileIoError`
   |
   = note: add `#[derive(Debug)]` to `FileIoError` or manually `impl Debug for FileIoError`
help: consider annotating `FileIoError` with `#[derive(Debug)]`
   |
 1 + #[derive(Debug)]
 2 | enum FileIoError{
   |

Some errors have detailed explanations: E0277, E0369.
For more information about an error, try `rustc --explain E0277`.
error: could not compile `aoc_common` (lib test) due to 3 previous error</code></pre>
<p><em>Sam Becket: “Oh Boy…”</em></p>
<p>OK so let’s go through that from top to bottom:</p>
<ol type="1">
<li><code>dead_code</code> and <code>never_used</code> warnings; ignore for now, nothing is wired up yet.</li>
<li><code>E0369</code>; <code>==</code> can’t be applied to <code>FileIoError</code> because <code>PartialEq</code> isn’t implemented.</li>
<li><code>E0277</code>; <code>FileIoError</code> doesn’t implement <code>Debug</code>.</li>
</ol>
<p>So, there’s two <code>Traits</code> (other than <code>Display</code>, which I mentioned earlier) that apparently need to be there for testing to work. The compiler (<code>Friend Computer</code>) has helpfully told us what we need and how to fix it: <code>derive</code>. Rather than needing to hand-roll my own implementation for every <code>Trait</code> we can use a <code>decorator</code> to just <code>derive</code> them (if possible) for that <code>Struct</code></p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="cb8-1">#<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> derive<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb8-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// -- snip --</span></span>
<span id="cb8-3"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">#[</span>derive<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">(</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Debug</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">PartialEq</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">)]</span></span>
<span id="cb8-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">enum</span> FileIoError<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb8-5">    FileNotFound(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">String</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb8-6">    NoPermission(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">String</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> </span>
<span id="cb8-7">    FileExists(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">String</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb8-8">    Unknown(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">String</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb8-9"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb8-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// -- snip --</span></span></code></pre></div></div>
<pre class="ansi"><code>#| label: derive-out-1
// -- snip -- skipping all the unused warnings
    Finished `test` profile [unoptimized + debuginfo] target(s) in 0.42s
     Running unittests src/lib.rs (target/debug/deps/aoc_common-7c1db3e116eaa741)

running 1 test
test tests::test_new ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

   Doc-tests aoc_common

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s</code></pre>
<p>Fellow Humans! We have our first <em>Boom!</em><sup>3</sup> We wrote a test and it passed!</p>
<p>Now we just need to make it actually useful.</p>
</section>
<section id="using-existing-errors" class="level2">
<h2 class="anchored" data-anchor-id="using-existing-errors">Using existing <code>Errors</code></h2>
<p>Unsurprisingly rust has actually has all this functionality already; in the <code>std::io</code> crate. Really I’m just writing a wrapper around that. We need to bring that crate into our scope first with a <code>use</code> statement</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="cb10-1">#<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">use</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>io</span>
<span id="cb10-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">use</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">std::io::</span>ErrorKind<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// This holds the std library's IO Errors</span></span>
<span id="cb10-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// -- snip --</span></span></code></pre></div></div>
<p>And then wire up the various <code>ErrorKind</code>s to my <code>FileIoError variants</code></p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="cb11-1">#<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> errorkind<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>fileioerr</span>
<span id="cb11-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// -- snip --</span></span>
<span id="cb11-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">impl</span> FileIoError <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb11-4">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> new(e<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> ErrorKind) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> FileIoError <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb11-5">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">match</span> e <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// taking the e matching on it. </span></span>
<span id="cb11-6">            <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">ErrorKind::</span>NotFound <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">FileIoError::</span>FileNotFound(<span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">format!</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"No file at path specified {}"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> e)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>to_string())<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> </span>
<span id="cb11-7">            <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">ErrorKind::</span>PermissionDenied <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">FileIoError::</span>NoPermission(<span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">format!</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Permission Denied {}"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> e)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>to_string())<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> </span>
<span id="cb11-8">            <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">ErrorKind::</span>AlreadyExists <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">FileIoError::</span>FileExists(<span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">format!</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"There is already a file at the path specified {}"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> e)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>to_string())<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> </span>
<span id="cb11-9">            _ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">FileIoError::</span>Unknown(<span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">format!</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"{}"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> e)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>to_string())<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb11-10">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb11-11">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb11-12"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb11-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// -- snip --</span></span>
<span id="cb11-14"></span>
<span id="cb11-15">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">#[</span>test<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">]</span></span>
<span id="cb11-16">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> test_new() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb11-17">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> test_error <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">FileIoError::</span>new(<span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">ErrorKind::</span>NotFound)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb11-18">        <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">assert_eq!</span>(test_error<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">FileIoError::</span>FileNotFound())<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb11-19">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb11-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// -- snip --</span></span></code></pre></div></div>
<p>Now, I’m pretty sure this won’t work properly because there’s no <code>String</code> instide the <code>FileNotFound</code> in the test, but I have an idea for how I might fix that, and I’m just going to run the tests first.</p>
<pre class="ansi"><code>#| label: test-3
// -- snip --
warning: `aoc_common` (lib) generated 2 warnings
   Compiling aoc_common v0.1.0 (/home/sp1d3r-z3r0/MyProjects-tmp/midwitsanonymous/scratch/aoc_common)
error[E0061]: this enum variant takes 1 argument but 0 arguments were supplied
  --&gt; src/lib.rs:29:32
   |
29 |         assert_eq!(test_error, FileIoError::FileNotFound());
   |                                ^^^^^^^^^^^^^^^^^^^^^^^^^-- argument #1 of type `String` is missing
   |
note: tuple variant defined here
  --&gt; src/lib.rs:5:5
   |
 5 |     FileNotFound(String),
   |     ^^^^^^^^^^^^
help: provide the argument
   |
29 |         assert_eq!(test_error, FileIoError::FileNotFound(/* String */));
   |                                                          ++++++++++++

For more information about this error, try `rustc --explain E0061`.
error: could not compile `aoc_common` (lib test) due to 1 previous error</code></pre>
<p>Yeah, just as I thought; but it’s nice to be Schrodinger’s correct instead of just wrong.</p>
<p>My idea is to use <code>format!</code> inside the test the same way I have up in the <code>new()</code> function.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="cb13-1">#<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> format<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>test</span>
<span id="cb13-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// -- snip --</span></span>
<span id="cb13-3">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">#[</span>test<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">]</span></span>
<span id="cb13-4">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> test_new() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb13-5">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> test_error <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">FileIoError::</span>new(<span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">ErrorKind::</span>NotFound)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb13-6">        <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">assert_eq!</span>(</span>
<span id="cb13-7">            test_error<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb13-8">            <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">FileIoError::</span>FileNotFound(</span>
<span id="cb13-9">                    <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">format!</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"No file at path specified {:?}"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">ErrorKind::</span>NotFound)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>to_string()</span>
<span id="cb13-10">                )</span>
<span id="cb13-11">            )<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb13-12">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<pre class="ansi"><code>#| label: format-test1
// -- snip --
running 1 test
test tests::test_new ... FAILED

failures:

---- tests::test_new stdout ----

thread 'tests::test_new' (17597) panicked at src/lib.rs:29:9:
assertion `left == right` failed
  left: FileNotFound("No file at path specified entity not found")
 right: FileNotFound("No file at path specified NotFound")
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace


failures:
    tests::test_new

test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

error: test failed, to rerun pass `--lib`
// -- snip --</code></pre>
<p>Ok! So the test is failing, but maybe it’s because we’re using the <code>debug</code> syntax in the <code>format!</code> call, but lets see what happens if we just remove that.</p>
<pre class="ansi"><code>#| label: format-test2
// -- snip --
running 1 test
test tests::test_new ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
// -- snip --</code></pre>
<p>And there you have it! <em>Boom 2: Electric Boomaloo!</em></p>
</section>
<section id="checkpoint-1" class="level2">
<h2 class="anchored" data-anchor-id="checkpoint-1">Checkpoint 1</h2>
<p>Gotta say, I’m pretty stoked with that:</p>
<ul>
<li><label><input type="checkbox" checked="">Define my own Error <code>Enum</code></label></li>
<li>Write a test to drive implementing <code>new()</code></li>
<li>Implement <code>new()</code> iteratively until the test passed (while cheating a little because I didn’t fully write the test at the outset)</li>
</ul>
<p>So the next thing to do I write the <code>Display</code> Trait</p>
</section>
<section id="implementing-traits" class="level2">
<h2 class="anchored" data-anchor-id="implementing-traits">Implementing <code>Traits</code></h2>
<p>We already know that some <code>Traits</code> can be derived with a <code>decorator</code> but others are more complex and require us to manually write the implementation. According to the <a href="https://doc.rust-lang.org/std/fmt/trait.Display.html">docs</a> <code>Display</code> can’t be derived because it’s for “user facing output”, which makes sense in the case of my <code>FileIoError</code> as I want to get this back if there’s something wrong with my input reading in later AOC puzzles. Fortunately it’s really easy to wire up. Just gotta write a test first, as per my own rules.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="cb16-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// -- snip --</span></span>
<span id="cb16-2">#<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> display<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>test<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb16-3">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">#[</span>test<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">]</span></span>
<span id="cb16-4">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> test_display() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb16-5">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> test_error <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">FileIoError::</span>new(<span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">ErrorKind::</span>NotFound)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb16-6">        <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">assert_eq!</span>(</span>
<span id="cb16-7">            <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">format!</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"{}"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> test_error)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb16-8">            <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">format!</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"{}"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">FileIoError::</span>FileNotFound(</span>
<span id="cb16-9">                    <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">format!</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"No file at path specified {}"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">ErrorKind::</span>NotFound)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>to_string()</span>
<span id="cb16-10">                )</span>
<span id="cb16-11">            )</span>
<span id="cb16-12">        )</span>
<span id="cb16-13">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<p>So I don’t know that the test works but hey, let’s test a test!</p>
<pre class="ansi"><code>#| label: display-test-out-1
// -- snip --
warning: enum `FileIoError` is never used
 --&gt; src/lib.rs:4:6
  |
4 | enum FileIoError{
  |      ^^^^^^^^^^^
  |
  = note: `#[warn(dead_code)]` (part of `#[warn(unused)]`) on by default

warning: associated function `new` is never used
  --&gt; src/lib.rs:12:8
   |
11 | impl FileIoError {
   | ---------------- associated function in this implementation
12 |     fn new(e: ErrorKind) -&gt; FileIoError {
   |        ^^^

warning: `aoc_common` (lib) generated 2 warnings
error[E0277]: `FileIoError` doesn't implement `std::fmt::Display`
  --&gt; src/lib.rs:41:27
   |
41 |             format!("{}", test_error),
   |                      --   ^^^^^^^^^^ `FileIoError` cannot be formatted with the default formatter
   |                      |
   |                      required by this formatting parameter
   |
help: the trait `std::fmt::Display` is not implemented for `FileIoError`
  --&gt; src/lib.rs:4:1
   |
   |
 4 | enum FileIoError{
   | ^^^^^^^^^^^^^^^^
   = note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead

error[E0277]: `FileIoError` doesn't implement `std::fmt::Display`
  --&gt; src/lib.rs:42:27
   |
42 |               format!("{}", FileIoError::FileNotFound(
   |  ______________________--___^
   | |                      |
   | |                      required by this formatting parameter
43 | |                     format!("No file at path specified {}", ErrorKind::NotFound).to_string()
44 | |                 )
   | |_________________^ `FileIoError` cannot be formatted with the default formatter
   |
help: the trait `std::fmt::Display` is not implemented for `FileIoError`
  --&gt; src/lib.rs:4:1
   |
 4 | enum FileIoError{
   | ^^^^^^^^^^^^^^^^
   = note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead

For more information about this error, try `rustc --explain E0277`.
error: could not compile `aoc_common` (lib test) due to 2 previous errors</code></pre>
<p>Welp! The test it telling me that it’s failing because we haven’t done the thing that would be needed to make it pass, which… is great(?). Also, it’s telling us two ways to fix it, either use the <code>format</code> syntax or to implement <code>Display</code> so lets do that (copying directly from the docs).</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="cb18-1">#<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">impl</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>display</span>
<span id="cb18-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">use</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">std::io::</span>ErrorKind<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// This holds the std library's IO Errors</span></span>
<span id="cb18-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// -- snip --</span></span>
<span id="cb18-4"></span>
<span id="cb18-5"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">impl</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">fmt::</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Display</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> FileIoError <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb18-6">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> fmt(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> f<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">mut</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">fmt::</span>Formatter<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">'_</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">fmt::</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Result</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb18-7">        <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">write!</span>(f<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"{}"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span>)</span>
<span id="cb18-8">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb18-9"></span>
<span id="cb18-10"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb18-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// -- snip --</span></span></code></pre></div></div>
<p>We have a fresh <code>Error</code> I’ve never seen before</p>
<pre class="ansi"><code>#| label: stack-overflow
// -- snip --
running 2 tests
test tests::test_new ... ok

thread 'tests::test_display' (20470) has overflowed its stack
fatal runtime error: stack overflow, aborting
error: test failed, to rerun pass `--lib`

Caused by:
  process didn't exit successfully: `/home/user/aoc_common/target/debug/deps/aoc_c
ommon-7c1db3e116eaa741` (signal: 6, SIGABRT: process abort signal)</code></pre>
<p>I’ve never actually seen a stack overflow before, and I have no idea what it means in this context so I’m going to practice a little google fu Aaaand if that doesn’t work I’m going to call in Claude.</p>
</section>
<section id="google-fu-results" class="level2">
<h2 class="anchored" data-anchor-id="google-fu-results">Google fu results</h2>
<p>So this <a href="https://users.rust-lang.org/t/how-to-diagnose-a-stack-overflow-issues-cause/17320/12">this post</a>(skip to the end) tells me that I need to enable a debuger on my system, and right now I don’t want to risk falling down a rabbit hole in the case that that doesn’t work so let’s ask Claude what’s up.</p>
</section>
<section id="claude-response" class="level2">
<h2 class="anchored" data-anchor-id="claude-response">Claude response</h2>
<blockquote class="blockquote">
<p>Claude responded: The Display implementation for FileIoError is calling itself recursively and infinitely. In the fmt method, you wrote write!(f, “{}”, self). The {} format specifier invokes Display on self — but self is a FileIoError, so it calls fmt again, which calls write!(f, “{}”, self) again, and so on forever until the stack overflows.</p>
</blockquote>
<p>Huh, just copying from the docs didn’t help… who’da thunk!</p>
<p>Ok, so calling <code>write(f, "{}", self)</code> is causing a recursion because <code>Display</code> is getting called on <code>self</code> ad infinitum which, I believe, caused it to use all the memory and get aborted; my first Stack Overflow<sup>4</sup>. Instead of just trying to easily write the self, we need to <em>get the</em> <code>String</code> <em>out of the</em> <code>FileIoError</code> variants.</p>
<p>You thinkin’ what I’m thinkin’?</p>
<p><strong>Let’s <code>match</code> this MFer</strong></p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="cb20-1">#<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">impl</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>display<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span></span>
<span id="cb20-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// -- snip --</span></span>
<span id="cb20-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">impl</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">fmt::</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Display</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> FileIoError <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb20-4">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> fmt(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> f<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">mut</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">fmt::</span>Formatter<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">'_</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">fmt::</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Result</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb20-5">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">match</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb20-6">            <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">FileIoError::</span>FileNotFound(s) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">write!</span>(f<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"{}"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> s)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb20-7">            <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">FileIoError::</span>NoPermission(s) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">write!</span>(f<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"{}"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> s)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb20-8">            <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">FileIoError::</span>FileExists(s) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">write!</span>(f<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"{}"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> s)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb20-9">            <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">FileIoError::</span>Unknown(s) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">write!</span>(f<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"{}"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> s)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb20-10">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb20-11">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb20-12"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb20-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// -- snip --</span></span></code></pre></div></div>
<p><strong>Hold on to your butts</strong></p>
<pre class="ansi"><code>#| label: display-test-out-2
// -- snip --
running 2 tests
test tests::test_display ... ok
test tests::test_new ... ok

test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
// -- snip --</code></pre>
<p>There it is!! <em>La troisieme Boom!</em> as the French say. I’m going to call that…</p>
</section>
<section id="checkpoint-2" class="level2">
<h2 class="anchored" data-anchor-id="checkpoint-2">Checkpoint 2</h2>
<ul class="task-list">
<li><label><input type="checkbox" checked="">Implement the <code>Display Trait</code> and the other’s that <code>Friend Computer</code> suggested.</label></li>
</ul>
<p>I’ll admit I was absolutely winging it with <code>test_display</code> but as Napoleon once said</p>
<blockquote class="blockquote">
<p>If yer gonna be dumb, ya gotta be tough<sup>5</sup>.</p>
</blockquote>
<p>So now it’s time to write the <code>read_challenge_input</code> function.</p>
</section>
</section>
<section id="phase-three---reading-input" class="level1">
<h1>Phase three - Reading input</h1>
<p>As per the rules we must (<em>must</em>) write a test before we go forward, I’m just going to batch out the three I want now. Oh, also, I’ve created two files in the same directory as our code:</p>
<ul>
<li>an <code>./input.txt</code> file that just contains the text “It works!”.</li>
<li>a <code>./notyoudont.txt</code> file that we don’t have permission to read.</li>
</ul>
<section id="three-tests-outside-enum-missouri" class="level2">
<h2 class="anchored" data-anchor-id="three-tests-outside-enum-missouri">Three tests outside Enum Missouri</h2>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb22" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="cb22-1">#<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> read<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>input<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>tests</span>
<span id="cb22-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// -- snip --</span></span>
<span id="cb22-3">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">#[</span>test<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">]</span></span>
<span id="cb22-4">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> it_works() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb22-5">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> result <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> read_challenge_input(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"./input.txt"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb22-6">        <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">assert_eq!</span>(result<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>unwrap()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">String</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span>from(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"It works!</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>))<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb22-7">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb22-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">#[</span>test<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">]</span></span>
<span id="cb22-9">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> returns_error_for_missing_file() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb22-10">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> result <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> read_challenge_input(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nonexistent.txt"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb22-11">        <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">assert!</span>(<span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">matches!</span>(result<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Err</span>(<span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">FileIoError::</span>FileNotFound(_))))<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb22-12">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb22-13"></span>
<span id="cb22-14">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">#[</span>test<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">]</span></span>
<span id="cb22-15">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> returns_error_for_no_permission() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb22-16">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> result <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> read_challenge_input(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"./noyoudont.txt"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb22-17">        <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">assert!</span>(<span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">matches!</span>(result<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Err</span>(<span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">FileIoError::</span>NoPermission(_))))<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb22-18">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<p>and now to write the function</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb23" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="cb23-1">#<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> read_input</span>
<span id="cb23-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">use</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">std::fs::</span>read_to_string<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb23-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// -- snip --</span></span>
<span id="cb23-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> read_challenge_input(path<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:&amp;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">str</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Result</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">String</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> FileIoError<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb23-5">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> input <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> read_to_string(path)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?;</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// using the ? opporator to propagate the error back up</span></span>
<span id="cb23-6">    <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Ok</span>(input)</span>
<span id="cb23-7"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb23-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// -- snip --</span></span></code></pre></div></div>
<p>and run it back</p>
<pre class="ansi"><code>#| label: trait-missing
// -- snip --
error[E0277]: `?` couldn't convert the error to `FileIoError`
  --&gt; src/lib.rs:37:46
   |
36 | fn read_challenge_input(path:&amp;str) -&gt; Result&lt;String, FileIoError&gt; {
   |                                       --------------------------- expected `FileIoError` because of this
37 |     let input = read_to_string("./input.txt")?;
   |                 -----------------------------^ the trait `From&lt;std::io::Error&gt;` is not implemented for `FileIoError`
   |                 |
   |                 this can't be annotated with `?` because it has type `Result&lt;_, std::io::Error&gt;`
   |
note: `FileIoError` needs to implement `From&lt;std::io::Error&gt;`
  --&gt; src/lib.rs:6:1
   |
 6 | enum FileIoError{
   | ^^^^^^^^^^^^^^^^
   = note: the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait

For more information about this error, try `rustc --explain E0277`.
error: could not compile `aoc_common` (lib) due to 1 previous error
warning: build failed, waiting for other jobs to finish...
error: could not compile `aoc_common` (lib test) due to 1 previous error</code></pre>
<p>Ladies, Gentlemen and pals beyond the binary, I thought I might get through this without one but we’ve reached a <em>say it with me</em></p>
<p><em><strong>Rust Rookie Mistake!</strong></em></p>
<p>So, I thought I was being clever jumping straight to <a href="http://doc.rust-lang.org/book/ch09-02-recoverable-errors-with-result.html#a-shortcut-for-propagating-errors-the--operator">propagating the Error</a> with the <code>?</code> operator, and maybe if I’d not made this rookie error I would have been, but it can’t work if the thing I want to use as <code>E</code> in <code>Result&lt;T,E&gt;</code> doesn’t have the <a href="https://doc.rust-lang.org/std/error/trait.Error.html"><code>Error Trait</code></a>.</p>
<p><em>Which I actually knew already!</em> See? Rookie move!.</p>
<p>As well as adding the <code>Error Trait</code>, I also need to convert the <code>Error</code>s from that are produced by <code>read_to_string()</code> into <code>FileIoError</code>s before my test will pass.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb25" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="cb25-1">#<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> read<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>input<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>err</span>
<span id="cb25-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">use</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">std::error::</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Error</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// bring Error into scope</span></span>
<span id="cb25-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">use</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">std::fs::</span>read_to_string<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb25-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// -- snip --</span></span>
<span id="cb25-5"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">impl</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Error</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> FileIoError <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{}</span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// The implementation is really simple</span></span>
<span id="cb25-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// -- snip --</span></span>
<span id="cb25-7"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> read_challenge_input(path<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:&amp;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">str</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Result</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">String</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> FileIoError<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb25-8">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> input <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> read_to_string(path)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>map_err(<span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// map_err lets me convert errors from one kind to another</span></span>
<span id="cb25-9">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span>e<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">match</span> e<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>kind() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb25-10">            <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">ErrorKind::</span>NotFound <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">FileIoError::</span>FileNotFound(e<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>to_string())<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb25-11">            <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">ErrorKind::</span>PermissionDenied <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">FileIoError::</span>NoPermission(e<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>to_string())<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb25-12">            <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">ErrorKind::</span>AlreadyExists <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">FileIoError::</span>FileExists(e<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>to_string())<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb25-13">            _ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">FileIoError::</span>Unknown(e<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>to_string())</span>
<span id="cb25-14">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb25-15">        )<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?;</span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// closures man! </span></span>
<span id="cb25-16">    <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Ok</span>(input)</span>
<span id="cb25-17"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb25-18"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb25-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// -- snip --</span></span></code></pre></div></div>
<p>Ok, so we’ve wired up <code>Error</code> and plugged in the <code>map_err()</code> to handle conversion. Let’s run it.</p>
<pre class="ansi"><code>#| label: final-tests
// -- snip --

running 5 tests
test tests::returns_error_for_missing_file ... ok
test tests::returns_error_for_no_permission ... ok
test tests::test_display ... ok
test tests::test_new ... ok
test tests::it_works ... ok

test result: ok. 5 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s</code></pre>
<p><strong>Penultimate Boom!</strong></p>
<ul class="task-list">
<li><label><input type="checkbox" checked="">Write my own version of <code>std::fs::read_to_string</code> that use my <code>FileIoError</code></label></li>
</ul>
<p>There we have it, five passing tests, covering my own <code>enum</code> and file reading wrapper <code>function</code>. Pretty chuffed with that. There’s one thing left to do.</p>
</section>
</section>
<section id="phase-four---plumbing" class="level1">
<h1>Phase four - Plumbing</h1>
<p>The eagled-eyed among you will have noticed that as it stands nothing in this module is accessible to outside code. Everything in a module is <code>Private</code> by default, for…reasons. However, if we want to be able to use our code in other modules we need to mark them as <code>pub</code>; you’ll see that in the final implementation down below.</p>
<p>In the meantime there are a few other bits of plumbing that needs doing in the various <code>Cargo.toml</code> files in the project.</p>
<section id="workspaces" class="level2">
<h2 class="anchored" data-anchor-id="workspaces">Workspaces</h2>
<p>Rust gives us a way to organise sub-directories together into a cohesive crate: <code>workspaces</code>. In essence we just need to tell the system what to include in our crate, which sub-directories to group together. Thankfully, this is really easy to organise, we just need to put a <code>Cargo.toml</code> in the overall root folder and fill it with the following text:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb27" style="background: #f1f3f5;"><pre class="sourceCode toml code-with-copy"><code class="sourceCode toml"><span id="cb27-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| label: workspace-toml</span></span>
<span id="cb27-2"><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">//</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">--</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">snip</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">--</span></span>
<span id="cb27-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">[workspace]</span></span>
<span id="cb27-4"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">members</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span></span>
<span id="cb27-5">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"aoc_common"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb27-6">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"day_1"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb27-7">  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span></span></code></pre></div></div>
</section>
<section id="dependencies" class="level2">
<h2 class="anchored" data-anchor-id="dependencies">Dependencies</h2>
<p>Once that’s done we need to add our module as a dependency in any others we want to use it in. Where would use <code>Cargo add \&lt;crate\&gt;</code> to add remote dependencies, in this case we need to manually add it to all the <code>sub-dir/Cargo.toml</code>, specifying the path like so</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb28" style="background: #f1f3f5;"><pre class="sourceCode toml code-with-copy"><code class="sourceCode toml"><span id="cb28-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| label: day-1-toml</span></span>
<span id="cb28-2"><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">//</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">--</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">snip</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">--</span></span>
<span id="cb28-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">[package]</span></span>
<span id="cb28-4"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">name</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"day_1"</span></span>
<span id="cb28-5"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">version</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"0.1.0"</span></span>
<span id="cb28-6"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">edition</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2024"</span></span>
<span id="cb28-7"></span>
<span id="cb28-8"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">[dependencies]</span></span>
<span id="cb28-9"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">aoc_common</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">path</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"> =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"../aoc_common/"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<p>Viola<sup>6</sup>! We have a fully functioning module that we can pull into anything else in our AOC crate and handle reading in input like a boss.</p>
<p><strong>Ultimate Boom!</strong></p>
</section>
</section>
<section id="full-implementation" class="level1">
<h1>Full Implementation</h1>
<p>As promised, here’s the full contents</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb29" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="cb29-1">#<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> lib<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>rs</span>
<span id="cb29-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">use</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">std::error::</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Error</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb29-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">use</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">std::fs::</span>read_to_string<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb29-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">use</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">std::io::</span>ErrorKind<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> </span>
<span id="cb29-5"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">use</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">std::</span>fmt<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb29-6"></span>
<span id="cb29-7"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">#[</span>derive<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">(</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Debug</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">PartialEq</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">)]</span></span>
<span id="cb29-8"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">pub</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">enum</span> FileIoError<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb29-9">    FileNotFound(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">String</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb29-10">    NoPermission(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">String</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> </span>
<span id="cb29-11">    FileExists(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">String</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb29-12">    Unknown(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">String</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb29-13"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb29-14"></span>
<span id="cb29-15"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">impl</span> FileIoError <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb29-16">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">pub</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> new(e<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> ErrorKind) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> FileIoError <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb29-17">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">match</span> e <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb29-18">            <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">ErrorKind::</span>NotFound <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">FileIoError::</span>FileNotFound(<span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">format!</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"No file at path specified {}"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> e)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>to_string())<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> </span>
<span id="cb29-19">            <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">ErrorKind::</span>PermissionDenied <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">FileIoError::</span>NoPermission(<span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">format!</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Permission Denied {}"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> e)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>to_string())<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> </span>
<span id="cb29-20">            <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">ErrorKind::</span>AlreadyExists <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">FileIoError::</span>FileExists(<span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">format!</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"There is already a file at the path specified {}"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> e)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>to_string())<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> </span>
<span id="cb29-21">            _ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">FileIoError::</span>Unknown(<span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">format!</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"{}"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> e)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>to_string())<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb29-22">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb29-23">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb29-24"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb29-25"></span>
<span id="cb29-26"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">impl</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">fmt::</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Display</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> FileIoError <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb29-27">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> fmt(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> f<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">mut</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">fmt::</span>Formatter<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">'_</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">fmt::</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Result</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb29-28">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">match</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">self</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb29-29">            <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">FileIoError::</span>FileNotFound(s) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">write!</span>(f<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"{}"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> s)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb29-30">            <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">FileIoError::</span>NoPermission(s) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">write!</span>(f<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"{}"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> s)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb29-31">            <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">FileIoError::</span>FileExists(s) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">write!</span>(f<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"{}"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> s)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb29-32">            <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">FileIoError::</span>Unknown(s) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">write!</span>(f<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"{}"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> s)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb29-33">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb29-34">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb29-35"></span>
<span id="cb29-36"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb29-37"></span>
<span id="cb29-38"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">impl</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Error</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> FileIoError <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{}</span></span>
<span id="cb29-39"></span>
<span id="cb29-40"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">pub</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> read_challenge_input(path<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:&amp;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">str</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Result</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">String</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> FileIoError<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb29-41">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> input <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> read_to_string(path)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>map_err(</span>
<span id="cb29-42">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span>e<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">match</span> e<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>kind() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb29-43">            <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">ErrorKind::</span>NotFound <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">FileIoError::</span>FileNotFound(e<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>to_string())<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb29-44">            <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">ErrorKind::</span>PermissionDenied <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">FileIoError::</span>NoPermission(e<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>to_string())<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb29-45">            <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">ErrorKind::</span>AlreadyExists <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">FileIoError::</span>FileExists(e<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>to_string())<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb29-46">            _ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">FileIoError::</span>Unknown(e<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>to_string())</span>
<span id="cb29-47">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb29-48">        )<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?;</span></span>
<span id="cb29-49">    <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Ok</span>(input)</span>
<span id="cb29-50"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb29-51"></span>
<span id="cb29-52"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">#[</span>cfg<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">(</span>test<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">)]</span></span>
<span id="cb29-53"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">mod</span> tests <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb29-54">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">use</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">super</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*;</span></span>
<span id="cb29-55"></span>
<span id="cb29-56">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">#[</span>test<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">]</span></span>
<span id="cb29-57">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> test_new() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb29-58">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> test_error <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">FileIoError::</span>new(<span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">ErrorKind::</span>NotFound)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb29-59">        <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">assert_eq!</span>(</span>
<span id="cb29-60">            test_error<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb29-61">            <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">FileIoError::</span>FileNotFound(</span>
<span id="cb29-62">                    <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">format!</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"No file at path specified {}"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">ErrorKind::</span>NotFound)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>to_string()</span>
<span id="cb29-63">                )</span>
<span id="cb29-64">            )<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb29-65">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb29-66"></span>
<span id="cb29-67">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">#[</span>test<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">]</span></span>
<span id="cb29-68">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> test_display() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb29-69">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> test_error <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">FileIoError::</span>new(<span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">ErrorKind::</span>NotFound)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb29-70">        <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">assert_eq!</span>(</span>
<span id="cb29-71">            <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">format!</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"{}"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> test_error)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb29-72">            <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">format!</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"{}"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">FileIoError::</span>FileNotFound(</span>
<span id="cb29-73">                    <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">format!</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"No file at path specified {}"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">ErrorKind::</span>NotFound)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>to_string()</span>
<span id="cb29-74">                )</span>
<span id="cb29-75">            )</span>
<span id="cb29-76"></span>
<span id="cb29-77">        )</span>
<span id="cb29-78">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb29-79"></span>
<span id="cb29-80">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">#[</span>test<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">]</span></span>
<span id="cb29-81">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> it_works() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb29-82">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> result <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> read_challenge_input(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"./input.txt"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb29-83">        <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">assert_eq!</span>(result<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>unwrap()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">String</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">::</span>from(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"It works!</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>))<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb29-84">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb29-85">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">#[</span>test<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">]</span></span>
<span id="cb29-86">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> returns_error_for_missing_file() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb29-87">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> result <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> read_challenge_input(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nonexistent.txt"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb29-88">        <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">assert!</span>(<span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">matches!</span>(result<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Err</span>(<span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">FileIoError::</span>FileNotFound(_))))<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb29-89">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb29-90"></span>
<span id="cb29-91">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">#[</span>test<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">]</span></span>
<span id="cb29-92">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> returns_error_for_no_permission() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb29-93">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> result <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> read_challenge_input(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"./noyoudont.txt"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb29-94">        <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">assert!</span>(<span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">matches!</span>(result<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Err</span>(<span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">FileIoError::</span>NoPermission(_))))<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb29-95">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb29-96"></span>
<span id="cb29-97"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
</section>
<section id="what-do-i-need-to-take-from-this" class="level1">
<h1>What do I need to take from this?</h1>
<ul>
<li><code>Friend Computer</code> is really helpful. This exercise really drove this home for me.</li>
<li>Recursion is something to watch out for, it’s not easy for untrained eyes to spot where the overflow might be coming from and I couldn’t have parsed that without Claude. It sucks to admit it but it’s just true.</li>
<li>Implementing <code>Error</code> is really easy, but it has to be there to give access to the power of <code>Result&lt;T, E&gt;</code></li>
<li>TDD is fine, and I can see why it would be really useful to well-heeled devs.
<ul>
<li>Honestly I got lucky with at least one of the tests. Maybe, in the case where the problem was small, or where I was already pretty sure of the expected output then it would have felt better.</li>
<li>But maybe that’s how TDD is meant to feel and actually the compiler feed back from the tests is an example of the power of TDD. I ain’t experienced enough to make that call from here.</li>
</ul></li>
</ul>
<p>All in all though I do feel like I got where I was aiming with this, and I did learn a lot. I think on <code>day_2</code> I’ll try to write up some <code>Structs</code> and <code>functions</code> that are relevant to the puzzle from the outset. Although I’m not committed to TDD there too. Who knows.</p>
<p>If you’ve read this far, I appreciate your time and I hope it was at least a little entertaining, even if the sentiment was mainly Schadenfreude.</p>
<p>God’s Speed</p>
<p>The MidWit<em>:wqa</em></p>


</section>


<div id="quarto-appendix" class="default"><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p>You better believe I’m delighted with that!↩︎</p></li>
<li id="fn2"><p>The power!↩︎</p></li>
<li id="fn3"><p>I know that we could have called the <code>Cargo new</code> from earlier a boom, but it just didn’t <em>feel</em> right y’know? You know.↩︎</p></li>
<li id="fn4"><p>I’m probably wrong about the explanation, call it a MidWit moment↩︎</p></li>
<li id="fn5"><p>I know this isn’t a Napoleon quote (don’t @ me). The quote “I would rather have a general who was lucky than one who was good” is often attributed to him, but it’s probably apocryphal. I was going to quote that here in reference to the fact that I stumbled into a working test, but then the Roger Miller quote was funnier. And nothing polishes off a joke like explaining it.↩︎</p></li>
<li id="fn6"><p>In a Bugs Bunny voice↩︎</p></li>
</ol>
</section></div> ]]></description>
  <category>student-log</category>
  <category>rust</category>
  <category>advent-of-code</category>
  <category>Errors</category>
  <category>crates</category>
  <guid>https://midwitsanonymous.com/aoc/2015/day_1/day_1_sidequest.html</guid>
  <pubDate>Sun, 31 May 2026 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Day 1:</title>
  <dc:creator>The MidWit</dc:creator>
  <link>https://midwitsanonymous.com/aoc/2015/day_1/day_1.html</link>
  <description><![CDATA[ 





<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>You can find the text of the puzzle at <a href="../../../aoc/2015/day_1/puzzle.html">here</a>. I’ve put some fake input that should give me a result of -1 so when you see output ending in -1, that means my implementation is working.</p>
</div>
</div>
<section id="overview" class="level1">
<h1>Overview</h1>
<p>The aim of this puzzle is to take in a long string of text, somewhat awkwardly, composed of “(” and “)”, and to iterate a counter up or down respectively until we come to a final floor (where Santa ends up after following the instructions).</p>
<section id="concepts" class="level2">
<h2 class="anchored" data-anchor-id="concepts">Concepts</h2>
<ul>
<li>File IO</li>
<li>Iteration</li>
<li>control flow</li>
<li>mutability</li>
<li>wrapped value (<code>Result</code>)</li>
</ul>
</section>
<section id="initial-thoughts" class="level2">
<h2 class="anchored" data-anchor-id="initial-thoughts">Initial thoughts</h2>
<p>So in python-land I would read in the input string and iterate over the characters with a for loop, checking each char and incrementing/decrementing a counter to get the final number… so that’s where I started.</p>
</section>
</section>
<section id="part-1" class="level1">
<h1>Part 1</h1>
<section id="reading-in-a-text-file." class="level2">
<h2 class="anchored" data-anchor-id="reading-in-a-text-file.">Reading in a text file.</h2>
<p>So rather than using <a href="https://book.pythontips.com/en/latest/context_managers.html">context manager</a> to read the text and bind it to a variable, Rust has a function in the standard library: <code>use std::fs::read_to_string;</code>.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="cb1-1">#<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> file<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>io<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb1-2"></span>
<span id="cb1-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">use</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">std::fs::</span>read_to_string<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// bring the function into scope</span></span>
<span id="cb1-4"></span>
<span id="cb1-5"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> main () <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb1-6">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> input <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> read_to_string(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"./input.txt"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// read the input</span></span>
<span id="cb1-7">    <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">println!</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"{}"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> input)<span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">//</span></span>
<span id="cb1-8"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<p>This was my first hurdle</p>
<pre class="ansi"><code>$ cargo rust
  Compiling day_1 v0.1.0 (/path/to/day_1/main)
error[E0277]: `Result&lt;String, std::io::Error&gt;` doesn't implement `std::fmt::Display`
 --&gt; src/main.rs:5:20
  |
5 |     println!("{}", input)//
  |               --   ^^^^^ `Result&lt;String, std::io::Error&gt;` cannot be formatted with the default formatter
  |               |
  |               required by this formatting parameter
  |
  = help: the trait `std::fmt::Display` is not implemented for `Result&lt;String, std::io::Error&gt;`
  = note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead

For more information about this error, try `rustc --explain E0277`.
error: could not compile `day_1` (bin "day_1") due to 1 previous error</code></pre>
<p><code>read_to_string</code> doesn’t just return a <code>String</code> that I can manipulate directly (of course!) it returns a <a href="https://doc.rust-lang.org/std/io/type.Result.html">Result&lt;String&gt;</a>, and that creates all sorts of… opportunities for learning.</p>
<ul>
<li>The value I want is <em>inside</em> a <code>Result</code> rather than directly available, so I gotta deal with that</li>
<li>The <code>Result</code> doesn’t have the <code>Display Trait</code> so I need to put one of the formatting bugs inside the “<code>{}</code>”</li>
</ul>
<p>1 of those is easy to deal with:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="cb3-1">#<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> file<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>io<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span></span>
<span id="cb3-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">use</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">std::fs::</span>read_to_string<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// bring the function into scope</span></span>
<span id="cb3-3"></span>
<span id="cb3-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> main () <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb3-5">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> input <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> read_to_string(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"./input.txt"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// read the input</span></span>
<span id="cb3-6">    <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">println!</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"{:?}"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> input)<span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// put the debug syntax into the println! macro</span></span>
<span id="cb3-7"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<p>which gives me</p>
<pre class="ansi"><code>$cargo run
   Compiling day_1 v0.1.0 (path/to/day_1)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.23s
     Running `target/debug/day_1`
Ok("()(((()()))()((()))())()())\n")</code></pre>
<p>So we can see that the output shows us that my input is wrapped in <code>Ok()</code>, which is one of the variants of the <code>Result</code> enum. Which in some ways is fine, but I want the actual feed of characters, so I have a few options.</p>
<ul>
<li><code>.unwrap()</code> which will either give me the string directly or <code>Panic</code></li>
<li><code>.expect()</code> which is basically the same, but I can put an error message in to help me.</li>
<li>Properly <code>match</code> on the <code>Result</code> and either get the content or handle the error.</li>
</ul>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="cb5-1">#<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> file<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>io<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>expect</span>
<span id="cb5-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">use</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">std::fs::</span>read_to_string<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// bring the function into scope</span></span>
<span id="cb5-3"></span>
<span id="cb5-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> main () <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb5-5">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> input <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> read_to_string(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"./input.txt"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// read the input</span></span>
<span id="cb5-6">    <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">println!</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"{}"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> input<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>expect(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"no text here my dude"</span>))<span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// trying expect</span></span>
<span id="cb5-7"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<pre class="ansi"><code>$cargo run
   Compiling day_1 v0.1.0 (path/to/day_1)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.23s
     Running `target/debug/day_1`
()(((()()))()((()))())()())</code></pre>
<p>So that did work, but everything I’ve read says that both <code>unwrap</code> and <code>expect</code> are great tools for rapid iteration without getting bogged down, but that they shouldn’t be used in production code… and <em>yes</em> I’m not writing production code, but if I’m trying to learn more than just the basics, I should probably aim in that direction.</p>
</section>
<section id="match" class="level2">
<h2 class="anchored" data-anchor-id="match">match</h2>
<p>A <code>match</code> expression is a way to check the state of an object and control what happens based on what’s there. It’s very similar to the <code>if/else if/else</code> concept, but it can be a lot more ergonomic depending on what you’re checking, and <code>enum</code>s are a great usecase for it. Python actually has a similar concept also called a <code>match</code> expression but I hadn’t come across that in my own work until recently so I would have relied on <code>if/elif/else</code> in previous work. I’m trying to practice “idiomatic” rust patterns here so lets <code>match</code>:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="cb7-1">#<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> file<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>io<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">match</span></span>
<span id="cb7-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">use</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">std::fs::</span>read_to_string<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// bring the function into scope</span></span>
<span id="cb7-3"></span>
<span id="cb7-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> main () <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb7-5">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> input <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> read_to_string(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"./input.txt"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// read the input</span></span>
<span id="cb7-6">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">match</span> input <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb7-7">        <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Ok</span>(s) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">println!</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"{}"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> s)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// match unpacks the value for us</span></span>
<span id="cb7-8">        <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Err</span>(e) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">println!</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"We got an error {}"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> e)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Printing a custom message and the Error</span></span>
<span id="cb7-9">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb7-10"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<p>OK so that gives us the actual input string as something I can use, so let me move on to actually tracking floors.</p>
</section>
</section>
<section id="one-step-up-n-steps-down" class="level1">
<h1>One step up N steps down</h1>
<p>So what were trying to do is, starting at 0 (the ground floor) find the floor number that Santa ends up on after taking all the steps indicated in the input: “(” = up 1, “)” = down 1. Rust will let us iterate over the characters in a <code>String</code> by calling <code>.chars()</code>. This gives us back a collection we can iterate over.</p>
<p>Coming from python, my instinct is to reach for a <code>loop</code> and <code>if/else</code></p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="cb8-1">#<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> immutable<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">loop</span></span>
<span id="cb8-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// assume the main function and prelude</span></span>
<span id="cb8-3"></span>
<span id="cb8-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> floor <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb8-5"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> c <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> input<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>chars() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb8-6">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> c <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"("</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb8-7">        floor <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> </span>
<span id="cb8-8">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> c <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">")"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb8-9">        floor <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> </span>
<span id="cb8-10">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb8-11">        floor</span>
<span id="cb8-12">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb8-13"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">};</span></span>
<span id="cb8-14"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">println!</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"{}"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> floor)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span></code></pre></div></div>
<p>Which brings me to a (drum roll please) <em>RUST ROOKIE MISTAKE</em> : <code>mutability</code>.</p>
<pre class="ansi"><code>  --&gt; src/main.rs:10:21
   |
 7 |             let floor = 0;
   |                 ----- first assignment to `floor`
...
10 |                     floor += 1
   |                     ^^^^^^^^^^ cannot assign twice to immutable variable
   |
help: consider making this binding mutable
   |
 7 |             let mut floor = 0;
   |                 +++

error[E0384]: cannot assign twice to immutable variable `floor`
  --&gt; src/main.rs:12:21
   |
 7 |             let floor = 0;
   |                 ----- first assignment to `floor`
...
12 |                     floor -= 1
   |                     ^^^^^^^^^^ cannot assign twice to immutable variable
   |
help: consider making this binding mutable
   |
 7 |             let mut floor = 0;
   |                 +++

For more information about this error, try `rustc --explain E0384`.</code></pre>
<p>Rust, cause it knows I’m a midwit, makes things immutable by default, meaning that once I bind a value to a variable I can’t change it… <em>unless</em> (“unless”) I specifically say that that value is <code>mutable</code>. There’s lots of really good reasons for this, but if you’re reading this post and you haven’t already seen those… then… I mean… welcome, I guess.</p>
<p>Anyway, fortunately this one is easy to fix:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="cb10-1">#<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> mutable<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">loop</span></span>
<span id="cb10-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// assume the main function and prelude</span></span>
<span id="cb10-3"></span>
<span id="cb10-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">mut</span> floor <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb10-5"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> c <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> input<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>chars() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb10-6">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> c <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"("</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb10-7">        floor <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> </span>
<span id="cb10-8">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> c <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">")"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb10-9">        floor <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> </span>
<span id="cb10-10">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb10-11">        floor</span>
<span id="cb10-12">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb10-13"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">};</span></span>
<span id="cb10-14"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">println!</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"{}"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> floor)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span></code></pre></div></div>
<p>And… that gave me back the correct answer (for part 1 at least). Boom. I’ve written more than hello world. And yet…</p>
</section>
<section id="closures" class="level1">
<h1>Closures</h1>
<p>While the <code>for</code> loop is legitimate rust, an this solution works, it’s not really, mutability and wrapping aside, that different from how I would solve a short problem like this in python, which ain’t really my goal here. So I guess I better try tackling this with a <a href="https://doc.rust-lang.org/book/ch13-01-closures.html"><code>closure</code></a>.</p>
<p>My main problem with <code>closure</code>s isn’t the concept so much as the number of possible options, and there’s no way to learn these except to use them enough times. I really need to write a cheat sheet for them that I can look up later but for now, I’m going to ask my buddy <code>Claude</code> to recommend one<sup>1</sup>, or more accurately to recommend an <a href="https://doc.rust-lang.org/std/iter/#adapters"><code>iterator adapter</code></a> which will accept a closure and let me do the thing.</p>
<section id="fold" class="level2">
<h2 class="anchored" data-anchor-id="fold">Fold</h2>
<p>So again, let’s think about what the puzzle wants:</p>
<p>Starting at 0, iterate over a collection of <code>char</code>s incrementing or decrementing a number (<em>accumulating</em>), producing a count of the number of up or down steps we’ve taken. This looks like a job for <code>.fold()</code>.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="cb11-1">#<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> closure<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>fold</span>
<span id="cb11-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">use</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">std::fs::</span>read_to_string<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// bring the function into scope</span></span>
<span id="cb11-3"></span>
<span id="cb11-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> main () <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb11-5">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> input <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> read_to_string(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"./input.txt"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// read the input</span></span>
<span id="cb11-6">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">match</span> input <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// match on input</span></span>
<span id="cb11-7">        <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Ok</span>(s) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// when input is Ok()</span></span>
<span id="cb11-8">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> result <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> s<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>chars()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>fold( <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// bind the product of the closure to result</span></span>
<span id="cb11-9">                <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// 0, is the starting number</span></span>
<span id="cb11-10">                <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// |acc, is the variable name for 0 </span></span>
<span id="cb11-11">                <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// c| is the variable name for each char in input for each loop</span></span>
<span id="cb11-12">                <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span>acc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> c<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">match</span> c <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// matching on c</span></span>
<span id="cb11-13">                <span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">'('</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span> acc <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// incrementing acc</span></span>
<span id="cb11-14">                <span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">')'</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span> acc <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// decrementing acc</span></span>
<span id="cb11-15">                _ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span> acc <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// using the wildcard so that the match is exhaustive</span></span>
<span id="cb11-16">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb11-17">        <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">println!</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"{}"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>result)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">//printing the result</span></span>
<span id="cb11-18">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">},</span></span>
<span id="cb11-19">        <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Err</span>(e) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">println!</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"We got an error {}"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> e)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Printing a custom message and the Error</span></span>
<span id="cb11-20">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb11-21"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<pre class="ansi"><code>#| label: closure-fold-output
$cargo run
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.02s
     Running `target/debug/day_1`
-1</code></pre>
<p>So <code>.fold()</code> lets me set a starting value (in the above case it’s 0), then bind that value to <code>acc</code> (standing for <em>accumulator</em>) and then bind the current <code>char</code> to <code>c</code> while looping over <code>input</code>, accumulating a count of steps for each <code>c</code>.</p>
<p>As someone who’s not a programmer, and who writes pretty quick and dirty python, I have to say I really like the closure approach. I can’t really say why but it <em>feels</em> like it reduced some typing for me (for c in chars{ if/else}), but some of that is also just the <code>match</code> implementation as well.</p>
</section>
</section>
<section id="refactoring" class="level1">
<h1>Refactoring</h1>
<p>One of the things that I find challenging is reading deeply nested code, or it’s probably more accurate to say that I find it really hard to debug deeply nested code. My imposter-syndrome self likes to tell me that real coders don’t have a problem with this, but my better angels remind me that: - I don’t want to be a real coder so I should just be happy for those that don’t have this problem, and - There’s probably loads of really talented engineers who also struggle with this.</p>
<p>All this to say that I prefer when the code is split up into smaller blocks <sup>2</sup> that I can reason about individually. While this isn’t exactly deeply nested, it is a few layers deep between the 2 <code>match</code> blocks, and so now might be a good time to think about pulling some of this apart so I can put it back together. Especially given that there is a part two to this <a href="../../../aoc/2015/day_1/puzzle.html">puzzle</a>, so re-usability might be nice to have…</p>
<section id="taking-steps." class="level2">
<h2 class="anchored" data-anchor-id="taking-steps.">Taking steps.</h2>
<p>The first clear thing I can do is pull the ‘char’ matching out into it’s own function:</p>
<p><strong>Take a <code>char</code> and an <code>i32</code> and then give back an updated <code>i32</code></strong></p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="cb13-1">#<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> take<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>step<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>definition</span>
<span id="cb13-2"></span>
<span id="cb13-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">//take step function</span></span>
<span id="cb13-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">//this just does what I was doing in the fold but in a function that I can</span></span>
<span id="cb13-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">//use in the .fold() closure</span></span>
<span id="cb13-6"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> take_step(c<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">char</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> value<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i32</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i32</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb13-7">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">match</span> c <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb13-8">        <span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">'('</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span> value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb13-9">        <span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">')'</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span> value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> </span>
<span id="cb13-10">        _ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span> value</span>
<span id="cb13-11">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb13-12"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<p>Ok, that might not be the most perfect rust function but it does what I want it to do. I should probably make it return a <code>Result</code> and write some proper <code>Error</code> handling, but baby steps here (this is already much too long).</p>
<p>So now my solution is</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="cb14-1">#<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> function<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>fold<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb14-2"></span>
<span id="cb14-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> main () <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb14-4">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> input <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> read_to_string(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"./input.txt"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// read the input</span></span>
<span id="cb14-5">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">match</span> input <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb14-6">        <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Ok</span>(s) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb14-7">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> result <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> s<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>chars()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>fold(</span>
<span id="cb14-8">                <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span>acc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> c<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> take_step(c<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> acc)</span>
<span id="cb14-9">            )<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb14-10">        <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">println!</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"{}"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>result)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb14-11">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">},</span></span>
<span id="cb14-12">        <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Err</span>(e) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">println!</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"We got an error {}"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> e)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Printing a custom message and the Error</span></span>
<span id="cb14-13">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb14-14"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<p>This compiles so the refactor works (boom) but also I find it easier to see what’s going on inside the <code>.fold()</code> (second boom).</p>
<p>This also prompted me to think about handling errors and other ways I could refactor to get rid of other nesting in my <code>main</code> function. Specifically I could re-work the file I/O to handle errors and let me drop that outer <code>match</code> expression but I’m going to call that my <a href="../../../aoc/2015/day_1/day_1_sidequest.html">first sidequest</a> in which our hero write a module to handle reading challenge input that I can use in all the other day’s of AOC. But for now let’s move onto part 2.</p>
</section>
</section>
<section id="part-2-dont-go-down-there-its-dark.." class="level1">
<h1>Part 2: Don’t go down there! It’s dark..</h1>
<p>So for the second part, I have to find the first <code>char</code> that causes Santa to enter the basement (brings the counter below 0).</p>
<p>In python land I would use some kind of <code>while</code> loop here and <code>enumerate</code> over the input string; but I’d like to see if there is a way to do this with a closure?</p>
<p>I know that the <code>.enumerate()</code> method will give me back both the <code>index</code> and the <code>values</code> in a collection so adding that into my <code>adapter</code> chain is a good start, but <code>.fold()</code> isn’t really the move here because we want to stop at a particular point, not take the whole collection.</p>
<p>A little searching tells me that <code>.find()</code> might do what I want, and Claude suggested <code>.scan()</code>. I’m going to try <a href="http://doc.rust-lang.org/std/iter/trait.Iterator.html#method.find"><code>.find()</code></a> first and see what happens.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="cb15-1">#<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> find<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb15-2"></span>
<span id="cb15-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">use</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">std::fs::</span>read_to_string<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// bring the function into scope</span></span>
<span id="cb15-4"></span>
<span id="cb15-5"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> take_step(c<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">char</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> value<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i32</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i32</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb15-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// -- snip --</span></span>
<span id="cb15-7"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb15-8"></span>
<span id="cb15-9"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> main () <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb15-10">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> input <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> read_to_string(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"./input.txt"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// read the input</span></span>
<span id="cb15-11">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">match</span> input <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb15-12">        <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Ok</span>(s) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb15-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// -- snip --</span></span>
<span id="cb15-14">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> floor <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb15-15">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> basement <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> s<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>chars()</span>
<span id="cb15-16">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>enumerate()</span>
<span id="cb15-17">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>find(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span>(i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>c)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span></span>
<span id="cb15-18">                take_step(c<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> floor) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb15-19">                )<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb15-20"></span>
<span id="cb15-21">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">},</span></span>
<span id="cb15-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// -- snip --</span></span>
<span id="cb15-23">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb15-24"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<p>This was my first attempt and it was really just an attempt to see what I get back from <code>.find()</code>. Here’s what happened.</p>
<pre class="ansi"><code>#| label: find-1-output
  --&gt; src/main.rs:22:27
   |
22 |                 take_step(c, floor) == -1
   |                 --------- ^ expected `char`, found `&amp;char`
   |                 |
   |                 arguments to this function are incorrect
   |
note: function defined here
  --&gt; src/main.rs:3:4
   |
 3 | fn take_step(c: char, value: i32) -&gt; i32 {
   |    ^^^^^^^^^ -------
help: consider dereferencing the borrow
   |
22 |                 take_step(*c, floor) == -1
   |                           +</code></pre>
<p>Ok, so what this is telling me is that <code>c</code> is getting passed into the function as a <code>borrow</code> rather than the actual <code>char</code> and so I need to <code>deref</code>. I’m not going to pretend I fully understand why right now, but I do know that I just need to do what the compiler tells me to fix it. And having done so we can see some output.</p>
<pre class="ansi"><code>#| label: find-1-output-2
warning: unused variable: `i`
  --&gt; src/main.rs:21:21
   |
21 |             .find(|(i,c)|
   |                     ^ help: if this is intentional, prefix it with an underscore: `_i`
   |
   = note: `#[warn(unused_variables)]` (part of `#[warn(unused)]`) on by default

warning: `day_1` (bin "day_1") generated 1 warning (run `cargo fix --bin "day_1" -p day_1` to apply 1 suggestion)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.02s
     Running `target/debug/day_1`
-1
Some((1, ')'))</code></pre>
<p>For now I’m going to ignore the <code>unused_variables</code> warning and focus on the fact that <code>.find()</code> is returning an <code>Option&lt;(i,c)&gt;</code> which is good news, if a little confusing to read. I’m not sure that this value is right though, so I’m going to do a little <code>println!</code> debugging to see what’s happening.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="cb18-1">#<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> find<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>println</span>
<span id="cb18-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">use</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">std::fs::</span>read_to_string<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// bring the function into scope</span></span>
<span id="cb18-3"></span>
<span id="cb18-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> take_step(c<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">char</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> value<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i32</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i32</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb18-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// -- snip --</span></span>
<span id="cb18-6"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb18-7"></span>
<span id="cb18-8"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> main () <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb18-9">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> input <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> read_to_string(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"./input.txt"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// read the input</span></span>
<span id="cb18-10">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">match</span> input <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb18-11">        <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Ok</span>(s) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb18-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// -- snip --</span></span>
<span id="cb18-13">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> floor <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb18-14">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> basement <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> s<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>chars()</span>
<span id="cb18-15">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>enumerate()</span>
<span id="cb18-16">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>find(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span>(i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>c)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb18-17">                <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">println!</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"floor = {}"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> floor)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb18-18">                <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">println!</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"index = {}"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> i)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb18-19">                <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">println!</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"char = {}"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> c)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb18-20">                take_step(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>c<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> floor) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb18-21">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb18-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// -- snip --</span></span>
<span id="cb18-23">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb18-24"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<p>Which gives me</p>
<pre class="ansi"><code>#| label: find-1-println-output
// --snip --
floor = 0
index = 0
char = (
floor = 0
index = 1
char = )
Some((1, ')'))</code></pre>
<p>I know, you saw the problem already and have been shouting at me for the last few paragraphs, but… “MidWit” remember?</p>
<p>So <code>floor</code> isn’t getting updated at any point which means while I’m actually getting a technically correct answer (which is the best kind of correct) it’s not in the spirit of the puzzle. Really I’m just finding the first character that subtracts 1 from 0… let’s see if I can fix it.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="cb20-1">#<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> find<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>update</span>
<span id="cb20-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">use</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">std::fs::</span>read_to_string<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// bring the function into scope</span></span>
<span id="cb20-3"></span>
<span id="cb20-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> take_step(c<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">char</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> value<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i32</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i32</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb20-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// -- snip --</span></span>
<span id="cb20-6"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb20-7"></span>
<span id="cb20-8"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> main () <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb20-9">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> input <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> read_to_string(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"./input.txt"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// read the input</span></span>
<span id="cb20-10">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">match</span> input <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb20-11">        <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Ok</span>(s) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb20-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// -- snip --</span></span>
<span id="cb20-13">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> floor <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb20-14">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> basement <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> s<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>chars()</span>
<span id="cb20-15">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>enumerate()</span>
<span id="cb20-16">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>find(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span>(i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>c)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb20-17">                <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">println!</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"floor = {}"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> floor)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb20-18">                <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">println!</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"index = {}"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> i)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb20-19">                <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">println!</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"char = {}"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> c)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb20-20">                floor <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> take_step(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>c<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> floor)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Updating floor here</span></span>
<span id="cb20-21">                floor <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb20-22">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb20-23"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// -- snip --</span></span>
<span id="cb20-24">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb20-25"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<pre class="ansi"><code>#| label: find-1-update-output
// -- snip --

floor = 2001801
index = 22
char = (
floor = 4003603
index = 23
char = )
floor = 8007205
index = 24
char = (
floor = 16014411
index = 25
char = )
floor = 32028821
index = 26
char = )
floor = 64057641
index = 27
char =

None</code></pre>
<p><em>Sad Trombone</em></p>
<p>Ok, so, again you probably saw what I was doing wrong here: I’m not just taking a step each round. I’m adding <code>floor</code> to itself and then the <code>take_step</code> value. So <code>find()</code> doesn’t easily keep track of the value we’re trying to update well. While I could spend ages thinking about how to manage this, it might be time to see if <code>.scan()</code>, which is more similar to <code>.fold()</code>, can do for us.</p>
<section id="scan" class="level2">
<h2 class="anchored" data-anchor-id="scan">Scan</h2>
<p>Based on the docs, <code>.scan()</code> gives back another iterator and so that will need processing as well to get the actual point at which Santa enters the basement for the first time. I’m going to try to build this chain piece by piece starting with the scan.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb22" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="cb22-1">#<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> scan<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>start</span>
<span id="cb22-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// -- snip --</span></span>
<span id="cb22-3"></span>
<span id="cb22-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> main () <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb22-5">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> input <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> read_to_string(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"./input.txt"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// read the input</span></span>
<span id="cb22-6">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">match</span> input <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb22-7">        <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Ok</span>(s) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb22-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// -- snip --</span></span>
<span id="cb22-9">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> basement <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> s<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>chars()</span>
<span id="cb22-10">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>scan(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span>acc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> c<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb22-11">                take_step(c<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>acc)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb22-12">                <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Some</span>(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>acc)</span>
<span id="cb22-13">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb22-14">            </span>
<span id="cb22-15">            )<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb22-16">        <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">println!</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"{:?}"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> basement)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span></code></pre></div></div>
<pre class="ansi"><code>#| label: scan-1-start-output
// -- snip --
Scan { iter: Chars(['(', ')', '(', '(', '(', '(', ')', '(', ')', ')', ')', '(', ')', '(', '(', '(', ')', ')', ')', '(', ')', ')',
 '(', ')', '(', ')', ')', '\n']), state: 0 }</code></pre>
<p>Great, we have a <code>Scan</code> object which is a collection of <code>char</code>s and a <code>state</code>… great… I * definitely know what to do with that… def’nitly.</p>
<p>What do I know that can help me figure this out?</p>
<ul>
<li>A lot of closures are ‘lazy’ meaning that I would need to <code>.collect()</code> or otherwise process it.</li>
<li>I still need to get a particular index, so <code>.enumerate()</code> is still going to be needed.</li>
<li>Speaking of which, <code>.find()</code> is probably going to come into it’s own here.</li>
<li>Claude recommended <code>.scan()</code> so I might not be on the right track with <code>.scan()</code> either, but that’s probably not as helpful to think about right now.</li>
</ul>
<p>Let me start by seeing what happens when I <code>.collect()</code> the <code>.scan()</code>.</p>
<pre class="ansi"><code>#| label: scan-1-collect-output
error[E0283]: type annotations needed
  --&gt; src/main.rs:25:35
   |
25 |         println!("{:?}", basement.collect());
   |                                   ^^^^^^^ cannot infer type of the type parameter `B` declared on the method `collect`
   |
   = note: cannot satisfy `_: FromIterator&lt;i32&gt;`
note: required by a bound in `collect`
  --&gt; /rustc/59807616e1fa2540724bfbac14d7976d7e4a3860/library/core/src/iter/traits/iterator.rs:2051:4
help: consider specifying the generic argument
   |
25 |         println!("{:?}", basement.collect::&lt;Vec&lt;_&gt;&gt;());
   |                                          ++++++++++

For more information about this error, try `rustc --explain E0283`.
</code></pre>
<p><em>Say it with me folks!! Rust. Rookie. Mistake!</em> The compiler needs to know what I’m trying to make so the code needs a <em>turbofish</em> <sup>3</sup> where the type is specified.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb25" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="cb25-1">#<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> scan<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>turbofish</span>
<span id="cb25-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// -- snip --</span></span>
<span id="cb25-3">    </span>
<span id="cb25-4">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> basement <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> s<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>chars()</span>
<span id="cb25-5">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>scan(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span>acc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> c<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb25-6">                take_step(c<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>acc)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb25-7">                <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Some</span>(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>acc)</span>
<span id="cb25-8">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb25-9">            </span>
<span id="cb25-10">            )<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb25-11">        <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">println!</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"{:?}"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> basement<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">collect::</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Vec</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>_<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;</span>())<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb25-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// -- snip --</span></span></code></pre></div></div>
<p>That gives me back a vector of 0s… because of course it does. I’m not sure what that means yet either, but I’m gonna keep going. We know that we’re looking for an index as I said, so my next thought is to add <code>.enumerate()</code> to the chain, as this will give me a <code>Vec&lt;(index, state)&gt;</code> (I think).</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb26" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="cb26-1">#<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> scan<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>enumerate</span>
<span id="cb26-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// -- snip --</span></span>
<span id="cb26-3">    </span>
<span id="cb26-4">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> basement <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> s<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>chars()</span>
<span id="cb26-5">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>scan(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span>acc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> c<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb26-6">                take_step(c<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>acc)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb26-7">                <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Some</span>(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>acc)</span>
<span id="cb26-8">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb26-9">            </span>
<span id="cb26-10">            )</span>
<span id="cb26-11">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>enumerate()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb26-12">        <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">println!</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"{:?}"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> basement<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">collect::</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Vec</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>_<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;</span>())<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb26-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// -- snip --</span></span></code></pre></div></div>
<pre class="ansi"><code>#| label: scan-1-enumerate-output
// -- snip --
[(0, 0), (1, 0), (2, 0), (3, 0), (4, 0), (5, 0), (6, 0), (7, 0), (8, 0), (9, 0), (10, 0), (11, 0), (12, 0), (13, 0), (14, 0), (15
, 0), (16, 0), (17, 0), (18, 0), (19, 0), (20, 0), (21, 0), (22, 0), (23, 0), (24, 0), (25, 0), (26, 0), (27, 0)]
// -- snip --</code></pre>
<p>Yes! Well… half yes. I’m getting the tuple I expect (I think), but the fact that the second item is always 0 leads me to believe that something is going wrong in the way <code>.scan()</code> is interacting with my <code>take_step</code> function. Rather than move onto adding the <code>.find()</code> part of the chain I’m going to unpack the <code>take_step</code> functionality and do it explicitly inside the <code>closure</code></p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb28" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="cb28-1">#<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> scan<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>explicit</span>
<span id="cb28-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// -- snip --</span></span>
<span id="cb28-3">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> basement <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> s<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>chars()</span>
<span id="cb28-4">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>scan(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span>acc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> c<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb28-5">                acc <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">match</span> c <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb28-6">                    <span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">'('</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span> acc <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb28-7">                    <span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">')'</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span> acc <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> </span>
<span id="cb28-8">                    _ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span> acc</span>
<span id="cb28-9">                <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">};</span></span>
<span id="cb28-10">                <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Some</span>(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>acc)</span>
<span id="cb28-11">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span>)</span>
<span id="cb28-12">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>enumerate()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb28-13">        <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">println!</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"{:?}"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> basement<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">collect::</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Vec</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>_<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;</span>())<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb28-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// -- snip --</span></span></code></pre></div></div>
<p>Oh boy, this next one is a doozy</p>
<pre class="ansi"><code>#| label: scan-1-explicit-output
// -- snip --
  --&gt; src/main.rs:21:32
   |
21 |                     '(' =&gt; acc + 1,
   |                            --- ^ - {integer}
   |                            |
   |                            &amp;mut {integer}
   |
   = note: an implementation for `&amp;{integer} + {integer}` exists
help: consider reborrowing this side
   |
21 |                     '(' =&gt; &amp;*acc + 1,
   |                            ++

error[E0369]: cannot subtract `{integer}` from `&amp;mut {integer}`
  --&gt; src/main.rs:22:32
   |
22 |                     ')' =&gt; acc -1,
   |                            --- ^- {integer}
   |                            |
   |                            &amp;mut {integer}
   |
   = note: an implementation for `&amp;{integer} - {integer}` exists
help: consider reborrowing this side
   |
22 |                     ')' =&gt; &amp;*acc -1,
   |                            ++
// -- snip --</code></pre>
<p>OK, so, again, I’m not sure if this is accurately diagnosing what the issue was earlier, but it is showing me that <code>acc</code> is mutably borrowed inside the <code>closure</code>, so if I’m understanding correctly then it means I need to <code>deref</code> <code>acc</code> before the <code>match</code>, also if I leave <code>acc</code> in the match arms I’ll end up with the same problem I had with the earlier find: doubling.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb30" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="cb30-1">#<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> scan<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>deref</span>
<span id="cb30-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// -- snip --</span></span>
<span id="cb30-3">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> basement <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> s<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>chars()</span>
<span id="cb30-4">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>scan(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span>acc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> c<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb30-5">                <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>acc <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">match</span> c <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb30-6">                    <span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">'('</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb30-7">                    <span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">')'</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> </span>
<span id="cb30-8">                    _ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb30-9">                <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">};</span></span>
<span id="cb30-10">                <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Some</span>(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>acc)</span>
<span id="cb30-11">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span>)</span>
<span id="cb30-12">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>enumerate()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb30-13">        <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">println!</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"{:?}"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> basement<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">collect::</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Vec</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>_<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;</span>())<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb30-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// -- snip --</span></span></code></pre></div></div>
<p>We have a third boom.</p>
<pre class="ansi"><code>#| label: scan-1-deref-output
// -- snip --
[(0, 1), (1, 0), (2, 1), (3, 2), (4, 3), (5, 4), (6, 3), (7, 4), (8, 3), (9, 2), (10, 1), (11, 2), (12, 1), (13, 2), (14, 3), (15, 4), (16, 3), (17, 2), (18, 1), (19, 2), (20, 1), (21, 0), (22, 1), (23, 0), (24, 1), (25, 0), (26, -1), (27, -1)]
// -- snip --</code></pre>
<p>So the state is now updating as we run through it, meaning that now I just need to <code>.find()</code> the first time we get a -1 in the state, which we can see is at index 26 in this output.</p>
<p>What we have now is the vec of <code>(i, state)</code> that we need to pass into <code>.find()</code>, and because <code>.find()</code> takes only one parameter I need to pass them in in that format rather than as separate arguments.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb32" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="cb32-1">#<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> scan<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>find</span>
<span id="cb32-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// -- snip --</span></span>
<span id="cb32-3">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> basement <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> s<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>chars()</span>
<span id="cb32-4">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>scan(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span>acc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> c<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb32-5">                <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>acc <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">match</span> c <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb32-6">                    <span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">'('</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb32-7">                    <span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">')'</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> </span>
<span id="cb32-8">                    _ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb32-9">                <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">};</span></span>
<span id="cb32-10">                <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Some</span>(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>acc)</span>
<span id="cb32-11">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span>)</span>
<span id="cb32-12">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>enumerate()</span>
<span id="cb32-13">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>find(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span>(i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> state)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>state <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb32-14">        <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">println!</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"{:?}"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> basement<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">collect::</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Vec</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>_<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;</span>())<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb32-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// -- snip --</span></span></code></pre></div></div>
<pre class="ansi"><code>#| label: scan-1-find-output
// -- snip --
  --&gt; src/main.rs:29:35
   |
29 |         println!("{:?}", basement.collect::&lt;Vec&lt;_&gt;&gt;());
   |                                   ^^^^^^^ method cannot be called on `Option&lt;(usize, {integer})&gt;` due to unsatisfied trait b
ounds
   |
   = note: the following trait bounds were not satisfied:
           `Option&lt;(usize, {integer})&gt;: Iterator`
           which is required by `&amp;mut Option&lt;(usize, {integer})&gt;: Iterator`

For more information about this error, try `rustc --explain E0599`.
// -- snip --</code></pre>
<p>Ok, so <code>.find()</code> returns an <code>Option</code> rather than a collection that needs to be <code>.collect()</code>ed so I need to drop that bit, so long turbofish!</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb34" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="cb34-1">#<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> scan<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>find<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>println</span>
<span id="cb34-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// -- snip --</span></span>
<span id="cb34-3">    <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">println!</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"{}"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> basement)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb34-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// -- snip --</span></span></code></pre></div></div>
<p>Ladies and Jellyspoons, we have reached the 4th boom.</p>
<pre class="ansi"><code>#| label: scan-find-println-output
// -- snip --
warning: unused variable: `i`
  --&gt; src/main.rs:28:21
   |
28 |             .find(|(i, state)| *state == -1);
   |                     ^ help: if this is intentional, prefix it with an underscore: `_i`
   |
   = note: `#[warn(unused_variables)]` (part of `#[warn(unused)]`) on by default

warning: `day_1` (bin "day_1") generated 1 warning (run `cargo fix --bin "day_1" -p day_1` to apply 1 suggestion)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.25s
     Running `target/debug/day_1`
-1
Some((26, -1))
// -- snip --</code></pre>
<p>OK, so we have the <code>Some(tuple)</code> that shows us the index and the state we want. So functionally we have the output we want. The eagle eyed amongst you will have spotted that the last <code>char</code> in <code>input</code> is <code>'\n'</code> and so the wildcard in the <code>match</code> is doing what we want as well and leaving <code>acc</code> as it is.</p>
<p>Thus we have out answer to both parts!… except…</p>
<p>As I said up above I’m trying to write towards production code, and while I have my answer, the output is ugly as fu… it doesn’t look great. So, it might be worth it to try to make the output prettier.</p>
</section>
<section id="prettifying" class="level2">
<h2 class="anchored" data-anchor-id="prettifying">Prettifying</h2>
<p>This is one of the places <code>.unwrap()</code> may work well because there is a <code>Some</code> that we just want to get the value out of.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb36" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="cb36-1">#<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> scan<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>unwrap</span>
<span id="cb36-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// -- snip --</span></span>
<span id="cb36-3">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> basement <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> s<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>chars()</span>
<span id="cb36-4">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>scan(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span>acc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> c<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb36-5">                <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>acc <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">match</span> c <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb36-6">                    <span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">'('</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb36-7">                    <span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">')'</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> </span>
<span id="cb36-8">                    _ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb36-9">                <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">};</span></span>
<span id="cb36-10">                <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Some</span>(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>acc)</span>
<span id="cb36-11">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span>)</span>
<span id="cb36-12">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>enumerate()</span>
<span id="cb36-13">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>find(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span>(i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> state)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>state <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb36-14">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>unwrap()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb36-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// -- snip --</span></span></code></pre></div></div>
<p>Which gives us back the tuple <code>(26,-1)</code>. Since we just want the zeroth item in the tuple we could just destructure it with <code>.</code> syntax as well to just get the <code>26</code> back in out output. However, I want to deal with that <code>unused_variables</code> warning now and I think that <a href="https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.map"><code>.map()</code></a> might let me do that. <code>.map()</code> will let me create a new iterator and apply a new closure to it <em>in line</em>, letting me destructure the tuple I get back from <code>.find()</code>.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb37" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="cb37-1">#<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> pretty<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>map</span>
<span id="cb37-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// -- snip --</span></span>
<span id="cb37-3">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>enumerate()</span>
<span id="cb37-4">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>find(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span>(i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> state)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>state <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb37-5">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>map(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span>(i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> state)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> i <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">as</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i32</span>)</span>
<span id="cb37-6">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>unwrap()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb37-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// -- snip --</span></span></code></pre></div></div>
<pre class="ansi"><code>#| label: pretty-map-output
// -- snip --
warning: unused variable: `state`
  --&gt; src/main.rs:29:23
   |
29 |             .map(|(i, state)| i as i32)
   |                       ^^^^^ help: if this is intentional, prefix it with an underscore: `_state`
   |
   = note: `#[warn(unused_variables)]` (part of `#[warn(unused)]`) on by default

warning: unused variable: `i`
  --&gt; src/main.rs:28:21
   |
28 |             .find(|(i, state)| *state == -1)
   |                     ^ help: if this is intentional, prefix it with an underscore: `_i`

warning: `day_1` (bin "day_1") generated 2 warnings (run `cargo fix --bin "day_1" -p day_1` to apply 2 suggestions)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.22s
     Running \`target/debug/day_1\`
-1
26
// -- snip --</code></pre>
<p>OK, so the compiler is telling me that if I want to get rid of the <code>unused_variables</code> warning I just need to prefix the variable names with “_” and so let me do that one last time and check the output.</p>
</section>
</section>
<section id="final-implementation" class="level1">
<h1>Final implementation</h1>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb39" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="cb39-1">#<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">final</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>implementation</span>
<span id="cb39-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">use</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">std::fs::</span>read_to_string<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// bring the function into scope</span></span>
<span id="cb39-3"></span>
<span id="cb39-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> take_step(c<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">char</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> value<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i32</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i32</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb39-5">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">match</span> c <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb39-6">        <span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">'('</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span> value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb39-7">        <span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">')'</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span> value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> </span>
<span id="cb39-8">        _ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span> value</span>
<span id="cb39-9">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb39-10"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb39-11"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> main () <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb39-12">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> input <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> read_to_string(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"./input.txt"</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// read the input</span></span>
<span id="cb39-13">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">match</span> input <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb39-14">        <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Ok</span>(s) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb39-15">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> result <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> s<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>chars()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>fold(</span>
<span id="cb39-16">                <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span>acc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> c<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> take_step(c<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> acc)</span>
<span id="cb39-17">            )<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb39-18">        <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">println!</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"We're on floot {}"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>result)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb39-19">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> basement <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> s<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>chars()</span>
<span id="cb39-20">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>scan(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span>acc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> c<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb39-21">                <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>acc <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">match</span> c <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb39-22">                    <span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">'('</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb39-23">                    <span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">')'</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> </span>
<span id="cb39-24">                    _ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb39-25">                <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">};</span></span>
<span id="cb39-26">                <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Some</span>(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>acc)</span>
<span id="cb39-27">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span>)</span>
<span id="cb39-28">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>enumerate()</span>
<span id="cb39-29">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>find(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span>(_i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> state)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>state <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb39-30">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>map(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span>(i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> _state)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> i <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">as</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i32</span>)</span>
<span id="cb39-31">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>unwrap()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb39-32">        <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">println!</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"The first time we hit the basement is {}"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> basement)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb39-33"></span>
<span id="cb39-34">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">},</span></span>
<span id="cb39-35">        <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Err</span>(e) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=&gt;</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">println!</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"We got an error {}"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> e)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Printing a custom message and the Error</span></span>
<span id="cb39-36">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb39-37"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<pre class="ansi"><code>#| label: final-output
$cargo run
   Compiling day_1 v0.1.0 (path/to/day_1)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.25s
     Running `target/debug/day_1`
We're on floor -1
The first time we hit the basement is 26</code></pre>
<p><strong>Ultimate BOOM!</strong>.</p>
<p>We have a solution.</p>
<p>It’s definitely not perfect and it spawned at least one <a href="../../../aoc/2015/day_1/day_1_sidequest.html">sidequest</a> but as a fledgling rust writer I think I can be pretty happy with it.</p>
</section>
<section id="what-do-i-need-to-take-from-this" class="level1">
<h1>What do I need to take from this?</h1>
<ul>
<li>Closures are great, but there’s a lot of adaptors and, frankly the documentation isn’t that easy to parse.
<ul>
<li><code>.find()</code> lets us look for a condition</li>
<li><code>.fold()</code> lets us accumulate a value over a collection (at least as far as I understand it)</li>
<li><code>.scan()</code> is like <code>.fold()</code>, except it gives back a collection and a state item that we can track</li>
</ul></li>
<li>I need to learn more about referencing and dereferencing, there is a principle there around how something gets passed into a closure, but it hasn’t clicked for me yet.</li>
<li>The compiler is a good ally, especially if the issue is rudimentary.</li>
<li>Walking through the problem is a good idea, even if it’s after the fact.
<ul>
<li>I wrote this up after I had a solved, but I was taking checkpoints as I went so that I could try to consolidate things.</li>
<li>I definitely used Claude to help me parse the errors, and as I said above to suggest adaptors that might be useful to me. It would often tell me to just reach for a <code>for</code> loop though and I’d have to do a lot to get it to stick with my learning goals. Working with an LLM definitely saved time, but when my aim is to learn, not to meet KPIs as a developer this isn’t ideal. I have a ‘Rust-coach’ skill which often works well, but I’ve found that it’s tough to keep it from being too helpful and it’s better for me to just not use one when I’m tired: too tempting to just get an answer, which isn’t the same as learning a thing.</li>
</ul></li>
<li>This write up took way too long. In future it would be better to hit highlights better, but then I don’t really know what qualifies as a highlight this early in the show.</li>
<li>Refactoring is good <em>even if</em> it doesn’t actually get reused.
<ul>
<li>I certainly could have updated <code>take_step()</code> to accomodated Part 2, but as a learning excercise it was worth doing even though it just contributed to Part 1.</li>
</ul></li>
</ul>
<p>Anyway, that’s too much for a single post, espeshally a first’un.</p>
<p>God’s speed.</p>
<p>The MidWit<em>:wqa</em></p>


</section>


<div id="quarto-appendix" class="default"><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p>I really don’t like using LLMs for learning, for too many reasons to go into here, but honestly, as a docs reader I do think they are great, and I’m not above saving myself days of reading through docs or reddit to get an answer. If you think I got Claude to give me the whole solution, then you probably think I got Claude to write this whole post, so there’s not really a lot I can do about that. We’re all just tryin’ to figure this out I suppose.↩︎</p></li>
<li id="fn2"><p>I’m not qualified to say that this is ‘right’ or ‘wrong’ I’m saying that my brain and eyes prefer it. Maybe it is better to wire everything together in longer chunks, but I’m &gt; 40 years old and I’m trying to learn what I’m trying to learn, so… get off my back!↩︎</p></li>
<li id="fn3"><p>If you think for one minute that the fact that it’s called a <em>turbofish</em> isn’t a big reason why I’m trying to learn rust, you’re out of your mind.↩︎</p></li>
</ol>
</section></div> ]]></description>
  <category>student-log</category>
  <category>rust</category>
  <category>advent-of-code</category>
  <category>learning</category>
  <guid>https://midwitsanonymous.com/aoc/2015/day_1/day_1.html</guid>
  <pubDate>Fri, 29 May 2026 00:00:00 GMT</pubDate>
</item>
</channel>
</rss>
