<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
    <title>Sakib&#x27;s blog</title>
    <subtitle>A digital diary of programming, literature, films, and whatever else.</subtitle>
    <link rel="self" type="application/atom+xml" href="https://selectiveduplicate.github.io/blog/atom.xml"/>
    <link rel="alternate" type="text/html" href="https://selectiveduplicate.github.io/blog"/>
    <generator uri="https://www.getzola.org/">Zola</generator>
    <updated>2026-05-25T00:00:00+00:00</updated>
    <id>https://selectiveduplicate.github.io/blog/atom.xml</id>
    <entry xml:lang="en">
        <title>Solving tool bloat in Agentic AI at the gateway layer</title>
        <published>2026-05-25T00:00:00+00:00</published>
        <updated>2026-05-25T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Abu Sakib
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://selectiveduplicate.github.io/blog/solving-tool-bloat-in-agentic-ai-at-the-gateway-layer/"/>
        <id>https://selectiveduplicate.github.io/blog/solving-tool-bloat-in-agentic-ai-at-the-gateway-layer/</id>
        
        <content type="html" xml:base="https://selectiveduplicate.github.io/blog/solving-tool-bloat-in-agentic-ai-at-the-gateway-layer/">&lt;p&gt;Large tool catalogs, although valuable, mean every request is more expensive, slower, and more likely to produce the wrong result, since each request contains all the tools there are. Every tool definition consumes tokens, irrespective of the query needing that tool or not.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;on-the-tool-bloat-problem&quot;&gt;On the tool bloat problem&lt;&#x2F;h2&gt;
&lt;p&gt;Tool bloat affects an AI application on three fronts: cost, response quality, and latency.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;cost&quot;&gt;Cost&lt;&#x2F;h3&gt;
&lt;p&gt;Each tool definition in an API request consumes input tokens. A name, a description, and a parameter schema together typically run between 50 and 150 tokens, depending on how detailed the schema is. So an application with, say, 60 tools, may therefore append anywhere from 3,000 to 9,000 tokens to every request, before the user&#x27;s actual message appears in the payload.&lt;&#x2F;p&gt;
&lt;p&gt;The expense recurs on every call. These tokens lack any informational value relative to the user&#x27;s intent, which you pay for regardless. At higher throughput, the overhead figure scales linearly. Input token cost is a function of payload size, and tool definitions &lt;em&gt;are&lt;&#x2F;em&gt; payload.&lt;&#x2F;p&gt;
&lt;p&gt;The problem compounds in heterogeneous tools. An application might integrate calendar management, email, database querying, code execution, and document retrieval into a single API surface. To say that the application has many tools would be a misperception; rather, it has tools that are almost never all relevant to any single query. For example, if you want to schedule a meeting, you neither need the code execution tools in that request nor the database query tools. Yet all of them appear in the request, and thereby billed.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;quality&quot;&gt;Quality&lt;&#x2F;h3&gt;
&lt;p&gt;Cost aside, sending irrelevant tools to an LLM actively degrades its ability to designate the right one. A May 2025 paper, &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;arxiv.org&#x2F;abs&#x2F;2505.03275&quot;&gt;RAG-MCP: Mitigating Prompt Bloat in LLM Tool Selection via Retrieval-Augmented Generation&lt;&#x2F;a&gt;, measured tool-selection accuracy across varying tool pool sizes. It found that naive tool delivery, such as passing all available tools, produced a baseline accuracy of 13.62%. A retrieval-augmented approach that pre-filtered tools by semantic relevance raised that figure to 43.13%, more than tripling accuracy, while cutting prompt token usage by over 50%.&lt;&#x2F;p&gt;
&lt;p&gt;The mechanism behind this degradation is attention dilution. Transformer-based models allocate attention across all tokens in the context window. For a tool list long and mostly irrelevant, the model&#x27;s attention distributes across entries that bear no relationship to the query. This increases the probability of selecting a tool that is plausible but wrong, one whose description overlaps superficially with the user&#x27;s intent. It also exacerbates the risk of hallucinated parameter construction for tools the model has misidentified as applicable. Neither failure is easily caught without downstream validation. And both undermine the reliability of the system in ways that are, without careful instrumentation, difficult to attribute.&lt;&#x2F;p&gt;
&lt;p&gt;The issue becomes more concerning with tool descriptions that share vocabulary. For example, two tools may both respond to queries involving search, and an overloaded context makes the distinction harder for the model to maintain with accuracy.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;latency&quot;&gt;Latency&lt;&#x2F;h3&gt;
&lt;p&gt;Larger inputs take longer to process. Take note of the fact that the model must attend to every token before generating output. So time-to-first-token—the interval between dispatching a request and receiving the start of a response—scales with input size. For interactive applications where a user waits for a response, this delay is user-visible. Excess tokens in the tool list contribute directly to it.&lt;&#x2F;p&gt;
&lt;p&gt;The latency effect is less acute than the cost or quality effects at moderate tool counts. It becomes significant, however, in high-volume, latency-sensitive environments, like real-time copilots, or systems where multiple LLM calls constitute a single user interaction. In those contexts, every nonessential token in the input is a compounding liability.&lt;&#x2F;p&gt;
&lt;p&gt;We can observe a common origin in these problems: a static tools array, while user intent remains specific. No query in a multi-domain agentic application requires access to every tool the application exposes. How to determine, per request and at runtime, the relevant tools, is the problem I will try to address in this post.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;the-naive-solutions-and-why-they-don-t-scale&quot;&gt;The naive solutions and why they don&#x27;t scale&lt;&#x2F;h2&gt;
&lt;p&gt;So how to tackle these complications? The usual approaches, although natural and addressing the issues in some measure, don&#x27;t resolve the elementary problem, nor do they scale.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;curating-tools-per-endpoint&quot;&gt;Curating tools per-endpoint&lt;&#x2F;h3&gt;
&lt;p&gt;Instinctively, you may want to curate the tools array manually: define which tools each endpoint receives and exclude everything else. It&#x27;s a sound strategy for narrow, well-defined endpoints with stable, predictable intent.&lt;&#x2F;p&gt;
&lt;p&gt;But what about an endpoint that serves varied intent, the architecture that agentic applications veer toward? In the cases of, for example, a general-purpose assistant or conversational interface, the tools any given query might require alters with the query itself. Manual curation therefore, in that context, is not as much a configuration decision you make once as it is a system upkeep. For every new tool now requires a review of every endpoint&#x27;s curated list. Each modification to application behavior carries with it risks of discrepancies between what the endpoint receives and the query&#x27;s actual requirements.&lt;&#x2F;p&gt;
&lt;p&gt;We also observe a subtler failure where, by severely constraining an endpoint&#x27;s tools, you unobtrusively debase capability. A tool mistakenly selected might result in a visible error, whereas a missing tool produces a response that simply does not invoke it, the latter being considerably harder to detect and diagnose.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;application-level-filtering&quot;&gt;Application-level filtering&lt;&#x2F;h3&gt;
&lt;p&gt;In a more dynamic approach, the application discharges the filtering at runtime. Before assembling the request, the application inspects the query, ascertains relevance, and duly constructs the tools array.&lt;&#x2F;p&gt;
&lt;p&gt;However, every team building on the platform must implement this logic individually, with no shared standard for determining relevance. As a result, you have a patchwork of implementations, each maintained separately and susceptible to drift.&lt;&#x2F;p&gt;
&lt;p&gt;Application-level filtering also conjoins tool selection strategy with business logic. At one point, and recurringly, the filtering approach needs to change, because, for example, the tool catalog has grown or relevance thresholds require calibration. Now you must coordinate that change across every service that has implemented its own version. The affiliated cost will be prohibitive if your platform serves many API consumers.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;sending-everything&quot;&gt;Sending everything&lt;&#x2F;h3&gt;
&lt;p&gt;The default, and by far the most common strategy, is to dispatch the full tools array on every request. It suffices for small tool sets, requiring no additional engineering,  maintenance, or coordination.&lt;&#x2F;p&gt;
&lt;p&gt;Its inadequacy at scale, however, follows from the previous section. Moreover, there is an additional liability in that applications that default to sending everything implicitly treat tool selection as the model&#x27;s problem. Reliability remains stable for a few tools, but degrades as the number of tools increases. The application, to make matters worse, lacks a method to detect this. Erroneous tool invocations and degraded response quality appear as downstream symptoms, often misattributed to other determinants.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;tool-selection-as-retrieval&quot;&gt;Tool selection as retrieval&lt;&#x2F;h2&gt;
&lt;p&gt;Relevance is a relationship between a tool and a query, one that only materializes at runtime. So if relevance is a relationship between two pieces of text, we can express it as a geometric relationship between vectors. A text embedding model converts a string into a high-dimensional vector, where position in that space encodes meaning. Semantically related strings produce vectors that point in similar directions, and the cosine of the angle between them (cosine similarity) yields a value between -1 and 1 that quantifies that alignment.&lt;&#x2F;p&gt;
&lt;p&gt;So to apply this to tool selection, a practical set of steps would be as follows:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Embed the user&#x27;s query.&lt;&#x2F;li&gt;
&lt;li&gt;Embed each tool as a concatenation of its name and description.&lt;&#x2F;li&gt;
&lt;li&gt;Compute cosine similarity between the query vector and each tool vector, and pass only the highest-scoring tools to the model.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;Thus, not unlike RAG systems, semantic tool filtering retrieves only the tools the query actually needs, provided accurate, descriptive tool definitions.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;why-the-gateway-is-the-right-place-to-solve-this&quot;&gt;Why the gateway is the right place to solve this&lt;&#x2F;h2&gt;
&lt;p&gt;So where does this &lt;em&gt;semantic filtering&lt;&#x2F;em&gt; actually execute? It might do so in the application, as discussed, but that reintroduces the foregoing coupling and fragmentation problems.&lt;&#x2F;p&gt;
&lt;p&gt;The correct answer is the API gateway.&lt;&#x2F;p&gt;
&lt;p&gt;The gateway, located between the application and the LLM, can intercept the tools array, perform semantic filtering, rewrite the payload, and forward the optimal request to the model.&lt;&#x2F;p&gt;
&lt;p&gt;This placement confers three distinct advantages. First, you define the filtering logic once; it applies uniformly across every API on the platform, precluding individual reimplementations across teams, services, and endpoints. Second, it is transparent to the application. Third benefit is unified governance; platform engineers can enforce it as infrastructure policy, as opposed to reliance on individual teams for consistent implementation.&lt;&#x2F;p&gt;
&lt;p&gt;The WSO2 AI Gateway in the WSO2 API Platform implements this motif through the &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;wso2.com&#x2F;api-platform&#x2F;policy-hub&#x2F;policies&#x2F;semantic-tool-filtering&quot;&gt;Semantic Tool Filtering policy, available through the WSO2 Policy Hub&lt;&#x2F;a&gt;. The policy intercepts any OpenAI-compatible request, applies vector-similarity filtering to the tools array, and rewrites the payload before it gets to the model, all of it in an orderly fashion within the gateway&#x27;s request pipeline. You do not have to alter anything upstream or downstream.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;how-the-semantic-tool-filtering-policy-works&quot;&gt;How the Semantic Tool Filtering policy works&lt;&#x2F;h2&gt;
&lt;h3 id=&quot;step-1-extract-the-query&quot;&gt;Step 1: Extract the query&lt;&#x2F;h3&gt;
&lt;p&gt;The policy reads the user&#x27;s latest message from the request body using a configurable &lt;code&gt;JSONPath&lt;&#x2F;code&gt; expression. The default, &lt;code&gt;$.messages[-1].content&lt;&#x2F;code&gt;, targets the last message in an OpenAI-style payload; but you can configure that to accommodate any request schema. Alternatively, if not structured JSON but embedded in a text, the policy extracts the query from a &lt;code&gt;&amp;lt;userq&amp;gt;&lt;&#x2F;code&gt; tag.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;step-2-extract-tool-definitions&quot;&gt;Step 2: Extract tool definitions&lt;&#x2F;h3&gt;
&lt;p&gt;A second &lt;code&gt;JSONPath&lt;&#x2F;code&gt; expression extracts the tools array. The policy accommodates three common schemas:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;$.tools&lt;&#x2F;code&gt; for a flat array&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;$.tools[*].function&lt;&#x2F;code&gt; for OpenAI and Mistral-style function wrappers where each tool is an object with a nested &lt;code&gt;function&lt;&#x2F;code&gt; field&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;$.tools[0].function_declarations&lt;&#x2F;code&gt; for Gemini-style payloads.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;For text-based payloads, the policy reads &lt;code&gt;&amp;lt;toolname&amp;gt;&lt;&#x2F;code&gt; and &lt;code&gt;&amp;lt;tooldescription&amp;gt;&lt;&#x2F;code&gt; tags from the request body and strips them from the forwarded payload afterward.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;step-3-generate-embeddings&quot;&gt;Step 3: Generate embeddings&lt;&#x2F;h3&gt;
&lt;p&gt;The policy embeds the user query and each tool, represented as &lt;code&gt;&quot;&amp;lt;name&amp;gt;: &amp;lt;description&amp;gt;&quot;&lt;&#x2F;code&gt;, using a configured embedding provider, namely, OpenAI, Mistral, and Azure OpenAI. You can configure each of these in the gateway&#x27;s &lt;code&gt;config.toml&lt;&#x2F;code&gt; with an endpoint, model name, and API key.&lt;&#x2F;p&gt;
&lt;p&gt;Tool embeddings are cached using an LRU cache keyed by a SHA-256 hash of the provider, model, and tool description. That means tool embeddings are computed once and reused across requests. Only the user query embedding is computed fresh on each call.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;step-4-score-and-filter&quot;&gt;Step 4: Score and filter&lt;&#x2F;h3&gt;
&lt;p&gt;Cosine similarity scores the query vector against each tool vector. The policy then applies one of two selection modes:&lt;&#x2F;p&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Mode&lt;&#x2F;th&gt;&lt;th&gt;Behavior&lt;&#x2F;th&gt;&lt;th&gt;Usage&lt;&#x2F;th&gt;&lt;&#x2F;tr&gt;&lt;&#x2F;thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;&lt;code&gt;By rank&lt;&#x2F;code&gt;&lt;&#x2F;strong&gt;&lt;&#x2F;td&gt;&lt;td&gt;Retains the top-K tools by score; the number of most relevant tools to include is configurable, the default being &lt;code&gt;5&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td&gt;When a predictable, bounded tool count matters: cost control, latency targets&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;&lt;code&gt;By threshold&lt;&#x2F;code&gt;&lt;&#x2F;strong&gt;&lt;&#x2F;td&gt;&lt;td&gt;Retains all tools scoring at or above a similarity threshold between &lt;code&gt;0.0&lt;&#x2F;code&gt; and &lt;code&gt;1.0&lt;&#x2F;code&gt;, the default being &lt;code&gt;0.7&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td&gt;When tool count should adapt to query specificity; a precise query might return one tool but an ambiguous one several&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;&#x2F;tbody&gt;&lt;&#x2F;table&gt;
&lt;h3 id=&quot;step-5-rewrite-and-forward&quot;&gt;Step 5: Rewrite and forward&lt;&#x2F;h3&gt;
&lt;p&gt;The original tools array is replaced in-place with the filtered subset. The amended request proceeds to the LLM. From the model&#x27;s perspective, these were the only tools it was ever given.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;the-embedding-cache&quot;&gt;The Embedding cache&lt;&#x2F;h3&gt;
&lt;p&gt;The process described above calls an external embedding service on every request, or it would, without the built-in LRU cache. The cache makes the policy viable at production throughput, so let&#x27;s briefly discuss that.&lt;&#x2F;p&gt;
&lt;p&gt;We can make a simple observation: tool names and descriptions remain static between requests, unlike user queries. So tool embeddings are computed once and stored; subsequent requests for the same tool retrieve the cached vector. Only the query embedding—the variable part—is computed anew on each request. At steady state, the cost of an embedding call per request reduces to only the cost of embedding a single short query string.&lt;&#x2F;p&gt;
&lt;p&gt;Cache keys are computed as a SHA-256 hash of three components: the embedding provider, the model name, and the tool description. The policy binds the provider and model to the key to take into account vectors produced by different models; they occupy different semantic spaces and hence are incompatible. In case of a different embedding model, existing cache entries will not match the new key format and will not be served, precluding stale or incompatible vectors from contaminating the filtered results.&lt;&#x2F;p&gt;
&lt;p&gt;Eviction follows an LRU-like strategy across APIs and tools within each API. Upon reaching capacity, the least recently used entries are evicted first in the cache. And when caching a new batch of tools, the policy calculates available slots before writing and skips tools that would not fit. It thus avoids evictions of recently used entries to accommodate a large batch that may itself see little reuse.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;format-flexibility&quot;&gt;Format flexibility&lt;&#x2F;h3&gt;
&lt;p&gt;The majority of LLM API interactions involve JSON payloads, but not all. So the policy offers another option for frameworks that embed tool definitions differently.&lt;&#x2F;p&gt;
&lt;p&gt;Some frameworks inject tool definitions directly into the text of a system or user message. The policy accommodates this through a text-tag format. Tool definitions are marked with &lt;code&gt;&amp;lt;toolname&amp;gt;&lt;&#x2F;code&gt; and &lt;code&gt;&amp;lt;tooldescription&amp;gt;&lt;&#x2F;code&gt; tags; the user query is marked with a &lt;code&gt;&amp;lt;userq&amp;gt;&lt;&#x2F;code&gt; tag—for example:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #F8F8F2; background-color: #272822;&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;lt;toolname&amp;gt;get_weather&amp;lt;&#x2F;toolname&amp;gt;  &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;lt;tooldescription&amp;gt;Get current weather and 7-day forecast for a location&amp;lt;&#x2F;tooldescription&amp;gt;  &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;lt;userq&amp;gt;I&amp;#39;m planning a corporate retreat in Denver next weekend&amp;lt;&#x2F;userq&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You use the boolean parameters &lt;code&gt;userQueryIsJson&lt;&#x2F;code&gt; and &lt;code&gt;toolsIsJson&lt;&#x2F;code&gt; to specify the extraction mode the policy applies to each component independently. This means the two components need not share a format. A request can carry the user query as a JSON field while embedding tool definitions in text tags, or vice versa. The &lt;code&gt;JSONPath&lt;&#x2F;code&gt; parameters remain active for whichever component uses JSON extraction; for text-based components, those paths point to the field containing the raw text from which the policy extracts the tags.&lt;&#x2F;p&gt;
&lt;p&gt;Thus the policy does not prescribe a payload structure but adapts to the format the application already produces.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;choosing-a-selection-mode&quot;&gt;Choosing a selection mode&lt;&#x2F;h3&gt;
&lt;p&gt;How the policy behaves depends on the selection mode you opt for.&lt;&#x2F;p&gt;
&lt;p&gt;The primary benefit of &lt;code&gt;By Rank&lt;&#x2F;code&gt; is predictability. Token consumption for each request is bounded and thereby no room for surprises at high throughput.&lt;&#x2F;p&gt;
&lt;p&gt;However, rank is a relative measure. The top five tools can all score poorly against the query, because the query is ambiguous, the tool descriptions sparse, or the user&#x27;s intent genuinely spans multiple domains. &lt;code&gt;By Rank&lt;&#x2F;code&gt; still dispatches five tools, including ones marginally relevant at best. The mode merely returns the best available options within the defined count.&lt;&#x2F;p&gt;
&lt;p&gt;In &lt;code&gt;By Threshold&lt;&#x2F;code&gt;, the tool count adapts to the query: a precise, well-scoped request may return a single tool; a broad or ambiguous one may return several. It means the forwarded context reflects the actual semantic proximity between the query and the tool catalog, as opposed to a fixed count that may over- or under-represent relevance.&lt;&#x2F;p&gt;
&lt;p&gt;The tradeoff is that you need to calibrate deliberately. A lenient threshold value offers little reduction from the unfiltered baseline. A higher value might drop tools that are legitimately applicable but described in vocabulary that diverges from the query&#x27;s phrasing. The right value depends on the semantic density of the tool catalog, the consistency of its descriptions, and the variability of user query phrasing. Although &lt;code&gt;0.7&lt;&#x2F;code&gt; is a reasonable starting point, there is no default universal, and we suggest that production deployments perform empirical tuning against representative query samples.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;the-role-of-tool-descriptions&quot;&gt;The role of tool descriptions&lt;&#x2F;h3&gt;
&lt;p&gt;Across both modes, filtering quality is contingent on description quality, a point I&#x27;ve made earlier. Cosine similarity measures the angular distance between two embedding vectors; but it cannot recompense for descriptions that fail to encode what a tool actually does. A sound description produces a precise vector that scores strongly against queries about the sought property or objective and weakly against everything else.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;wrapping-up&quot;&gt;Wrapping up&lt;&#x2F;h2&gt;
&lt;p&gt;Tool bloat can discreetly become a principal concern as applications accumulate capabilities, and compounds across cost, quality, and latency simultaneously. By dint of addressing it at the gateway, you can dispense the fix universally without affecting application code.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>On Collecting Result Types in Rust</title>
        <published>2021-08-08T00:00:00+00:00</published>
        <updated>2021-08-08T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Abu Sakib
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://selectiveduplicate.github.io/blog/collecting-result/"/>
        <id>https://selectiveduplicate.github.io/blog/collecting-result/</id>
        
        <content type="html" xml:base="https://selectiveduplicate.github.io/blog/collecting-result/">&lt;p&gt;There&#x27;s a good chance that today&#x27;s topic being somewhat complex, I will end up explaining some stuff poorly. If it is and leaves something important off the discussion, I apologize beforehand. As someone new to Rust, I&#x27;m trying not to get too overwhelmed but at the same time squeezing out all the fun I&#x27;m able to. So I&#x27;m not exploring too deep for now so that I don&#x27;t get lost in confusion. For readers who are also fairly new to Rust, I hope this post helps your understanding, maybe piques your interest too, without casting an ominous shadow of fear about the language.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;so-iterators&quot;&gt;So... iterators...&lt;&#x2F;h2&gt;
&lt;p&gt;Iterators are awesome! They&#x27;re really handy when you&#x27;re looping over a sequence of data. Iterators play a huge role in Rust and chances are that even in a trivial Rust program that you&#x27;ve written, there are iterators.&lt;&#x2F;p&gt;
&lt;p&gt;Rust has only one trait that a type needs to implement so that it&#x27;s possible to iterate over a sequence of data belonging to those types.&lt;&#x2F;p&gt;
&lt;p&gt;Meet the &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;std&#x2F;iter&#x2F;trait.Iterator.html&quot;&gt;&lt;code&gt;Iterator&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; trait:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #F8F8F2; background-color: #272822;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F92672;&quot;&gt;pub&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;font-style: italic;&quot;&gt; trait&lt;&#x2F;span&gt;&lt;span style=&quot;color: #A6E22E;&quot;&gt; Iterator&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #66D9EF;font-style: italic;&quot;&gt;    type&lt;&#x2F;span&gt;&lt;span style=&quot;color: #A6E22E;&quot;&gt; Item&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #88846F;&quot;&gt;    &#x2F;&#x2F; Lots of methods below...&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;It has a bunch of functionalities. We&#x27;ll look at only one of these: &lt;code&gt;collect()&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s look at the signature:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #F8F8F2; background-color: #272822;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #A6E22E;&quot;&gt; collect&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt;B&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt; FromIterator&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FD971F;&quot;&gt;Self&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt;Item&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&amp;gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FD971F;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt; B&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F92672;&quot;&gt;where&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FD971F;&quot;&gt;	Self&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt; Sized&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt;    FromIterator&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #A6E22E;&quot;&gt;from_iter&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FD971F;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The signature, using &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;reference&#x2F;trait-bounds.html&quot;&gt;trait bounds&lt;&#x2F;a&gt;, tells us that this method works on any type &lt;code&gt;B&lt;&#x2F;code&gt; provided that it implements the &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;std&#x2F;iter&#x2F;trait.FromIterator.html&quot;&gt;&lt;code&gt;FromIterator&lt;&#x2F;code&gt; trait&lt;&#x2F;a&gt;. Under the hood, it calls the &lt;code&gt;from_iter&lt;&#x2F;code&gt; function of the &lt;code&gt;FromIterator&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;This trait only has one method:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #F8F8F2; background-color: #272822;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F92672;&quot;&gt;pub&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;font-style: italic;&quot;&gt; trait&lt;&#x2F;span&gt;&lt;span style=&quot;color: #A6E22E;&quot;&gt; FromIterator&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt;A&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F92672;&quot;&gt;    pub&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt; fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #A6E22E;&quot;&gt; from_iter&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;(iter&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt; T&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FD971F;&quot;&gt; Self&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F92672;&quot;&gt;    where&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt;        T&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt; IntoIterator&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt;Item&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt; A&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This method is widely used to &quot;collect&quot; data that we&#x27;re iterating over into a collection. If you read between the lines, you&#x27;ll understand that you can use it to convert one collection into another type of collection.&lt;&#x2F;p&gt;
&lt;p&gt;Take the f&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;std&#x2F;iter&#x2F;trait.Iterator.html#examples-28&quot;&gt;ollowing example from the docs&lt;&#x2F;a&gt; where it iterates over an array (a collection) and turns it into a vector (another type of collection):&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #F8F8F2; background-color: #272822;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #66D9EF;font-style: italic;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; a&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AE81FF;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AE81FF;&quot;&gt; 2&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AE81FF;&quot;&gt; 3&lt;&#x2F;span&gt;&lt;span&gt;];&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #66D9EF;font-style: italic;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; doubled&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt; Vec&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt;i32&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; a&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #A6E22E;&quot;&gt;iter&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F92672;&quot;&gt;                         .&lt;&#x2F;span&gt;&lt;span style=&quot;color: #A6E22E;&quot;&gt;map&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt;|&amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;x&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt;|&lt;&#x2F;span&gt;&lt;span&gt; x&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt; *&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AE81FF;&quot;&gt; 2&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F92672;&quot;&gt;                         .&lt;&#x2F;span&gt;&lt;span style=&quot;color: #A6E22E;&quot;&gt;collect&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #A6E22E;&quot;&gt;assert_eq!&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #A6E22E;&quot;&gt;vec!&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AE81FF;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AE81FF;&quot;&gt; 4&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AE81FF;&quot;&gt; 6&lt;&#x2F;span&gt;&lt;span&gt;], doubled);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;collecting-results&quot;&gt;Collecting &lt;code&gt;Result&lt;&#x2F;code&gt;s&lt;&#x2F;h2&gt;
&lt;p&gt;What about collections that are not your run-of-the-mill collections, like arrays and vectors? What about a vector of &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;std&#x2F;result&#x2F;enum.Result.html&quot;&gt;&lt;code&gt;Result&lt;&#x2F;code&gt; types&lt;&#x2F;a&gt;?&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;collect()&lt;&#x2F;code&gt; can also create instances of types that are not typical collections. For example, a &lt;code&gt;String&lt;&#x2F;code&gt; can be built from chars, and an iterator of &lt;code&gt;Result&amp;lt;T, E&amp;gt;&lt;&#x2F;code&gt; items can be collected into &lt;code&gt;Result&amp;lt;Collection&amp;lt;T&amp;gt;, E&amp;gt;&lt;&#x2F;code&gt;...&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;According to the docs, you can do that, because &lt;code&gt;Result&lt;&#x2F;code&gt; types implement the &lt;code&gt;FromIterator&lt;&#x2F;code&gt; trait.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;down-the-rabbit-hole&quot;&gt;Down the rabbit hole...&lt;&#x2F;h2&gt;
&lt;p&gt;Now let&#x27;s turn our eyes to the example that&#x27;s at the heart of today&#x27;s rant here:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #F8F8F2; background-color: #272822;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #66D9EF;font-style: italic;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; results&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color: #A6E22E;&quot;&gt;Ok&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AE81FF;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;),&lt;&#x2F;span&gt;&lt;span style=&quot;color: #A6E22E;&quot;&gt; Err&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E6DB74;&quot;&gt;&amp;quot;nope&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;),&lt;&#x2F;span&gt;&lt;span style=&quot;color: #A6E22E;&quot;&gt; Ok&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AE81FF;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;),&lt;&#x2F;span&gt;&lt;span style=&quot;color: #A6E22E;&quot;&gt; Err&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E6DB74;&quot;&gt;&amp;quot;bad&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;)];&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #66D9EF;font-style: italic;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; result&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt; Result&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt;Vec&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;_&amp;gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt; &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #A6E22E;&quot;&gt;str&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; results&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #A6E22E;&quot;&gt;iter&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #A6E22E;&quot;&gt;cloned&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #A6E22E;&quot;&gt;collect&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #88846F;&quot;&gt;&#x2F;&#x2F; gives us the first error&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #A6E22E;&quot;&gt;assert_eq!&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #A6E22E;&quot;&gt;Err&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E6DB74;&quot;&gt;&amp;quot;nope&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;), result);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #66D9EF;font-style: italic;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; results&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color: #A6E22E;&quot;&gt;Ok&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AE81FF;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;),&lt;&#x2F;span&gt;&lt;span style=&quot;color: #A6E22E;&quot;&gt; Ok&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AE81FF;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;)];&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #66D9EF;font-style: italic;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; result&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt; Result&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt;Vec&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;_&amp;gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt; &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #A6E22E;&quot;&gt;str&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; results&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #A6E22E;&quot;&gt;iter&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #A6E22E;&quot;&gt;cloned&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #A6E22E;&quot;&gt;collect&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #88846F;&quot;&gt;&#x2F;&#x2F; gives us the list of answers&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #A6E22E;&quot;&gt;assert_eq!&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #A6E22E;&quot;&gt;Ok&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #A6E22E;&quot;&gt;vec!&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AE81FF;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AE81FF;&quot;&gt; 3&lt;&#x2F;span&gt;&lt;span&gt;]), result);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Here we have an array of &lt;code&gt;Result&lt;&#x2F;code&gt; types, containing both &lt;code&gt;Ok&lt;&#x2F;code&gt; and &lt;code&gt;Err&lt;&#x2F;code&gt; variants. Using &lt;code&gt;collect()&lt;&#x2F;code&gt;, we&#x27;re &lt;em&gt;trying&lt;&#x2F;em&gt; to collect that into a &lt;code&gt;Result&lt;&#x2F;code&gt;...&lt;&#x2F;p&gt;
&lt;p&gt;Hmm, that feels.. awkward...&lt;&#x2F;p&gt;
&lt;p&gt;We&#x27;re not collecting into a vector of &lt;code&gt;Result&lt;&#x2F;code&gt;s, but rather into a &lt;code&gt;Result&lt;&#x2F;code&gt; that has a &lt;code&gt;Vector&lt;&#x2F;code&gt; variant.&lt;&#x2F;p&gt;
&lt;p&gt;So what? It does implement &lt;code&gt;FromIterator&lt;&#x2F;code&gt;, so &lt;code&gt;collect()&lt;&#x2F;code&gt; should work. Period.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s take a step back and try to deduce from what we know so far:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;Result&lt;&#x2F;code&gt; is an enum. So a &lt;code&gt;Result&lt;&#x2F;code&gt; type can only have one variant at a time, either &lt;code&gt;Ok&lt;&#x2F;code&gt;, or &lt;code&gt;Err&lt;&#x2F;code&gt;.&lt;&#x2F;li&gt;
&lt;li&gt;So after the operation on line 2, the &lt;code&gt;result&lt;&#x2F;code&gt; variable &lt;em&gt;should&lt;&#x2F;em&gt; contain only one variant, either the &lt;code&gt;Vec&lt;&#x2F;code&gt; variant (like the second &lt;code&gt;result&lt;&#x2F;code&gt; variable in the example ) or the &lt;code&gt;&amp;amp;str&lt;&#x2F;code&gt; variant representing the &lt;code&gt;Err&lt;&#x2F;code&gt;.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;It does end up having only one variant in each case, (the &lt;code&gt;Ok&lt;&#x2F;code&gt; variant with the vector, and the &lt;code&gt;Err&lt;&#x2F;code&gt; variant with an &lt;code&gt;&amp;amp;str&lt;&#x2F;code&gt;), but how is it only yielding the first &lt;code&gt;Err&lt;&#x2F;code&gt; variant?&lt;&#x2F;p&gt;
&lt;h3 id=&quot;searching-for-answers&quot;&gt;Searching for answers&lt;&#x2F;h3&gt;
&lt;p&gt;The magic happens inside the &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;std&#x2F;result&#x2F;enum.Result.html#impl-FromIterator%3CResult%3CA%2C%20E%3E%3E&quot;&gt;implementation of &lt;code&gt;FromIterator&lt;&#x2F;code&gt; for &lt;code&gt;Result&lt;&#x2F;code&gt; types&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;As I said at the beginning, I&#x27;m not going to dive too deep into the mystery behind this magic because that&#x27;d be something out of my depth (it is... for now). But we can go pretty far at understanding what&#x27;s happening under the hood.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s take a look at the relevant part of the source code of that implementation:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #F8F8F2; background-color: #272822;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F92672;&quot;&gt;impl&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt;A&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt; E&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt; V&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt; FromIterator&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt;A&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt; FromIterator&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt;Result&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt;A&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt; E&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt; for&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt; Result&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt;V&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt; E&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #88846F;&quot;&gt;    &#x2F;&#x2F;&#x2F; Takes each element in the `Iterator`: if it is an `Err`, no further&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #88846F;&quot;&gt;    &#x2F;&#x2F;&#x2F; elements are taken, and the `Err` is returned. Should no `Err` occur, a&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #88846F;&quot;&gt;    &#x2F;&#x2F;&#x2F; container with the values of each `Result` is returned.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt;    fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #A6E22E;&quot;&gt; from_iter&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt;I&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt; IntoIterator&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt;Item&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt; Result&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt;A&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt; E&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&amp;gt;&amp;gt;(iter&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt; I&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt; Result&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt;V&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt; E&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt;        iter&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #A6E22E;&quot;&gt;process_results&lt;&#x2F;span&gt;&lt;span&gt;(iter&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #A6E22E;&quot;&gt;into_iter&lt;&#x2F;span&gt;&lt;span&gt;(),&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt; |&lt;&#x2F;span&gt;&lt;span&gt;i&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt;|&lt;&#x2F;span&gt;&lt;span&gt; i&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #A6E22E;&quot;&gt;collect&lt;&#x2F;span&gt;&lt;span&gt;())&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The comments mention the behavior we&#x27;ve just observed in the previous example. At the first encounter of an &lt;code&gt;Err&lt;&#x2F;code&gt; variant, it returns that variant and stops collecting. Otherwise, it collects all the values into a container (collection) and returns that instead.&lt;&#x2F;p&gt;
&lt;p&gt;We already know that &lt;code&gt;FromIterator&lt;&#x2F;code&gt; has a single method that a type needs to define in order to implement the trait, and that&#x27;s &lt;code&gt;from_iter&lt;&#x2F;code&gt;; every type that implements it does so in different ways.&lt;&#x2F;p&gt;
&lt;p&gt;For &lt;code&gt;Result&lt;&#x2F;code&gt;, we see that it calls another utility function called &lt;code&gt;process_results&lt;&#x2F;code&gt; that does the actual job. &lt;code&gt;process_results&lt;&#x2F;code&gt; is tailored to work on &lt;code&gt;Result&lt;&#x2F;code&gt; type values.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s try to break it down as much as we can. Only keep in mind for now that &lt;em&gt;we&#x27;re iterating over &lt;code&gt;Result&lt;&#x2F;code&gt; types&lt;&#x2F;em&gt;.&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;From the signature, the single argument in &lt;code&gt;from_iter&lt;&#x2F;code&gt; has to implement the &lt;code&gt;IntoIterator&lt;&#x2F;code&gt; trait. If you scroll back a bit, we called &lt;code&gt;cloned()&lt;&#x2F;code&gt; before calling &lt;code&gt;collect()&lt;&#x2F;code&gt; on the array. And the r&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;std&#x2F;iter&#x2F;struct.Cloned.html#impl-IntoIterator&quot;&gt;eturn value type of &lt;code&gt;cloned()&lt;&#x2F;code&gt; implements &lt;code&gt;IntoIterator&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;.
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;cloned()&lt;&#x2F;code&gt; creates a new iterator that clones (makes copies of) the underlying elements, i.e. the &lt;code&gt;Result&lt;&#x2F;code&gt; types. The clones aren&#x27;t of type &lt;code&gt;&amp;amp;T&lt;&#x2F;code&gt; but type &lt;code&gt;T&lt;&#x2F;code&gt;.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;process_results&lt;&#x2F;code&gt; takes two arguments: a closure and an iterator. Since &lt;code&gt;cloned()&lt;&#x2F;code&gt; yields type &lt;code&gt;T&lt;&#x2F;code&gt;, &lt;code&gt;into_iter()&lt;&#x2F;code&gt; is used on &lt;code&gt;iter&lt;&#x2F;code&gt; so that it&#x27;s possible to iterate over &lt;code&gt;T&lt;&#x2F;code&gt; types (refer to the &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;std&#x2F;iter&#x2F;index.html#the-three-forms-of-iteration&quot;&gt;docs on the &lt;code&gt;iter&lt;&#x2F;code&gt; module&lt;&#x2F;a&gt; if you&#x27;re confused; it summarizes the types of iteration existing in Rust).&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h4 id=&quot;the-one-who-processes-results&quot;&gt;The one who processes Results&lt;&#x2F;h4&gt;
&lt;p&gt;Here&#x27;s the whole &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;rust-lang&#x2F;rust&#x2F;blob&#x2F;6830052c7b87217886324129bffbe096e485d415&#x2F;library&#x2F;core&#x2F;src&#x2F;iter&#x2F;adapters&#x2F;mod.rs#L138-L147&quot;&gt;&lt;code&gt;process_results&lt;&#x2F;code&gt; function&lt;&#x2F;a&gt;:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #F8F8F2; background-color: #272822;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #A6E22E;&quot;&gt; process_results&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt;I&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt; T&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt; E&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt; F&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt; U&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;(iter&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt; I&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt; mut&lt;&#x2F;span&gt;&lt;span&gt; f&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt; F&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt; Result&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt;U&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt; E&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F92672;&quot;&gt;where&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt;    I&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt; Iterator&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt;Item&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt; Result&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt; E&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&amp;gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F92672;&quot;&gt;    for&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #A6E22E;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt; F&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #A6E22E;&quot;&gt; FnMut&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt;ResultShunt&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #A6E22E;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt; I&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt; E&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt; U&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #66D9EF;font-style: italic;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt; mut&lt;&#x2F;span&gt;&lt;span&gt; error&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #A6E22E;&quot;&gt; Ok&lt;&#x2F;span&gt;&lt;span&gt;(());&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #66D9EF;font-style: italic;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span&gt; shunt&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt; ResultShunt&lt;&#x2F;span&gt;&lt;span&gt; { iter, error&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt;: &amp;amp;mut&lt;&#x2F;span&gt;&lt;span&gt; error };&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #66D9EF;font-style: italic;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span&gt; value&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #A6E22E;&quot;&gt; f&lt;&#x2F;span&gt;&lt;span&gt;(shunt);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    error&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #A6E22E;&quot;&gt;map&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt;|&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt;|&lt;&#x2F;span&gt;&lt;span&gt; value)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Looks like the closure works on a type called &lt;code&gt;ResultShunt&lt;&#x2F;code&gt;... what the heck is that?&lt;&#x2F;p&gt;
&lt;h4 id=&quot;the-iterator-that-wraps-another-iterator&quot;&gt;The iterator that wraps another iterator&lt;&#x2F;h4&gt;
&lt;p&gt;&lt;code&gt;ResultShunt&lt;&#x2F;code&gt; is another iterator that wraps the first iterator &lt;code&gt;iter&lt;&#x2F;code&gt; passed to the function. &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;rust-lang&#x2F;rust&#x2F;blob&#x2F;6830052c7b87217886324129bffbe096e485d415&#x2F;library&#x2F;core&#x2F;src&#x2F;iter&#x2F;adapters&#x2F;mod.rs#L130-L133&quot;&gt;Here&#x27;s how &lt;code&gt;ResultShunt&lt;&#x2F;code&gt; looks like&lt;&#x2F;a&gt;:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #F8F8F2; background-color: #272822;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #88846F;&quot;&gt;&#x2F;&#x2F;&#x2F; An iterator adapter that produces output as long as the underlying&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #88846F;&quot;&gt;&#x2F;&#x2F;&#x2F; iterator produces `Result::Ok` values.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #88846F;&quot;&gt;&#x2F;&#x2F;&#x2F;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #88846F;&quot;&gt;&#x2F;&#x2F;&#x2F; If an error is encountered, the iterator stops and the error is&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #88846F;&quot;&gt;&#x2F;&#x2F;&#x2F; stored.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #66D9EF;font-style: italic;&quot;&gt;struct&lt;&#x2F;span&gt;&lt;span style=&quot;color: #A6E22E;&quot;&gt; ResultShunt&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #A6E22E;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt; I&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt; E&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    iter&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt; I&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    error&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #A6E22E;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt; mut&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt; Result&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;(),&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt; E&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;At this point, let&#x27;s think through a couple of things:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;The closure is iterating over &lt;code&gt;ResultShunt&lt;&#x2F;code&gt; types. That could only mean that &lt;code&gt;ResultShunt&lt;&#x2F;code&gt; type implements the &lt;code&gt;Iterator&lt;&#x2F;code&gt; trait.&lt;&#x2F;li&gt;
&lt;li&gt;The doc comments describe the behavior we&#x27;re investigating.
&lt;ul&gt;
&lt;li&gt;Quite intuitively, the &lt;code&gt;error&lt;&#x2F;code&gt; field only cares about the &lt;code&gt;E&lt;&#x2F;code&gt; type of a &lt;code&gt;Result&lt;&#x2F;code&gt; since it has to stop at the first encounter of an &lt;code&gt;Err&lt;&#x2F;code&gt; variant.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;The return type of &lt;code&gt;process_results&lt;&#x2F;code&gt; is a &lt;code&gt;Result&amp;lt;U,E&amp;gt;&lt;&#x2F;code&gt; type, whereas the closure it takes as a parameter returns only the &lt;code&gt;Ok&lt;&#x2F;code&gt; value &lt;code&gt;U&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;em&gt;That&#x27;s how the &lt;code&gt;collect&lt;&#x2F;code&gt; method in the closure ends up collecting the &lt;code&gt;Ok&lt;&#x2F;code&gt; variants if there are no errors.&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;p&gt;We don&#x27;t need to go over all of &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;rust-lang&#x2F;rust&#x2F;blob&#x2F;6830052c7b87217886324129bffbe096e485d415&#x2F;library&#x2F;core&#x2F;src&#x2F;iter&#x2F;adapters&#x2F;mod.rs#L149-L197&quot;&gt;this implementation&lt;&#x2F;a&gt;. For now, notice the type parameters that show up in the &lt;code&gt;impl&lt;&#x2F;code&gt; signature:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #F8F8F2; background-color: #272822;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F92672;&quot;&gt;impl&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt;I&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt; T&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt; E&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt; Iterator&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt; for&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt; ResultShunt&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #A6E22E;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt; I&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt; E&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F92672;&quot;&gt;where&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt;    I&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt; Iterator&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt;Item&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt; Result&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt; E&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&amp;gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #66D9EF;font-style: italic;&quot;&gt;    type&lt;&#x2F;span&gt;&lt;span style=&quot;color: #A6E22E;&quot;&gt; Item&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt; T&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F92672;&quot;&gt;...&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Here &lt;code&gt;I&lt;&#x2F;code&gt; is the old iterator over &lt;code&gt;Result&lt;&#x2F;code&gt; types. But notice the associated type here:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #F8F8F2; background-color: #272822;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F92672;&quot;&gt;...&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #66D9EF;font-style: italic;&quot;&gt;type&lt;&#x2F;span&gt;&lt;span style=&quot;color: #A6E22E;&quot;&gt; Item&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt; T&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F92672;&quot;&gt;...&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This indicates the &lt;em&gt;type&lt;&#x2F;em&gt; of elements being iterated over. So &lt;code&gt;collect&lt;&#x2F;code&gt; is actually collecting this &lt;code&gt;T&lt;&#x2F;code&gt; type, which already has a &lt;code&gt;FromIterator&lt;&#x2F;code&gt;  implementation (in our case, it&#x27;s an integer). That explains how it yielded the vector containing all &lt;code&gt;Ok&lt;&#x2F;code&gt; variants in our example.&lt;&#x2F;p&gt;
&lt;h4 id=&quot;finally-getting-there&quot;&gt;Finally getting there...&lt;&#x2F;h4&gt;
&lt;p&gt;The implementation &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;rust-lang&#x2F;rust&#x2F;blob&#x2F;6830052c7b87217886324129bffbe096e485d415&#x2F;library&#x2F;core&#x2F;src&#x2F;iter&#x2F;adapters&#x2F;mod.rs#L168-L183&quot;&gt;has a try_fold method&lt;&#x2F;a&gt;. Again, only the following bit of code is relevant for now:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #F8F8F2; background-color: #272822;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #A6E22E;&quot;&gt;Err&lt;&#x2F;span&gt;&lt;span&gt;(e)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt; =&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F92672;&quot;&gt;    *&lt;&#x2F;span&gt;&lt;span&gt;error&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #A6E22E;&quot;&gt; Err&lt;&#x2F;span&gt;&lt;span&gt;(e);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt;    ControlFlow&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #A6E22E;&quot;&gt;Break&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt;try&lt;&#x2F;span&gt;&lt;span&gt; { acc })&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We can disregard the feeling of &quot;understanding everything&quot;, which is impossible, in favor of how much we&#x27;ve been able to dig up incrementally and make a sense out of. And seeing the above bit of code will make a click sound in your brain...&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;At the encounter of an &lt;code&gt;Err&lt;&#x2F;code&gt;, its value is stored in the &lt;code&gt;error&lt;&#x2F;code&gt; field of the &lt;code&gt;ResultShunt&lt;&#x2F;code&gt; type. Then it just stops iterating, as advertised.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;process_results&lt;&#x2F;code&gt; makes a return with either success values or the error value (the last two lines):&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #F8F8F2; background-color: #272822;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #66D9EF;font-style: italic;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; value&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #A6E22E;&quot;&gt; f&lt;&#x2F;span&gt;&lt;span&gt;(shunt);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;error&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #A6E22E;&quot;&gt;map&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt;|&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt;|&lt;&#x2F;span&gt;&lt;span&gt; value)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This is the last stretch of explanation that we need to wrap this up. Let&#x27;s break it down:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Remember the closure signature we observed earlier in &lt;code&gt;process_results&lt;&#x2F;code&gt;. It returns the &lt;code&gt;Ok&lt;&#x2F;code&gt; value. So the &lt;code&gt;value&lt;&#x2F;code&gt; above is just that (a string, integer whatever, but specifically in the example of ours it&#x27;s an integer).&lt;&#x2F;li&gt;
&lt;li&gt;Next we&#x27;re &lt;code&gt;map&lt;&#x2F;code&gt;ping over the &lt;code&gt;error&lt;&#x2F;code&gt; variable, which is a &lt;code&gt;Result&lt;&#x2F;code&gt; type. &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;std&#x2F;result&#x2F;enum.Result.html#method.map&quot;&gt;This is how &lt;code&gt;map&lt;&#x2F;code&gt; works on &lt;code&gt;Result&lt;&#x2F;code&gt; types&lt;&#x2F;a&gt;:
&lt;ul&gt;
&lt;li&gt;It doesn&#x27;t touch the &lt;code&gt;Err&lt;&#x2F;code&gt; value, but applies the closure to the &lt;code&gt;Ok&lt;&#x2F;code&gt; value, transforming the initial &lt;code&gt;Result&lt;&#x2F;code&gt; into another. The closure argument is &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;std&#x2F;primitive.unit.html&quot;&gt;the &quot;unit&quot; type&lt;&#x2F;a&gt;, meaning it&#x27;s taking no argument and mapping the &lt;code&gt;Ok&lt;&#x2F;code&gt; values to &lt;code&gt;value&lt;&#x2F;code&gt;.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;Now if the program encountered an error, the &lt;code&gt;error&lt;&#x2F;code&gt; variable already contains it, and the &lt;code&gt;map&lt;&#x2F;code&gt; operation above wouldn&#x27;t matter because it only touches the &lt;code&gt;Ok&lt;&#x2F;code&gt; values. That&#x27;s how we get &lt;code&gt;Err(&quot;nope&quot;)&lt;&#x2F;code&gt;  in the first case of our example.&lt;&#x2F;li&gt;
&lt;li&gt;Otherwise, &lt;code&gt;collect&lt;&#x2F;code&gt; collects the &lt;code&gt;Ok&lt;&#x2F;code&gt; values into a collection, and we&#x27;re getting &lt;code&gt;Ok(vec![1, 3])&lt;&#x2F;code&gt; in the second case of our example.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;Phew! 😌 That was a wild ride but we&#x27;ve managed to solve much of the mystery! I&#x27;m going to scream with joy into my pillow and then pet my cat... 🎉🎉💪&lt;&#x2F;p&gt;
&lt;h2 id=&quot;parting-words&quot;&gt;Parting words&lt;&#x2F;h2&gt;
&lt;p&gt;Thanks for reading up to this point and descent into a bit of madness with me. Feel free to hit me up on &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;twitter.com&#x2F;Slctvdplcate&quot;&gt;Twitter&lt;&#x2F;a&gt; with any feedback that you might have. I hope you find it useful in some way.&lt;&#x2F;p&gt;
&lt;p&gt;This article could not have been possible without the following fine people and communities:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;The amazing community of &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;www.twitch.tv&#x2F;togglebit&#x2F;about&quot;&gt;Togglebit&lt;&#x2F;a&gt; where I started the initial discussion around the topics.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;shepmaster&quot;&gt;Jake Goulding&lt;&#x2F;a&gt; for jumping in on the Rust Discord server so quickly and being patient with my questions.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Rust closures: How a closure can implement more than one trait</title>
        <published>2021-07-04T00:00:00+00:00</published>
        <updated>2021-07-04T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Abu Sakib
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://selectiveduplicate.github.io/blog/closure-traits-rust/"/>
        <id>https://selectiveduplicate.github.io/blog/closure-traits-rust/</id>
        
        <content type="html" xml:base="https://selectiveduplicate.github.io/blog/closure-traits-rust/">&lt;p&gt;A pretty interesting conversation took place on a &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;twitter.com&#x2F;Slctvdplcate&#x2F;status&#x2F;1404843998297018370?s=20&quot;&gt;series of Tweets&lt;&#x2F;a&gt; a while back on Rust closures. This was (and still is, although less than before) a confusing topic for me as a Rust newbie. So I was expecting some help from the Rust Twitter community. What I&#x27;m about to discuss today comes as a side effect out of that discussion; it wasn&#x27;t my line of query initially.&lt;&#x2F;p&gt;
&lt;p&gt;My confusion centered around how closures implement the three traits &lt;code&gt;Fn&lt;&#x2F;code&gt;, &lt;code&gt;FnOnce&lt;&#x2F;code&gt;, and &lt;code&gt;FnMut&lt;&#x2F;code&gt;. From a slender look it looks fairly simple:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;You can call closures that implement &lt;code&gt;Fn&lt;&#x2F;code&gt; multiple times; you can&#x27;t use them to mutate anything.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;FnOnce&lt;&#x2F;code&gt; closures are callable only once. This is for scenarios where a piece of data could be &lt;em&gt;moved into&lt;&#x2F;em&gt; the closure.
So after the first call, you can&#x27;t call that closure again since it&#x27;s been moved during the first call (unless the data type in question implements the &lt;code&gt;Clone&lt;&#x2F;code&gt;
trait).&lt;&#x2F;li&gt;
&lt;li&gt;Closures that mutate the state implement &lt;code&gt;FnMut&lt;&#x2F;code&gt;. You can call this type of closure more than one time.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;Let&#x27;s look at an example:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #F8F8F2; background-color: #272822;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #A6E22E;&quot;&gt; main&lt;&#x2F;span&gt;&lt;span&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #88846F;&quot;&gt;    &#x2F;&#x2F; this closure doesn&amp;#39;t capture anything &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #66D9EF;font-style: italic;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span&gt; print_&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt; = |&lt;&#x2F;span&gt;&lt;span&gt;film, rating&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color: #A6E22E;&quot;&gt; println!&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E6DB74;&quot;&gt;&amp;quot;{}: {}&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, film, rating);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #66D9EF;font-style: italic;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span&gt; film_ratings&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; [(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E6DB74;&quot;&gt;&amp;quot;Sunset Boulevard&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AE81FF;&quot;&gt; 7&lt;&#x2F;span&gt;&lt;span&gt;), (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E6DB74;&quot;&gt;&amp;quot;The Last&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AE81FF;&quot;&gt; 6&lt;&#x2F;span&gt;&lt;span&gt;), (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E6DB74;&quot;&gt;&amp;quot;Drive&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AE81FF;&quot;&gt; 8&lt;&#x2F;span&gt;&lt;span&gt;)];&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F92672;&quot;&gt;    for&lt;&#x2F;span&gt;&lt;span&gt; (film, rating)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt; in&lt;&#x2F;span&gt;&lt;span&gt; film_ratings&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #A6E22E;&quot;&gt;iter&lt;&#x2F;span&gt;&lt;span&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #A6E22E;&quot;&gt;        print_&lt;&#x2F;span&gt;&lt;span&gt;(film, rating);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The comment says it: this closure doesn&#x27;t capture anything from the scope. So what trait does it implement?&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s look at the &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;std&#x2F;ops&#x2F;trait.Fn.html&quot;&gt;docs on &lt;code&gt;Fn&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;:&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;Fn is implemented automatically by closures which only take immutable references to captured variables or don&#x27;t capture anything at all...&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;So that&#x27;s it then, right?&lt;&#x2F;p&gt;
&lt;p&gt;Hmm, I have trust issues so let&#x27;s test that statement on our piece of code. &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;twitter.com&#x2F;oconnor663&quot;&gt;Jack&lt;&#x2F;a&gt; was kind enough to provide the following code snippet that checks what trait(s) the closure implements.&lt;&#x2F;p&gt;
&lt;p&gt;It uses &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;reference&#x2F;types&#x2F;trait-object.html&quot;&gt;&lt;em&gt;trait objects&lt;&#x2F;em&gt;&lt;&#x2F;a&gt; to create three variables of three types corresponding to each of the traits &lt;code&gt;Fn&lt;&#x2F;code&gt;, &lt;code&gt;FnOnce&lt;&#x2F;code&gt; and &lt;code&gt;FnMut&lt;&#x2F;code&gt;. Then they&#x27;re assigned to the closure.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #F8F8F2; background-color: #272822;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #66D9EF;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #A6E22E;&quot;&gt; main&lt;&#x2F;span&gt;&lt;span&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #66D9EF;font-style: italic;&quot;&gt;   let&lt;&#x2F;span&gt;&lt;span&gt; print_&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt; = |&lt;&#x2F;span&gt;&lt;span&gt;film, rating&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color: #A6E22E;&quot;&gt; println!&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E6DB74;&quot;&gt;&amp;quot;{}: {}&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, film, rating);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #66D9EF;font-style: italic;&quot;&gt;   let&lt;&#x2F;span&gt;&lt;span&gt; film_ratings&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; [(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E6DB74;&quot;&gt;&amp;quot;Sunset Boulevard&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AE81FF;&quot;&gt; 7&lt;&#x2F;span&gt;&lt;span&gt;), (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E6DB74;&quot;&gt;&amp;quot;The Last&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AE81FF;&quot;&gt; 6&lt;&#x2F;span&gt;&lt;span&gt;), (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E6DB74;&quot;&gt;&amp;quot;Drive&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #AE81FF;&quot;&gt; 8&lt;&#x2F;span&gt;&lt;span&gt;)];&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F92672;&quot;&gt;   for&lt;&#x2F;span&gt;&lt;span&gt; (film, rating)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt; in&lt;&#x2F;span&gt;&lt;span&gt; film_ratings&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #A6E22E;&quot;&gt;iter&lt;&#x2F;span&gt;&lt;span&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #A6E22E;&quot;&gt;       print_&lt;&#x2F;span&gt;&lt;span&gt;(film, rating);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;   }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #66D9EF;font-style: italic;&quot;&gt;   let&lt;&#x2F;span&gt;&lt;span&gt; _this_works&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt;: &amp;amp;dyn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #A6E22E;&quot;&gt; FnOnce&lt;&#x2F;span&gt;&lt;span&gt;(_, _)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt; = &amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;print_;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #66D9EF;font-style: italic;&quot;&gt;   let&lt;&#x2F;span&gt;&lt;span&gt; _this_works_too&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt;: &amp;amp;dyn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #A6E22E;&quot;&gt; FnMut&lt;&#x2F;span&gt;&lt;span&gt;(_, _)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt; = &amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;print_;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #66D9EF;font-style: italic;&quot;&gt;   let&lt;&#x2F;span&gt;&lt;span&gt; _all_of_these_work&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt;: &amp;amp;dyn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #A6E22E;&quot;&gt; Fn&lt;&#x2F;span&gt;&lt;span&gt;(_, _)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F92672;&quot;&gt; = &amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;print_;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This code compiles without so much as a warning...&lt;&#x2F;p&gt;
&lt;p&gt;So... the closure implements each trait. The question that arises now is why it ends up implementing all three (put pressure on the phrase &quot;ends up&quot;).&lt;&#x2F;p&gt;
&lt;p&gt;I found a &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;30177395&#x2F;when-does-a-closure-implement-fn-fnmut-and-fnonce#30232500&quot;&gt;Stack Overflow&lt;&#x2F;a&gt; answer that started shedding some light on that question.&lt;&#x2F;p&gt;
&lt;p&gt;It says that all closures implement &lt;code&gt;FnOnce&lt;&#x2F;code&gt;. Why? Because you should be able to call a closure at least once. Closures are functions, and if you can&#x27;t call a function at least once it&#x27;s not living up to its name is it?!&lt;&#x2F;p&gt;
&lt;p&gt;So our closure implements &lt;code&gt;FnOnce&lt;&#x2F;code&gt;. And according to the docs, since it doesn&#x27;t capture anything, it also implements &lt;code&gt;Fn&lt;&#x2F;code&gt;. That&#x27;s two out of three. What about the third one &lt;code&gt;FnMut&lt;&#x2F;code&gt;?&lt;&#x2F;p&gt;
&lt;p&gt;The following line &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;std&#x2F;ops&#x2F;trait.Fn.html&quot;&gt;in the &lt;code&gt;Fn&lt;&#x2F;code&gt; docs&lt;&#x2F;a&gt; tries to give something away:&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;Since both FnMut and FnOnce are supertraits of Fn, any instance of Fn can be used as a parameter where a FnMut or FnOnce is expected.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;Wait what? &quot;Supertraits&quot;?&lt;&#x2F;p&gt;
&lt;p&gt;Supertraits are Rust&#x27;s way of achieving inheritance-like features. You can define a trait that uses some of the functionalities of another trait. The first trait then becomes the &quot;supertrait&quot; (or &quot;parent trait&quot;, somewhat mirroring the OOP term &quot;parent class&quot;), and the second trait that depends on the first one becomes a &quot;subtrait&quot;.&lt;&#x2F;p&gt;
&lt;p&gt;For a type that implements a subtrait, it also needs to implement its supertrait. That&#x27;s enough, for now, to understand what&#x27;s going on with our closures. I might delve into supertraits in another article in the future for my sanity (and clarity on it).&lt;&#x2F;p&gt;
&lt;p&gt;In our closure&#x27;s case, so another way to explain its behavior is that since it implements &lt;code&gt;Fn&lt;&#x2F;code&gt;, it also implements the supertraits &lt;code&gt;FnOnce&lt;&#x2F;code&gt; and &lt;code&gt;FnMut&lt;&#x2F;code&gt;. This marks another claim made in that Stack Overflow answer:&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;A closure |...| ... will automatically implement as many of those [traits] as it can.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;Bottomline:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;All closures implement &lt;code&gt;FnOnce&lt;&#x2F;code&gt;. This has to happen since all closures can at least be called once.&lt;&#x2F;li&gt;
&lt;li&gt;Our example closure doesn&#x27;t capture anything from its sorrounding scope. Because of that, it implements &lt;code&gt;Fn&lt;&#x2F;code&gt;.&lt;&#x2F;li&gt;
&lt;li&gt;In order to implement &lt;code&gt;Fn&lt;&#x2F;code&gt;, it also ends up implementing the spuertraits &lt;code&gt;FnOnce&lt;&#x2F;code&gt; and &lt;code&gt;FnMut&lt;&#x2F;code&gt;.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;Oh &lt;code&gt;FnOnce&lt;&#x2F;code&gt; is also a supertrait of &lt;code&gt;Fnmut&lt;&#x2F;code&gt;...&lt;&#x2F;p&gt;
&lt;p&gt;That&#x27;s it for today&#x27;s ramblings. Hope you&#x27;ve found it useful in some way. Thanks for reading!&lt;&#x2F;p&gt;
</content>
        
    </entry>
</feed>
