<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: SomeB1oody</title>
    <description>The latest articles on DEV Community by SomeB1oody (@someb1oody).</description>
    <link>https://dev.to/someb1oody</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2563425%2F66f2ab20-9536-4814-9f8b-5b6ab698a194.png</url>
      <title>DEV Community: SomeB1oody</title>
      <link>https://dev.to/someb1oody</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/someb1oody"/>
    <language>en</language>
    <item>
      <title>[Rust Guide] 14.6. Installing Binary Crates</title>
      <dc:creator>SomeB1oody</dc:creator>
      <pubDate>Wed, 08 Jul 2026 03:35:42 +0000</pubDate>
      <link>https://dev.to/someb1oody/rust-guide-146-installing-binary-crates-33mc</link>
      <guid>https://dev.to/someb1oody/rust-guide-146-installing-binary-crates-33mc</guid>
      <description>&lt;p&gt;&lt;strong&gt;If you find this helpful, please like, bookmark, and follow. To keep learning along, follow this series.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  14.6.1 Install a Binary Crate from crates.io
&lt;/h2&gt;

&lt;p&gt;You can use the &lt;code&gt;cargo install&lt;/code&gt; command to install a binary crate from &lt;a href="https://crates.io/" rel="noopener noreferrer"&gt;crates.io&lt;/a&gt;. This is not meant to replace system packages; it is intended as a convenient way for Rust developers to install tools shared by others.&lt;/p&gt;

&lt;p&gt;Its limitation is that it can only install crates that have a binary target. A binary target is an executable program, produced by a crate that has &lt;code&gt;src/main.rs&lt;/code&gt; or is otherwise configured as a binary crate.&lt;/p&gt;

&lt;p&gt;Since there is the concept of a binary target, there is also the concept of a library target. A library target cannot be run by itself.&lt;/p&gt;

&lt;p&gt;Usually, the &lt;code&gt;README.md&lt;/code&gt; file contains a description of the crate and tells you whether the crate has a library target, a binary target, or both.&lt;/p&gt;

&lt;h2&gt;
  
  
  14.6.2 &lt;code&gt;cargo install&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Binary files installed by &lt;code&gt;cargo install&lt;/code&gt; are placed in the &lt;code&gt;bin&lt;/code&gt; folder under the home directory.&lt;/p&gt;

&lt;p&gt;If you installed Rust with the default &lt;code&gt;rustup&lt;/code&gt; configuration, the binary directory is &lt;code&gt;$HOME/.cargo/bin&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To make programs installed by &lt;code&gt;cargo install&lt;/code&gt; directly executable, you need to make sure that directory is in the &lt;code&gt;$PATH&lt;/code&gt; environment variable.&lt;/p&gt;

&lt;p&gt;For example, in Chapter 12 we mentioned the Rust implementation of the &lt;code&gt;grep&lt;/code&gt; tool, called &lt;code&gt;ripgrep&lt;/code&gt;, which is used to search files. To install &lt;code&gt;ripgrep&lt;/code&gt;, we can run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ cargo install ripgrep
    Updating crates.io index
  Downloaded ripgrep v13.0.0
  Downloaded 1 crate (243.3 KB) in 0.88s
  Installing ripgrep v13.0.0
......
   Compiling ripgrep v13.0.0
    Finished release [optimized + debuginfo] target(s) in 3m 10s
  Installing ~/.cargo/bin/rg
   Installed package `ripgrep v13.0.0` (executable `rg`)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Some output has been omitted, but this is roughly what it looks like. The penultimate line shows that the program was installed at &lt;code&gt;~/.cargo/bin/rg&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In the terminal, use &lt;code&gt;echo $PATH&lt;/code&gt; to check whether this directory is in the environment variable.&lt;/p&gt;

&lt;h2&gt;
  
  
  14.6.3 Extending Cargo with Custom Commands
&lt;/h2&gt;

&lt;p&gt;Cargo is designed so that it can be extended with subcommands.&lt;/p&gt;

&lt;p&gt;For example, if a binary in &lt;code&gt;$PATH&lt;/code&gt; is named &lt;code&gt;cargo-something&lt;/code&gt;, you can run it with &lt;code&gt;cargo something&lt;/code&gt;, treating it as if it were a Cargo subcommand.&lt;/p&gt;

&lt;p&gt;When you run &lt;code&gt;cargo --list&lt;/code&gt;, custom commands like this are also listed. Being able to install extensions with &lt;code&gt;cargo install&lt;/code&gt; and then run them like built-in Cargo tools is a very convenient benefit of Cargo’s design.&lt;/p&gt;

</description>
      <category>rust</category>
      <category>programming</category>
      <category>learning</category>
    </item>
    <item>
      <title>[Rsut Guide] 14.5. Cargo Workspace</title>
      <dc:creator>SomeB1oody</dc:creator>
      <pubDate>Wed, 08 Jul 2026 03:34:01 +0000</pubDate>
      <link>https://dev.to/someb1oody/rsut-guide-145-cargo-workspace-7l</link>
      <guid>https://dev.to/someb1oody/rsut-guide-145-cargo-workspace-7l</guid>
      <description>&lt;p&gt;&lt;strong&gt;If you find this helpful, please like, bookmark, and follow. To keep learning along, follow this series.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  14.5.1 Why Cargo Workspaces Are Needed
&lt;/h2&gt;

&lt;p&gt;Suppose we build a binary crate that contains both a library and an application. As the project grows, the library crate may become larger and larger. In that case, it is usually split into multiple packages. For this need, Rust provides Cargo workspaces, also called cargo workspaces.&lt;/p&gt;

&lt;p&gt;Cargo workspaces help manage multiple related crates that need to be developed together. In essence, they are a set of packages that share the same &lt;code&gt;Cargo.lock&lt;/code&gt; and output files.&lt;/p&gt;

&lt;h2&gt;
  
  
  14.5.2 Using a Workspace
&lt;/h2&gt;

&lt;p&gt;There are multiple ways to create a workspace.&lt;/p&gt;

&lt;p&gt;Here is an example: this workspace contains one binary crate and one library crate:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The binary crate has a &lt;code&gt;main&lt;/code&gt; function and depends on the library crate.&lt;/li&gt;
&lt;li&gt;One library crate provides a function called &lt;code&gt;add_one&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  1. Create the Workspace Directory
&lt;/h3&gt;

&lt;p&gt;First, create a directory for the workspace. I will name it &lt;code&gt;add&lt;/code&gt;. Enter the following commands in the terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ mkdir add
$ cd add
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Use the Workspace in the Main Project
&lt;/h3&gt;

&lt;p&gt;Next, inside the &lt;code&gt;add&lt;/code&gt; directory, create a &lt;code&gt;Cargo.toml&lt;/code&gt; file that configures the entire workspace. This file will not have a &lt;code&gt;[package]&lt;/code&gt; section. Instead, it starts with a &lt;code&gt;[workspace]&lt;/code&gt; section:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight toml"&gt;&lt;code&gt;&lt;span class="nn"&gt;[workspace]&lt;/span&gt;

&lt;span class="py"&gt;members&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="s"&gt;"adder"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;adder&lt;/code&gt; is the name I gave the binary crate, and this list can be extended with more members.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Add the Library
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ cargo new adder
     Created binary (application) `adder` package
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command creates the &lt;code&gt;adder&lt;/code&gt; crate under &lt;code&gt;add/adder&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;At this point, the project structure looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;├── Cargo.lock
├── Cargo.toml
├── adder
│   ├── Cargo.toml
│   └── src
│       └── main.rs
└── target
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that at this point, we can run &lt;code&gt;cargo build&lt;/code&gt; for the &lt;code&gt;add&lt;/code&gt; project, and we can also run &lt;code&gt;cargo build&lt;/code&gt; for the &lt;code&gt;adder&lt;/code&gt; crate under &lt;code&gt;add&lt;/code&gt;. However, there will only be one &lt;code&gt;target&lt;/code&gt; directory and one &lt;code&gt;Cargo.lock&lt;/code&gt; file, both under &lt;code&gt;add&lt;/code&gt;, and the build output of the &lt;code&gt;adder&lt;/code&gt; crate will also be stored there. Because crates in a workspace are often interdependent, having a separate &lt;code&gt;target&lt;/code&gt; directory for each folder would force developers to repeatedly rebuild the other crates in the workspace.&lt;/p&gt;

&lt;p&gt;Next, add another crate:&lt;/p&gt;

&lt;p&gt;The other crate is called &lt;code&gt;add_one&lt;/code&gt;, so update the workspace information:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight toml"&gt;&lt;code&gt;&lt;span class="nn"&gt;[workspace]&lt;/span&gt;

&lt;span class="py"&gt;members&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="s"&gt;"adder"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s"&gt;"add_one"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use &lt;code&gt;cargo new&lt;/code&gt; to add the library. Remember to use the &lt;code&gt;--lib&lt;/code&gt; flag to declare it as a library crate:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ cargo new add_one --lib
     Created library `add_one` package
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the project structure looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;├── Cargo.lock
├── Cargo.toml
├── add_one
│   ├── Cargo.toml
│   └── src
│       └── lib.rs
├── adder
│   ├── Cargo.toml
│   └── src
│       └── main.rs
└── target
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  4. Write the Code
&lt;/h3&gt;

&lt;p&gt;In &lt;code&gt;add_one/src/lib.rs&lt;/code&gt;, add an &lt;code&gt;add_one&lt;/code&gt; function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;add_one&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we can make the &lt;code&gt;adder&lt;/code&gt; package and our binary depend on &lt;code&gt;add_one&lt;/code&gt;. First, add the path dependency &lt;code&gt;add_one&lt;/code&gt; to &lt;code&gt;adder/Cargo.toml&lt;/code&gt;, because Cargo does not assume that crates in a workspace depend on each other, so we need to make the dependency explicit. Write this in &lt;code&gt;adder/Cargo.toml&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight toml"&gt;&lt;code&gt;&lt;span class="nn"&gt;[dependencies]&lt;/span&gt;
&lt;span class="py"&gt;add_one&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="py"&gt;path&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"../add_one"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, let’s use the &lt;code&gt;add_one&lt;/code&gt; function from the &lt;code&gt;add_one&lt;/code&gt; crate. Open &lt;code&gt;adder/src/main.rs&lt;/code&gt;, add &lt;code&gt;use&lt;/code&gt; at the top to bring &lt;code&gt;add_one&lt;/code&gt; into scope, and then modify &lt;code&gt;main&lt;/code&gt; to call the &lt;code&gt;add_one&lt;/code&gt; function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="n"&gt;add_one&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello, world! {num} plus one is {}!"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nn"&gt;add_one&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;add_one&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  5. Build
&lt;/h3&gt;

&lt;p&gt;Run &lt;code&gt;cargo build&lt;/code&gt; for the &lt;code&gt;add&lt;/code&gt; project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ cargo build
   Compiling add_one v0.1.0 (file:///projects/add/add_one)
   Compiling adder v0.1.0 (file:///projects/add/adder)
    Finished dev [unoptimized + debuginfo] target(s) in 0.68s
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;No errors occurred; it runs normally.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Testing
&lt;/h2&gt;

&lt;p&gt;We can also run tests for a specific package in the workspace from the top-level directory by using the &lt;code&gt;-p&lt;/code&gt; flag and specifying the package name. For example, to test only the &lt;code&gt;add_one&lt;/code&gt; function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ cargo test -p add_one
    Finished test [unoptimized + debuginfo] target(s) in 0.00s
     Running unittests src/lib.rs (target/debug/deps/add_one-b3235fea9a156f74)

running 1 test
test tests::it_works ... ok

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

   Doc-tests add_one

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you publish the crates in a workspace to &lt;a href="https://crates.io/" rel="noopener noreferrer"&gt;crates.io&lt;/a&gt;, each crate in the workspace must be published separately. As with &lt;code&gt;cargo test&lt;/code&gt;, we can use &lt;code&gt;-p&lt;/code&gt; to target a specific package in the workspace and specify the package name we want to publish.&lt;/p&gt;

</description>
      <category>rust</category>
      <category>programming</category>
      <category>learning</category>
    </item>
    <item>
      <title>[Rust Guide] 14.4. Publishing Crates Part 2</title>
      <dc:creator>SomeB1oody</dc:creator>
      <pubDate>Wed, 08 Jul 2026 03:32:51 +0000</pubDate>
      <link>https://dev.to/someb1oody/rust-guide-144-publishing-crates-part-2-232n</link>
      <guid>https://dev.to/someb1oody/rust-guide-144-publishing-crates-part-2-232n</guid>
      <description>&lt;p&gt;&lt;strong&gt;If you find this helpful, please like, bookmark, and follow. To keep learning along, follow this series.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  14.4.1 Create and Set Up a crates.io Account
&lt;/h2&gt;

&lt;p&gt;Before publishing any crate, you need to have a &lt;a href="https://crates.io/" rel="noopener noreferrer"&gt;crates.io&lt;/a&gt; account and obtain an API token. To do this, visit the &lt;a href="https://crates.io/" rel="noopener noreferrer"&gt;crates.io&lt;/a&gt; homepage and sign in with a GitHub account. Currently, only GitHub login is supported. If you are already signed in, open your account settings at &lt;a href="https://crates.io/me/" rel="noopener noreferrer"&gt;https://crates.io/me/&lt;/a&gt; and find the API key. Then use the &lt;code&gt;cargo login&lt;/code&gt; command locally and paste your API key when prompted:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ cargo login
just1a1nexample
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command tells Cargo your API token and stores it locally in &lt;code&gt;~/.cargo/credentials&lt;/code&gt;. Note that &lt;strong&gt;this token must not be shared with anyone else. If you leak it, you should revoke it and generate a new token on &lt;a href="https://crates.io/" rel="noopener noreferrer"&gt;crates.io&lt;/a&gt;.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  14.4.2 Add Metadata to the Crate
&lt;/h2&gt;

&lt;p&gt;Before publishing a crate, you also need to add some metadata to the &lt;code&gt;[package]&lt;/code&gt; section in &lt;code&gt;Cargo.toml&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;First, make sure the project name is unique on the website.&lt;/li&gt;
&lt;li&gt;Second, write a &lt;code&gt;description&lt;/code&gt;, which is a short introduction. It does not need to be long; one or two sentences is enough. The &lt;code&gt;description&lt;/code&gt; will appear in crate search results.&lt;/li&gt;
&lt;li&gt;You need to provide the license identifier value used by this crate (you can look it up at &lt;a href="https://spdx.org/licenses/" rel="noopener noreferrer"&gt;spdx.org/licenses/&lt;/a&gt;); you can specify multiple licenses, separated by &lt;code&gt;OR&lt;/code&gt;, in &lt;code&gt;license&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://semver.org/" rel="noopener noreferrer"&gt;Semantic version&lt;/a&gt; information goes in &lt;code&gt;version&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Author information goes in &lt;code&gt;authors&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Of course, you can provide more information than that; for details, see the &lt;a href="https://doc.rust-lang.org/cargo/" rel="noopener noreferrer"&gt;Cargo Book&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The full &lt;code&gt;[package]&lt;/code&gt; section should look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight toml"&gt;&lt;code&gt;&lt;span class="nn"&gt;[package]&lt;/span&gt;
&lt;span class="py"&gt;name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"guessing_game"&lt;/span&gt;
&lt;span class="py"&gt;version&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"0.1.0"&lt;/span&gt;
&lt;span class="py"&gt;edition&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"2021"&lt;/span&gt;
&lt;span class="py"&gt;description&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"A fun game where you guess what number the computer has chosen."&lt;/span&gt;
&lt;span class="py"&gt;license&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"MIT OR Apache-2.0"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  14.4.3 Publish a Crate with a Command
&lt;/h2&gt;

&lt;p&gt;You can publish a crate with the &lt;code&gt;cargo publish&lt;/code&gt; command, but only if the metadata is complete and the project name is unique.&lt;/p&gt;

&lt;p&gt;When &lt;code&gt;cargo publish&lt;/code&gt; runs successfully, it sends a verification email to the address registered to your &lt;a href="https://crates.io/" rel="noopener noreferrer"&gt;crates.io&lt;/a&gt; account; just confirm it in your inbox.&lt;/p&gt;

&lt;p&gt;If something goes wrong, &lt;code&gt;cargo publish&lt;/code&gt; reports an error:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ cargo publish
    Updating crates.io index
warning: manifest has no description, license, license-file, documentation, homepage or repository.
See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for more info.
......
error: failed to publish to registry at https://crates.io

Caused by:
  the remote server responded with an error: missing or empty metadata fields: description, license. Please see https://doc.rust-lang.org/cargo/reference/manifest.html for how to upload metadata
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I omitted some of the middle content. The &lt;code&gt;Caused by&lt;/code&gt; section says that the error was caused by missing metadata.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Once a crate is published, it is permanent: that version cannot be overwritten, and the code cannot be deleted.&lt;/strong&gt; This is so projects depending on that version can continue to work normally.&lt;/p&gt;

&lt;h2&gt;
  
  
  14.4.4 Publish a New Crate Version
&lt;/h2&gt;

&lt;p&gt;If you need to publish a newer version of an existing crate, modify the crate source code, update the &lt;code&gt;version&lt;/code&gt; value in &lt;code&gt;Cargo.toml&lt;/code&gt; according to semantic versioning, and then publish again.&lt;/p&gt;

&lt;h2&gt;
  
  
  14.4.5 Yank a Version
&lt;/h2&gt;

&lt;p&gt;Yanking a version prevents new projects from depending on that version, but projects that were already built against it can still use and download it.&lt;/p&gt;

&lt;p&gt;The command is &lt;code&gt;cargo yank --vers the-specified-version&lt;/code&gt;. For example, to yank version 1.0.1, write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;cargo yank &lt;span class="nt"&gt;--vers&lt;/span&gt; 1.0.1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you change your mind after yanking and want to undo it, write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;cargo yank &lt;span class="nt"&gt;--vers&lt;/span&gt; 1.0.1 &lt;span class="nt"&gt;--undo&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;yank&lt;/code&gt; means:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Projects that already have a generated &lt;code&gt;Cargo.lock&lt;/code&gt; will not be interrupted by the version being yanked.&lt;/li&gt;
&lt;li&gt;Future &lt;code&gt;Cargo.lock&lt;/code&gt; files will not use the yanked version.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>rust</category>
      <category>programming</category>
      <category>learning</category>
    </item>
    <item>
      <title>[Rust Guide] 14.3. Re-Exporting APIs with Pub Use</title>
      <dc:creator>SomeB1oody</dc:creator>
      <pubDate>Wed, 08 Jul 2026 03:30:52 +0000</pubDate>
      <link>https://dev.to/someb1oody/rust-guide-143-re-exporting-apis-with-pub-use-ii5</link>
      <guid>https://dev.to/someb1oody/rust-guide-143-re-exporting-apis-with-pub-use-ii5</guid>
      <description>&lt;p&gt;&lt;strong&gt;If you find this helpful, please like, bookmark, and follow. To keep learning along, follow this series.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  14.3.1 Re-Exporting APIs with &lt;code&gt;pub use&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;In Chapter 7, we introduced the &lt;code&gt;mod&lt;/code&gt; keyword. We use it to organize code into modules. The &lt;code&gt;pub&lt;/code&gt; keyword introduced there can make modules or methods public so that external code can call them. External code then uses the &lt;code&gt;use&lt;/code&gt; keyword to bring modules or methods into the current scope.&lt;/p&gt;

&lt;p&gt;Using these keywords lets us organize code in a developer-friendly way. However, this structure is not always very friendly for the end users of the codebase. For example, the structure of a crate may be very convenient for developers during development, but not very convenient for users. Developers often split the program into many layers, and users may find it difficult to locate a type hidden deep inside that structure. For example, &lt;code&gt;my_crate::some_module::another_module::UsefulType&lt;/code&gt; is cumbersome, while &lt;code&gt;my_crate::UsefulType&lt;/code&gt; is much easier to use.&lt;/p&gt;

&lt;p&gt;For this kind of problem, there is no need to reorganize the internal code structure. Instead, &lt;code&gt;pub use&lt;/code&gt; can be used to re-export items and create a public-facing structure different from the internal private structure. Re-exporting takes a public item from one location and makes it public at another location, as if it had been defined there originally.&lt;/p&gt;

&lt;p&gt;Here is an example:&lt;br&gt;
&lt;code&gt;lib.rs&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="cd"&gt;//! # Art&lt;/span&gt;
&lt;span class="cd"&gt;//!&lt;/span&gt;
&lt;span class="cd"&gt;//! A library for modeling artistic concepts.&lt;/span&gt;

&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;mod&lt;/span&gt; &lt;span class="n"&gt;kinds&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="cd"&gt;/// The primary colors according to the RYB color model.&lt;/span&gt;
    &lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;PrimaryColor&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Red&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;Yellow&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;Blue&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="cd"&gt;/// The secondary colors according to the RYB color model.&lt;/span&gt;
    &lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;SecondaryColor&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Orange&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;Green&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;Purple&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;mod&lt;/span&gt; &lt;span class="n"&gt;utils&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="k"&gt;crate&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;kinds&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="cd"&gt;/// Combines two primary colors in equal amounts to create&lt;/span&gt;
    &lt;span class="cd"&gt;/// a secondary color.&lt;/span&gt;
    &lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;mix&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;PrimaryColor&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;c2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;PrimaryColor&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;SecondaryColor&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;//...&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Under the &lt;code&gt;kinds&lt;/code&gt; module there are two enum types, &lt;code&gt;PrimaryColor&lt;/code&gt; and &lt;code&gt;SecondaryColor&lt;/code&gt;, used to store color variants.&lt;/li&gt;
&lt;li&gt;Under the &lt;code&gt;utils&lt;/code&gt; module there is a function called &lt;code&gt;mix&lt;/code&gt;. Its job is to mix two &lt;code&gt;PrimaryColor&lt;/code&gt; values into a &lt;code&gt;SecondaryColor&lt;/code&gt;. The code inside is not shown here.&lt;/li&gt;
&lt;li&gt;Putting the enum types under &lt;code&gt;kinds&lt;/code&gt; and the function under &lt;code&gt;utils&lt;/code&gt; is very developer-friendly.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;main.rs&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;art&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;kinds&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;PrimaryColor&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;art&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;utils&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;mix&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;red&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;PrimaryColor&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Red&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;yellow&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;PrimaryColor&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Yellow&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;mix&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;red&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;yellow&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This uses the enum types and the &lt;code&gt;mix&lt;/code&gt; function from &lt;code&gt;lib.rs&lt;/code&gt;. Because it requires three levels to bring them into scope, and because the enum type and function live in different modules, it is quite inconvenient for users.&lt;/p&gt;

&lt;p&gt;The generated crate documentation looks like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fufht0pmu4c89cfk43v13.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fufht0pmu4c89cfk43v13.png" alt=" " width="800" height="409"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If we refactor the code using re-exports:&lt;br&gt;
&lt;code&gt;lib.rs&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="cd"&gt;//! # Art&lt;/span&gt;
&lt;span class="cd"&gt;//!&lt;/span&gt;
&lt;span class="cd"&gt;//! A library for modeling artistic concepts.&lt;/span&gt;

&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;kinds&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;PrimaryColor&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;kinds&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;SecondaryColor&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;utils&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;mix&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;mod&lt;/span&gt; &lt;span class="n"&gt;kinds&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="cd"&gt;/// The primary colors according to the RYB color model.&lt;/span&gt;
    &lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;PrimaryColor&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Red&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;Yellow&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;Blue&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="cd"&gt;/// The secondary colors according to the RYB color model.&lt;/span&gt;
    &lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;SecondaryColor&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Orange&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;Green&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;Purple&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;mod&lt;/span&gt; &lt;span class="n"&gt;utils&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="k"&gt;crate&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;kinds&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="cd"&gt;/// Combines two primary colors in equal amounts to create&lt;/span&gt;
    &lt;span class="cd"&gt;/// a secondary color.&lt;/span&gt;
    &lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;mix&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;PrimaryColor&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;c2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;PrimaryColor&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;SecondaryColor&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;//...&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;main.rs&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;art&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;mix&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;art&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;PrimaryColor&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;red&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;PrimaryColor&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Red&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;yellow&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;PrimaryColor&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Yellow&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;mix&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;red&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;yellow&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At this point, calling the enum type and the function no longer requires writing module paths layer by layer.&lt;/p&gt;

&lt;p&gt;The generated crate documentation now looks like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fy760ve64th6w6leqqgvc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fy760ve64th6w6leqqgvc.png" alt=" " width="799" height="521"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The documentation includes a Re-exports section, and all re-exported items are listed there. For actual users of the crate, this makes it very convenient to find these types and functions.&lt;/p&gt;

</description>
      <category>rust</category>
      <category>programming</category>
      <category>learning</category>
    </item>
    <item>
      <title>[Rust Guide] 14.2. Documentation Comments and Publishing Crates</title>
      <dc:creator>SomeB1oody</dc:creator>
      <pubDate>Wed, 08 Jul 2026 03:27:13 +0000</pubDate>
      <link>https://dev.to/someb1oody/rust-guide-142-documentation-comments-and-publishing-crates-5425</link>
      <guid>https://dev.to/someb1oody/rust-guide-142-documentation-comments-and-publishing-crates-5425</guid>
      <description>&lt;p&gt;&lt;strong&gt;If you find this helpful, please like, bookmark, and follow. To keep learning along, follow this series.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  14.2.1 crates.io
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://crates.io/" rel="noopener noreferrer"&gt;crates.io&lt;/a&gt; is the official package management platform for the Rust programming language, similar to package managers in other languages such as npm for JavaScript and pip for Python. Its main uses include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Hosting Rust libraries (crates):&lt;/strong&gt; Developers can publish their own Rust libraries on crates.io, and other developers can download and use them.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Dependency management:&lt;/strong&gt; Cargo, Rust’s build tool and package manager, downloads required dependencies from crates.io to simplify project development.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Search and discovery:&lt;/strong&gt; Users can search for existing libraries on crates.io and find solutions that fit their project needs.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Versioning and updates:&lt;/strong&gt; &lt;a href="https://crates.io/" rel="noopener noreferrer"&gt;crates.io&lt;/a&gt; supports version management, so developers can upload new versions of libraries and users can update dependencies easily.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the guessing game from Chapter 1, we already used the third-party &lt;code&gt;rand&lt;/code&gt; crate from &lt;a href="https://crates.io/" rel="noopener noreferrer"&gt;crates.io&lt;/a&gt; to get random numbers. We can use crates provided by others, and we can also publish our own crates to &lt;a href="https://crates.io/" rel="noopener noreferrer"&gt;crates.io&lt;/a&gt; for others to use.&lt;/p&gt;

&lt;p&gt;Rust and Cargo include features that make your published packages easier for people to find and use. Next we will discuss some of those features and then explain how to publish a package.&lt;/p&gt;

&lt;h2&gt;
  
  
  14.2.2 Documentation Comments
&lt;/h2&gt;

&lt;p&gt;Use &lt;code&gt;///&lt;/code&gt; to write documentation comments. Documentation comments are used to generate project documentation. They are different from &lt;code&gt;//&lt;/code&gt;, which is used for code comments. Documentation comments are annotations for the whole project.&lt;/p&gt;

&lt;p&gt;This documentation is HTML documentation and supports Markdown. It displays documentation comments for public APIs and usually explains how readers should use the API.&lt;/p&gt;

&lt;p&gt;Documentation comments are usually placed directly before the item they describe.&lt;/p&gt;

&lt;p&gt;Here is an example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fde44bwa8fsuzl1iwpwhx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fde44bwa8fsuzl1iwpwhx.png" alt=" " width="796" height="566"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;PS: In Markdown, &lt;code&gt;#&lt;/code&gt; marks a heading.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  14.2.3 Commands for Generating HTML Documentation
&lt;/h2&gt;

&lt;p&gt;Running &lt;code&gt;cargo doc&lt;/code&gt; in the terminal uses the &lt;code&gt;rustdoc&lt;/code&gt; tool, which comes with Rust, to generate documentation. The generated documentation is placed in the &lt;code&gt;target/doc&lt;/code&gt; directory.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;cargo doc --open&lt;/code&gt; generates the documentation and opens the result in a web browser:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fqtxbqt9bx9e1abgoy3mi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fqtxbqt9bx9e1abgoy3mi.png" alt=" " width="800" height="462"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  14.2.4 Common Sections
&lt;/h2&gt;

&lt;p&gt;Here are some sections that crate authors often use in their documentation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;# Example&lt;/code&gt; is the examples section, where sample code is placed in the code block.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Panic&lt;/code&gt;: The function being documented may panic. Callers who do not want the program to panic should ensure that the function is not called in those cases.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Error&lt;/code&gt;: If the function returns a &lt;code&gt;Result&lt;/code&gt;, then describing the possible error types and the conditions that may cause those errors is helpful to callers, so they can write code that handles different errors in different ways.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Safety&lt;/code&gt;: If the function calls &lt;code&gt;unsafe&lt;/code&gt; (which we will cover later), there should be a section explaining why the function is unsafe and covering the invariants the caller is expected to uphold.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  14.2.5 Documentation Comments as Tests
&lt;/h2&gt;

&lt;p&gt;Code blocks in documentation comments are executed as tests when you run &lt;code&gt;cargo test&lt;/code&gt;. You will see this part in the test results:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   Doc-tests my_crate

running 1 test
test src/lib.rs - add_one (line 5) ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.27s
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  14.2.6 Adding Documentation Comments for Items with Outer Comments
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;//!&lt;/code&gt; adds documentation to the outer item, rather than to the item that follows the comment. We usually use these documentation comments in the crate root file, conventionally &lt;code&gt;src/lib.rs&lt;/code&gt;, or inside a module to document a crate or an entire module:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fd2wkywozfijcltysevfj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fd2wkywozfijcltysevfj.png" alt=" " width="799" height="498"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this example, we added documentation describing what &lt;code&gt;my_crate&lt;/code&gt; does, that is, comments on the outer item of &lt;code&gt;add_one&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The HTML documentation changes accordingly as well:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F1az85gewtrbuu9pq89wi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F1az85gewtrbuu9pq89wi.png" alt=" " width="800" height="367"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>rust</category>
      <category>programming</category>
      <category>learning</category>
    </item>
    <item>
      <title>[Rust Guide] 14.1. Cargo Publishing Configuration</title>
      <dc:creator>SomeB1oody</dc:creator>
      <pubDate>Wed, 08 Jul 2026 03:10:53 +0000</pubDate>
      <link>https://dev.to/someb1oody/rust-guide-141-cargo-publishing-configuration-4oi1</link>
      <guid>https://dev.to/someb1oody/rust-guide-141-cargo-publishing-configuration-4oi1</guid>
      <description>&lt;p&gt;&lt;strong&gt;If you find this helpful, please like, bookmark, and follow. To keep learning along, follow this series.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  14.1.1 Release Profile
&lt;/h2&gt;

&lt;p&gt;A release profile is a publishing configuration, meaning it is a set of pre-defined configuration options. It is also customizable. We can define our own configurations and use different settings, giving programmers more control over how code is compiled.&lt;/p&gt;

&lt;p&gt;Each profile is its own configuration preset and is independent from the others.&lt;/p&gt;

&lt;p&gt;Cargo mainly has two profiles:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;dev profile&lt;/code&gt;: used for development and &lt;code&gt;cargo build&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;release profile&lt;/code&gt;: used for publishing and &lt;code&gt;cargo build --release&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Using &lt;code&gt;cargo build&lt;/code&gt; and &lt;code&gt;cargo build --release&lt;/code&gt; will apply two different configuration profiles:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ cargo build
    Finished dev [unoptimized + debuginfo] target(s) in 0.0s
$ cargo build --release
    Finished release [optimized] target(s) in 0.0s
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  14.1.2 Custom Profiles
&lt;/h2&gt;

&lt;p&gt;Cargo provides default settings for each profile.&lt;/p&gt;

&lt;p&gt;If you want to customize the configuration, whether for &lt;code&gt;dev profile&lt;/code&gt; or &lt;code&gt;release profile&lt;/code&gt;, you can add a &lt;code&gt;[profile.xxxx]&lt;/code&gt; section to &lt;code&gt;Cargo.toml&lt;/code&gt; and override a subset of the default settings. Usually we do not override every option; we only override the ones we want to change.&lt;/p&gt;

&lt;p&gt;Here is an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight toml"&gt;&lt;code&gt;&lt;span class="nn"&gt;[profile.dev]&lt;/span&gt;
&lt;span class="py"&gt;opt-level&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;

&lt;span class="nn"&gt;[profile.release]&lt;/span&gt;
&lt;span class="py"&gt;opt-level&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;opt-level&lt;/code&gt; setting controls how much optimization Rust applies to your code, with values ranging from 0 to 3. Applying more optimizations increases compile time, so if you are developing and compiling code frequently, you may want fewer optimizations even if the generated code runs more slowly, because that speeds up compilation. That is why the default &lt;code&gt;opt-level&lt;/code&gt; for &lt;code&gt;dev&lt;/code&gt; is &lt;code&gt;0&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;When you are ready to publish code, it is better to spend more time compiling. Code is compiled only once in release mode, but the compiled program runs many times, so release mode trades longer compile time for faster code. That is why the default &lt;code&gt;opt-level&lt;/code&gt; for &lt;code&gt;release&lt;/code&gt; is &lt;code&gt;3&lt;/code&gt;.&lt;/p&gt;

</description>
      <category>rust</category>
      <category>programming</category>
      <category>learning</category>
    </item>
    <item>
      <title>[Rust Guide] 13.10. Performance Comparison - Loops vs. Iterators</title>
      <dc:creator>SomeB1oody</dc:creator>
      <pubDate>Mon, 06 Jul 2026 04:25:01 +0000</pubDate>
      <link>https://dev.to/someb1oody/rust-guide-1310-performance-comparison-loops-vs-iterators-53nd</link>
      <guid>https://dev.to/someb1oody/rust-guide-1310-performance-comparison-loops-vs-iterators-53nd</guid>
      <description>&lt;h2&gt;
  
  
  13.10.0 Before We Begin
&lt;/h2&gt;

&lt;p&gt;During its design, Rust drew inspiration from many languages, and functional programming had a particularly strong influence on Rust. Functional programming often includes passing functions as values to parameters, returning them from other functions, assigning them to variables for later execution, and so on.&lt;/p&gt;

&lt;p&gt;In this chapter, we will discuss some Rust features that are similar to what many languages call functional features:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Closures&lt;/li&gt;
&lt;li&gt;Iterators&lt;/li&gt;
&lt;li&gt;Improving the I/O Project with Closures and Iterators&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Performance of Closures and Iterators (this article)&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;If you find this helpful, please like, bookmark, and follow. To keep learning along, follow this series.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  13.10.1 A Test
&lt;/h2&gt;

&lt;p&gt;To run a benchmark test, load all of &lt;em&gt;The Adventures of Sherlock Holmes&lt;/em&gt; by Sir Arthur Conan Doyle into a &lt;code&gt;String&lt;/code&gt; and search the contents for the word &lt;code&gt;the&lt;/code&gt;. Here are the benchmark results for the &lt;code&gt;search&lt;/code&gt; version that uses a &lt;code&gt;for&lt;/code&gt; loop and the version that uses iterators:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="go"&gt;test bench_search_for  ... bench:  19,620,300 ns/iter (+/- 915,700)
test bench_search_iter ... bench:  19,234,900 ns/iter (+/- 657,200)
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;The iterator version is slightly faster!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We will not explain the benchmark code here, because the point is not to prove that the two versions are equivalent. The point is to get a rough idea of how these two implementations compare in performance.&lt;/p&gt;

&lt;p&gt;Iterators are a high-level abstraction in Rust, yet the code they generate after compilation is almost the same as the low-level code we would write by hand. &lt;strong&gt;This is called a zero-cost abstraction.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  13.10.2 Zero-Cost Abstraction
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Zero-cost abstraction means that using an abstraction does not introduce extra runtime overhead.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Rust iterators can achieve zero-cost abstraction because:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Generics and Monomorphization&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Rust iterators make heavy use of generics to define operations, such as the &lt;code&gt;Iterator&lt;/code&gt; trait. During compilation, the compiler instantiates generic code for each concrete type and generates efficient machine code specialized for those types. This process is called &lt;strong&gt;monomorphization&lt;/strong&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Static dispatch&lt;/strong&gt;: The compiler generates code that calls functions directly for concrete types, so there is no need to look up function addresses at runtime, unlike dynamic dispatch through a virtual table.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Optimization opportunities&lt;/strong&gt;: Because the types are known at compile time, the compiler can deeply optimize the code, such as eliminating function-call overhead and inlining.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;2. Inlining and LLVM Optimization&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Rust uses LLVM as its backend compiler. The compiler can optimize iterator chains in the following ways:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Function inlining&lt;/strong&gt;: The Rust compiler inlines operations inside iterators, such as &lt;code&gt;map&lt;/code&gt; and &lt;code&gt;filter&lt;/code&gt;, expanding them into compact code without function-call overhead.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Loop unrolling and merging&lt;/strong&gt;: Multiple iterator method calls, such as &lt;code&gt;map().filter().collect()&lt;/code&gt;, can be merged into a single loop at compile time.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Redundancy elimination&lt;/strong&gt;: For example, the compiler can directly remove some unnecessary intermediate variables or operations.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The result is that the final code executed by an iterator chain is almost as efficient as a hand-written loop.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Lazy Evaluation&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Rust iterators are lazy, which means:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Before a terminal method is called, such as &lt;code&gt;collect()&lt;/code&gt; or &lt;code&gt;for_each()&lt;/code&gt;, the iterator does not perform any actual work.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Each intermediate operation, such as &lt;code&gt;map&lt;/code&gt; and &lt;code&gt;filter&lt;/code&gt;, only creates a new iterator and does not apply the operation immediately.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This lazy design allows the compiler to generate code optimized for the final use case when the iterator is actually used, without introducing unnecessary intermediate data structures or calculations.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. No Runtime Overhead&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;One of Rust’s design principles is to avoid runtime costs. Iterator implementations avoid dynamic allocation and runtime polymorphism:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Rust iterators are based on &lt;strong&gt;static types&lt;/strong&gt;, so they usually do not require heap allocation, unless you explicitly use &lt;code&gt;Box&lt;/code&gt; or &lt;code&gt;dyn Iterator&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &lt;code&gt;Iterator&lt;/code&gt; trait uses static dispatch, which avoids dynamic dispatch. Even when dynamic dispatch is needed, you must explicitly declare &lt;code&gt;dyn Iterator&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;5. No Extra Abstraction Cost&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Rust iterators provide functionality by operating directly on the underlying data structures, without introducing extra abstraction layers. For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;An iterator created by calling &lt;code&gt;.iter()&lt;/code&gt; operates directly on the underlying slice or collection, so the overhead is very low.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Intermediate iterators, such as &lt;code&gt;Map&lt;/code&gt; and &lt;code&gt;Filter&lt;/code&gt;, are optimized at compile time into a compact set of instructions instead of introducing unnecessary wrapping.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  13.10.3 An Example: An Audio Decoder
&lt;/h2&gt;

&lt;p&gt;The following code comes from an audio decoder. The decoding algorithm uses linear prediction math to estimate future values from a linear function of previous samples. This code uses an iterator chain to perform several mathematical operations on three variables in a range: a &lt;code&gt;buffer&lt;/code&gt; slice of data, an array of 12 &lt;code&gt;coefficients&lt;/code&gt;, and the amount of data shifting in &lt;code&gt;qlp_shift&lt;/code&gt;. We declare the variables in this example but do not assign values to them. Although this code does not mean much outside its original context, it is still a concise, real-world example of how Rust turns high-level ideas into low-level code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;coefficients&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;i64&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;qlp_shift&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i16&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="nf"&gt;.len&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;prediction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;coefficients&lt;/span&gt;&lt;span class="nf"&gt;.iter&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
                                 &lt;span class="nf"&gt;.zip&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
                                 &lt;span class="nf"&gt;.map&lt;/span&gt;&lt;span class="p"&gt;(|(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)|&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nb"&gt;i64&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                                 &lt;span class="py"&gt;.sum&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;i64&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;qlp_shift&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;delta&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;prediction&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;delta&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To compute the &lt;code&gt;prediction&lt;/code&gt; value, this code iterates over each of the 12 values in &lt;code&gt;coefficients&lt;/code&gt; and uses &lt;code&gt;zip&lt;/code&gt; to pair each coefficient with the previous 12 values in &lt;code&gt;buffer&lt;/code&gt;. Then, for each pair, it multiplies the values, sums all the results, and shifts the total to the right by &lt;code&gt;qlp_shift&lt;/code&gt; bits.&lt;/p&gt;

&lt;p&gt;All coefficients are stored in registers, which means accessing these values is very fast. There are no bounds checks on array access at runtime. All of these optimizations that Rust can apply make the generated code extremely efficient. Now that you know this, you can use iterators and closures without fear! &lt;strong&gt;They make the code look higher level without causing runtime performance loss.&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>rust</category>
      <category>programming</category>
      <category>learning</category>
    </item>
    <item>
      <title>[Rust Guide] 13.9. Improving the I/O Project With Closures and Iterators</title>
      <dc:creator>SomeB1oody</dc:creator>
      <pubDate>Mon, 06 Jul 2026 04:18:02 +0000</pubDate>
      <link>https://dev.to/someb1oody/rust-guide-139-improving-the-io-project-with-closures-and-iterators-50gk</link>
      <guid>https://dev.to/someb1oody/rust-guide-139-improving-the-io-project-with-closures-and-iterators-50gk</guid>
      <description>&lt;h2&gt;
  
  
  13.9.0 Before We Begin
&lt;/h2&gt;

&lt;p&gt;During its design, Rust drew inspiration from many languages, and functional programming had a particularly strong influence on Rust. Functional programming often includes passing functions as values to parameters, returning them from other functions, assigning them to variables for later execution, and so on.&lt;/p&gt;

&lt;p&gt;In this chapter, we will discuss some Rust features that are similar to what many languages call functional features:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Closures&lt;/li&gt;
&lt;li&gt;Iterators&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Improving the I/O Project with Closures and Iterators (this article)&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Performance of Closures and Iterators&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;If you find this helpful, please like, bookmark, and follow. To keep learning along, follow this series.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  13.9.1 Review
&lt;/h2&gt;

&lt;p&gt;This article uses the &lt;code&gt;grep&lt;/code&gt; project from Chapter 12 as an example to show how closures and iterators can improve an I/O project, so let’s review it first.&lt;/p&gt;

&lt;p&gt;Chapter 12 builds a practical project: a command-line program. This program is a &lt;code&gt;grep&lt;/code&gt; (&lt;strong&gt;Global Regular Expression Print&lt;/strong&gt;) tool, a global regular-expression search and output utility. Its job is to &lt;strong&gt;search for specified text in a specified file.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The project is split into these steps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Accept command-line arguments&lt;/li&gt;
&lt;li&gt;Read a file&lt;/li&gt;
&lt;li&gt;Refactor to improve modules and error handling&lt;/li&gt;
&lt;li&gt;Develop library functionality with TDD (test-driven development)&lt;/li&gt;
&lt;li&gt;Use environment variables&lt;/li&gt;
&lt;li&gt;Write error messages to standard error instead of standard output&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;lib.rs&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;error&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  
&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  

&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;Config&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
    &lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  
    &lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="n"&gt;filename&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  
    &lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="n"&gt;case_sensitive&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;bool&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  
&lt;span class="p"&gt;}&lt;/span&gt;  

&lt;span class="k"&gt;impl&lt;/span&gt; &lt;span class="n"&gt;Config&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
    &lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Config&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;'static&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="nf"&gt;.len&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;Err&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Not enough arguments"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  
        &lt;span class="p"&gt;}&lt;/span&gt;  
        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;query&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="nf"&gt;.clone&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;  
        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;filename&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="nf"&gt;.clone&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;  
        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;case_sensitive&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;env&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;var&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"CASE_INSENSITIVE"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.is_err&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;  
        &lt;span class="nf"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Config&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;filename&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;case_sensitive&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;  
    &lt;span class="p"&gt;}&lt;/span&gt;  
&lt;span class="p"&gt;}&lt;/span&gt;  

&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Config&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="nb"&gt;Box&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;dyn&lt;/span&gt; &lt;span class="n"&gt;Error&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;contents&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;read_to_string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="py"&gt;.filename&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;results&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="py"&gt;.case_sensitive&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
        &lt;span class="nf"&gt;search&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="py"&gt;.query&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;contents&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
        &lt;span class="nf"&gt;search_case_insensitive&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="py"&gt;.query&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;contents&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
    &lt;span class="p"&gt;};&lt;/span&gt;  
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;results&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
        &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"{}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  
    &lt;span class="p"&gt;}&lt;/span&gt;  
    &lt;span class="nf"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(())&lt;/span&gt;  
&lt;span class="p"&gt;}&lt;/span&gt;  

&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="n"&gt;search&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nv"&gt;'a&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;contents&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nv"&gt;'a&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Vec&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;amp;&lt;/span&gt;&lt;span class="nv"&gt;'a&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;results&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;Vec&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;  
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;contents&lt;/span&gt;&lt;span class="nf"&gt;.lines&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="nf"&gt;.contains&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
            &lt;span class="n"&gt;results&lt;/span&gt;&lt;span class="nf"&gt;.push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  
        &lt;span class="p"&gt;}&lt;/span&gt;  
    &lt;span class="p"&gt;}&lt;/span&gt;  
    &lt;span class="n"&gt;results&lt;/span&gt;  
&lt;span class="p"&gt;}&lt;/span&gt;  

&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="n"&gt;search_case_insensitive&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nv"&gt;'a&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;contents&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nv"&gt;'a&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Vec&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;amp;&lt;/span&gt;&lt;span class="nv"&gt;'a&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;results&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;Vec&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;  
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;query&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="nf"&gt;.to_lowercase&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;  
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;contents&lt;/span&gt;&lt;span class="nf"&gt;.to_lowercase&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.lines&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="nf"&gt;.contains&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
            &lt;span class="n"&gt;results&lt;/span&gt;&lt;span class="nf"&gt;.push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  
        &lt;span class="p"&gt;}&lt;/span&gt;  
    &lt;span class="p"&gt;}&lt;/span&gt;  
    &lt;span class="n"&gt;results&lt;/span&gt;  
&lt;span class="p"&gt;}&lt;/span&gt;  

&lt;span class="nd"&gt;#[cfg(test)]&lt;/span&gt;  
&lt;span class="k"&gt;mod&lt;/span&gt; &lt;span class="n"&gt;tests&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
    &lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  

    &lt;span class="nd"&gt;#[test]&lt;/span&gt;  
    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;case_sensitive&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;query&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"duct"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  
        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;contents&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="s"&gt;  
Rust:  
safe, fast, productive.  
Pick three.  
Duct tape."&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  

        &lt;span class="nd"&gt;assert_eq!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;vec!&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"safe, fast, productive."&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nf"&gt;search&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;contents&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;  
    &lt;span class="p"&gt;}&lt;/span&gt;  

    &lt;span class="nd"&gt;#[test]&lt;/span&gt;  
    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;case_insensitive&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;query&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"rUsT"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  
        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;contents&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="s"&gt;  
Rust:  
safe, fast, productive.  
Pick three.  
Trust me."&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  

        &lt;span class="nd"&gt;assert_eq!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;  
            &lt;span class="nd"&gt;vec!&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"Rust:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Trust me."&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;  
            &lt;span class="nf"&gt;search_case_insensitive&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;contents&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
        &lt;span class="p"&gt;);&lt;/span&gt;  
    &lt;span class="p"&gt;}&lt;/span&gt;  
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;main.rs&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  
&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;process&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  
&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;minigrep&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Config&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nb"&gt;Vec&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;env&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;args&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.collect&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;  
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;config&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;Config&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.unwrap_or_else&lt;/span&gt;&lt;span class="p"&gt;(|&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
        &lt;span class="nd"&gt;eprintln!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Problem parsing arguments: {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  
        &lt;span class="nn"&gt;process&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;exit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  
    &lt;span class="p"&gt;});&lt;/span&gt;  
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nf"&gt;Err&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;minigrep&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
        &lt;span class="nd"&gt;eprintln!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Application error: {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  
        &lt;span class="nn"&gt;process&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;exit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  
    &lt;span class="p"&gt;}&lt;/span&gt;  
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  13.9.2 Improving the &lt;code&gt;new&lt;/code&gt; Function
&lt;/h2&gt;

&lt;p&gt;Take a look at the &lt;code&gt;new&lt;/code&gt; function in &lt;code&gt;lib.rs&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;impl&lt;/span&gt; &lt;span class="n"&gt;Config&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
    &lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Config&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;'static&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="nf"&gt;.len&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;Err&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Not enough arguments"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  
        &lt;span class="p"&gt;}&lt;/span&gt;  
        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;query&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="nf"&gt;.clone&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;  
        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;filename&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="nf"&gt;.clone&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;  
        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;case_sensitive&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;env&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;var&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"CASE_INSENSITIVE"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.is_err&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;  
        &lt;span class="nf"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Config&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;filename&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;case_sensitive&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;  
    &lt;span class="p"&gt;}&lt;/span&gt;  
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These two lines:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;query&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="nf"&gt;.clone&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;  
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;filename&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="nf"&gt;.clone&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;use cloning. That is because the argument passed in is &lt;code&gt;&amp;amp;[String]&lt;/code&gt;, which does not have ownership, but the &lt;code&gt;Config&lt;/code&gt; struct needs to own the data. Only cloning lets &lt;code&gt;Config&lt;/code&gt; own &lt;code&gt;query&lt;/code&gt; and &lt;code&gt;filename&lt;/code&gt;, even though cloning adds performance overhead.&lt;/p&gt;

&lt;p&gt;After learning iterators, we can pass an iterator directly into &lt;code&gt;new&lt;/code&gt; so that it can take ownership. We can also use the iterator to handle length checks and indexing, which makes the scope of &lt;code&gt;new&lt;/code&gt;’s responsibility clearer.&lt;/p&gt;

&lt;p&gt;Before changing &lt;code&gt;new&lt;/code&gt;, we need to change how &lt;code&gt;main&lt;/code&gt; handles input arguments. Originally it was:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nb"&gt;Vec&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;env&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;args&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.collect&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;  
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;config&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;Config&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.unwrap_or_else&lt;/span&gt;&lt;span class="p"&gt;(|&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
    &lt;span class="nd"&gt;eprintln!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Problem parsing arguments: {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  
    &lt;span class="nn"&gt;process&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;exit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  
&lt;span class="p"&gt;});&lt;/span&gt;  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we remove &lt;code&gt;collect&lt;/code&gt; and pass the arguments from &lt;code&gt;env::args()&lt;/code&gt; directly to &lt;code&gt;new&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;config&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;Config&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nn"&gt;env&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;args&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;&lt;span class="nf"&gt;.unwrap_or_else&lt;/span&gt;&lt;span class="p"&gt;(|&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
    &lt;span class="nd"&gt;eprintln!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Problem parsing arguments: {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  
    &lt;span class="nn"&gt;process&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;exit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  
&lt;span class="p"&gt;});&lt;/span&gt;  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The return type of &lt;code&gt;env::args()&lt;/code&gt; is &lt;code&gt;std::env::Args&lt;/code&gt;, which implements the &lt;code&gt;Iterator&lt;/code&gt; trait, so it is an iterator.&lt;/p&gt;

&lt;p&gt;Now let’s modify &lt;code&gt;new&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;impl&lt;/span&gt; &lt;span class="n"&gt;Config&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
    &lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;env&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Config&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;'static&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="nf"&gt;.len&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;Err&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Not enough arguments"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  
        &lt;span class="p"&gt;}&lt;/span&gt;  
        &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="nf"&gt;.next&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;  
        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;query&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="nf"&gt;.next&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.unwrap&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;  
        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;filename&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="nf"&gt;.next&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.unwrap&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;  
        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;case_sensitive&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;env&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;var&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"CASE_INSENSITIVE"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.is_err&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;  
        &lt;span class="nf"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Config&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;filename&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;case_sensitive&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;  
    &lt;span class="p"&gt;}&lt;/span&gt;  
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The parameter &lt;code&gt;args&lt;/code&gt; is changed to &lt;code&gt;std::env::Args&lt;/code&gt;, and it must also be declared mutable with &lt;code&gt;mut&lt;/code&gt; because &lt;code&gt;next&lt;/code&gt; is a consuming iterator method.&lt;/li&gt;
&lt;li&gt;The line that contains only &lt;code&gt;args.next();&lt;/code&gt; is there because the first value returned by &lt;code&gt;env::args()&lt;/code&gt; is the program name, not an argument. Calling &lt;code&gt;args.next();&lt;/code&gt; skips that value.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;query&lt;/code&gt; and &lt;code&gt;filename&lt;/code&gt; are then obtained in order by calling &lt;code&gt;next&lt;/code&gt;. At that point, &lt;code&gt;query&lt;/code&gt; and &lt;code&gt;filename&lt;/code&gt; are owned &lt;code&gt;String&lt;/code&gt; values. Since &lt;code&gt;next&lt;/code&gt; returns an &lt;code&gt;Option&lt;/code&gt;, we can use &lt;code&gt;unwrap&lt;/code&gt; to extract the value.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  13.9.3 Improving the &lt;code&gt;search&lt;/code&gt; Function
&lt;/h2&gt;

&lt;p&gt;The current &lt;code&gt;search&lt;/code&gt; function looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="n"&gt;search&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nv"&gt;'a&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;contents&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nv"&gt;'a&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Vec&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;amp;&lt;/span&gt;&lt;span class="nv"&gt;'a&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;results&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;Vec&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;  
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;contents&lt;/span&gt;&lt;span class="nf"&gt;.lines&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="nf"&gt;.contains&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
            &lt;span class="n"&gt;results&lt;/span&gt;&lt;span class="nf"&gt;.push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  
        &lt;span class="p"&gt;}&lt;/span&gt;  
    &lt;span class="p"&gt;}&lt;/span&gt;  
    &lt;span class="n"&gt;results&lt;/span&gt;  
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;contents.lines()&lt;/code&gt; also returns an iterator. Here we manually check whether each line contains the keyword stored in &lt;code&gt;query&lt;/code&gt;, and if it does, we push that line into the &lt;code&gt;Vector&lt;/code&gt; and finally return the &lt;code&gt;Vector&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For finding items that satisfy a condition in an iterator and building a new iterator, we can use &lt;code&gt;filter&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="n"&gt;search&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nv"&gt;'a&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;contents&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nv"&gt;'a&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Vec&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;amp;&lt;/span&gt;&lt;span class="nv"&gt;'a&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
    &lt;span class="n"&gt;contents&lt;/span&gt;&lt;span class="nf"&gt;.lines&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.filter&lt;/span&gt;&lt;span class="p"&gt;(|&lt;/span&gt;&lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="nf"&gt;.contains&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="nf"&gt;.collect&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;  
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using &lt;code&gt;contains&lt;/code&gt; inside the closure implements the same logic.&lt;/p&gt;

&lt;p&gt;Since the ordinary search function can use iterators, the case-insensitive search function can use them too:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="n"&gt;search_case_insensitive&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nv"&gt;'a&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;contents&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nv"&gt;'a&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Vec&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;amp;&lt;/span&gt;&lt;span class="nv"&gt;'a&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
    &lt;span class="n"&gt;contents&lt;/span&gt;&lt;span class="nf"&gt;.to_lowercase&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;  
        &lt;span class="nf"&gt;.lines&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;  
        &lt;span class="nf"&gt;.filter&lt;/span&gt;&lt;span class="p"&gt;(|&lt;/span&gt;&lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="nf"&gt;.contains&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="nf"&gt;.to_lowercase&lt;/span&gt;&lt;span class="p"&gt;()))&lt;/span&gt;  
        &lt;span class="nf"&gt;.collect&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;  
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that &lt;code&gt;query.to_lowercase()&lt;/code&gt; needs &lt;code&gt;&amp;amp;&lt;/code&gt; because &lt;code&gt;query.to_lowercase()&lt;/code&gt; produces a &lt;code&gt;String&lt;/code&gt;, while &lt;code&gt;contains&lt;/code&gt; expects &lt;code&gt;&amp;amp;str&lt;/code&gt;. So you cannot pass &lt;code&gt;query.to_lowercase()&lt;/code&gt; directly; you must pass a reference, that is, &lt;code&gt;&amp;amp;query.to_lowercase()&lt;/code&gt;, for it to work correctly.&lt;/p&gt;

&lt;p&gt;To convert to &lt;code&gt;&amp;amp;str&lt;/code&gt;, you can use &lt;code&gt;&amp;amp;&lt;/code&gt;, of course, but you can also use &lt;code&gt;as_str&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="n"&gt;search_case_insensitive&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nv"&gt;'a&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;contents&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nv"&gt;'a&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Vec&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;amp;&lt;/span&gt;&lt;span class="nv"&gt;'a&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
    &lt;span class="n"&gt;contents&lt;/span&gt;&lt;span class="nf"&gt;.to_lowercase&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;  
        &lt;span class="nf"&gt;.lines&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;  
        &lt;span class="nf"&gt;.filter&lt;/span&gt;&lt;span class="p"&gt;(|&lt;/span&gt;&lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="nf"&gt;.contains&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="nf"&gt;.to_lowercase&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.as_str&lt;/span&gt;&lt;span class="p"&gt;()))&lt;/span&gt;  
        &lt;span class="nf"&gt;.collect&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;  
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In terms of both code volume and readability, using &lt;code&gt;filter&lt;/code&gt; is better. In addition, &lt;code&gt;filter&lt;/code&gt; reduces temporary variables. Eliminating mutable state (&lt;code&gt;let mut results = Vec::new();&lt;/code&gt;) also makes it possible to improve search performance through parallelization in the future, because we no longer need to worry about concurrent access safety for &lt;code&gt;results&lt;/code&gt;.&lt;/p&gt;

</description>
      <category>rust</category>
      <category>programming</category>
      <category>learning</category>
    </item>
    <item>
      <title>[Rust Guide] 13.8. Creating Custom Iterators</title>
      <dc:creator>SomeB1oody</dc:creator>
      <pubDate>Mon, 06 Jul 2026 04:06:44 +0000</pubDate>
      <link>https://dev.to/someb1oody/rust-guide-138-creating-custom-iterators-10h3</link>
      <guid>https://dev.to/someb1oody/rust-guide-138-creating-custom-iterators-10h3</guid>
      <description>&lt;h2&gt;
  
  
  13.8.0 Before We Begin
&lt;/h2&gt;

&lt;p&gt;During its design, Rust drew inspiration from many languages, and functional programming had a particularly strong influence on Rust. Functional programming often includes passing functions as values to parameters, returning them from other functions, assigning them to variables for later execution, and so on.&lt;/p&gt;

&lt;p&gt;In this chapter, we will discuss some Rust features that are similar to what many languages call functional features:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Closures&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Iterators (this article)&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Improving the I/O Project with Closures and Iterators&lt;/li&gt;
&lt;li&gt;Performance of Closures and Iterators&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;If you find this helpful, please like, bookmark, and follow. To keep learning along, follow this series.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  13.8.1 Creating a Custom Iterator With the &lt;code&gt;Iterator&lt;/code&gt; Trait
&lt;/h2&gt;

&lt;p&gt;The main step is just one: provide an implementation of &lt;code&gt;next&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Take an example:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Build an iterator that traverses from 1 to 5&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;Counter&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
    &lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;u32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  
&lt;span class="p"&gt;}&lt;/span&gt;  

&lt;span class="k"&gt;impl&lt;/span&gt; &lt;span class="n"&gt;Counter&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Counter&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
        &lt;span class="n"&gt;Counter&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;  
    &lt;span class="p"&gt;}&lt;/span&gt;  
&lt;span class="p"&gt;}&lt;/span&gt;  

&lt;span class="k"&gt;impl&lt;/span&gt; &lt;span class="nb"&gt;Iterator&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;Counter&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
    &lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;Item&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;u32&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  
    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Option&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;u32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="py"&gt;.count&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
            &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="py"&gt;.count&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  
            &lt;span class="nf"&gt;Some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="py"&gt;.count&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
            &lt;span class="nb"&gt;None&lt;/span&gt;  
        &lt;span class="p"&gt;}&lt;/span&gt;  
    &lt;span class="p"&gt;}&lt;/span&gt;  
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;First create a struct called &lt;code&gt;Counter&lt;/code&gt;. It has a &lt;code&gt;count&lt;/code&gt; field used to store the value needed during iteration, that is, the iterator’s state. The &lt;code&gt;count&lt;/code&gt; field is private rather than &lt;code&gt;pub&lt;/code&gt; so that the &lt;code&gt;Counter&lt;/code&gt; struct manages its own value independently.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Then write an associated function &lt;code&gt;new&lt;/code&gt; on the struct to create a new instance and make sure the new instance starts from 0.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Next, implement the &lt;code&gt;Iterator&lt;/code&gt; trait for &lt;code&gt;Counter&lt;/code&gt;. The &lt;code&gt;Iterator&lt;/code&gt; trait has an associated type &lt;code&gt;type Item&lt;/code&gt; and a &lt;code&gt;next&lt;/code&gt; method. First set the associated type to &lt;code&gt;u32&lt;/code&gt;, which means writing &lt;code&gt;type Item = u32;&lt;/code&gt;. This syntax will be covered in detail in Chapter 19; for now, just know that this iterator returns &lt;code&gt;u32&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The return type of &lt;code&gt;next&lt;/code&gt; is &lt;code&gt;Option&amp;lt;Self::Item&amp;gt;&lt;/code&gt;. Since the associated type above is &lt;code&gt;u32&lt;/code&gt;, you can think of it as &lt;code&gt;Option&amp;lt;u32&amp;gt;&lt;/code&gt;. When the &lt;code&gt;count&lt;/code&gt; field is less than 5, it keeps increasing by 1; when it is 5 or greater, it returns &lt;code&gt;None&lt;/code&gt;. That guarantees iteration from 1 to 5.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now let’s implement something more complex:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Use a &lt;code&gt;Counter&lt;/code&gt; struct in two forms: one from 1 to 5 and another from 2 to 5. Multiply each pair of elements from the two iterators, keep only the elements in the new iterator that are divisible by 3, and then return the sum of those elements&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;using_other_iterator_traits_methods&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;u32&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;Counter&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;  
        &lt;span class="nf"&gt;.zip&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nn"&gt;Counter&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.skip&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;  
        &lt;span class="nf"&gt;.map&lt;/span&gt;&lt;span class="p"&gt;(|(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)|&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
        &lt;span class="nf"&gt;.filter&lt;/span&gt;&lt;span class="p"&gt;(|&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
        &lt;span class="nf"&gt;.sum&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;  
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;I wrote it on separate lines because the chained call would be too long on one line. If the chain is not long, there is no need to split it across lines.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;zip&lt;/code&gt; method pairs each element from two iterators to form a new iterator. The elements of that new iterator are tuples, and each tuple has two values, one from each iterator.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Counter::new()&lt;/code&gt; creates a &lt;code&gt;Counter&lt;/code&gt; that goes from 1 to 5, and &lt;code&gt;Counter::new().skip(1)&lt;/code&gt; creates a &lt;code&gt;Counter&lt;/code&gt; that skips the first value, so it goes from 2 to 5. When the two are zipped together, the result looks like this:&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;Counter::new()&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;Counter::new().skip(1)&lt;/code&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Tuple 0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Tuple 1&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Tuple 2&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Tuple 3&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;em&gt;PS: &lt;code&gt;Counter::new()&lt;/code&gt; does not iterate to 5 because &lt;code&gt;Counter::new().skip(1)&lt;/code&gt; is &lt;code&gt;None&lt;/code&gt; at that point, so no more values are produced.&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;map&lt;/code&gt; takes a closure that is applied to each element of the iterator. It transforms each element of the current iterator into another element, and those new elements form a new iterator. In this example, it multiplies the two values in each tuple stored in the iterator to produce a new iterator.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;filter&lt;/code&gt; keeps the values divisible by 3 and forms a new iterator through the closure.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;sum&lt;/code&gt; consumes all elements of the iterator and adds them together.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The final result should be 18.&lt;/p&gt;

</description>
      <category>rust</category>
      <category>programming</category>
      <category>learning</category>
    </item>
    <item>
      <title>[Rust Guide] 13.7. Using Closures to Capture the Environment With Iterators</title>
      <dc:creator>SomeB1oody</dc:creator>
      <pubDate>Mon, 06 Jul 2026 04:01:27 +0000</pubDate>
      <link>https://dev.to/someb1oody/rust-guide-137-using-closures-to-capture-the-environment-with-iterators-1ia9</link>
      <guid>https://dev.to/someb1oody/rust-guide-137-using-closures-to-capture-the-environment-with-iterators-1ia9</guid>
      <description>&lt;h2&gt;
  
  
  13.7.0 Before We Begin
&lt;/h2&gt;

&lt;p&gt;During its design, Rust drew inspiration from many languages, and functional programming had a particularly strong influence on Rust. Functional programming often includes passing functions as values to parameters, returning them from other functions, assigning them to variables for later execution, and so on.&lt;/p&gt;

&lt;p&gt;In this chapter, we will discuss some Rust features that are similar to what many languages call functional features:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Closures&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Iterators (this article)&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Improving the I/O Project with Closures and Iterators&lt;/li&gt;
&lt;li&gt;Performance of Closures and Iterators&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;If you find this helpful, please like, bookmark, and follow. To keep learning along, follow this series.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  13.7.1 Using Closures to Capture the Environment
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;filter&lt;/code&gt; method is an &lt;em&gt;iterator adaptor&lt;/em&gt; that is usually used together with a closure that captures the environment.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;filter&lt;/code&gt; method takes a closure that returns a boolean value as it traverses each element of the iterator. If the return value is &lt;code&gt;true&lt;/code&gt;, the current element will be included in the new iterator produced by &lt;code&gt;filter&lt;/code&gt;; otherwise, the current element will not be included.&lt;/p&gt;

&lt;p&gt;Take an example:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Use &lt;code&gt;filter&lt;/code&gt; with a closure to capture the &lt;code&gt;shoe_size&lt;/code&gt; variable from the environment and iterate over a collection of &lt;code&gt;Shoe&lt;/code&gt; struct instances. It will return only shoes of the specified size.&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="nd"&gt;#[derive(PartialEq,&lt;/span&gt; &lt;span class="nd"&gt;Debug)]&lt;/span&gt;
&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;Shoe&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;u32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;style&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;shoes_in_size&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;shoes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Vec&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Shoe&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;shoe_size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;u32&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Vec&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Shoe&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;shoes&lt;/span&gt;&lt;span class="nf"&gt;.into_iter&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.filter&lt;/span&gt;&lt;span class="p"&gt;(|&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="py"&gt;.size&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;shoe_size&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.collect&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nd"&gt;#[cfg(test)]&lt;/span&gt;
&lt;span class="k"&gt;mod&lt;/span&gt; &lt;span class="n"&gt;tests&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nd"&gt;#[test]&lt;/span&gt;
    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;filters_by_size&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;shoes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nd"&gt;vec!&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;
            &lt;span class="n"&gt;Shoe&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="n"&gt;style&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nn"&gt;String&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"sneaker"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
            &lt;span class="p"&gt;},&lt;/span&gt;
            &lt;span class="n"&gt;Shoe&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;13&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="n"&gt;style&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nn"&gt;String&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"sandal"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
            &lt;span class="p"&gt;},&lt;/span&gt;
            &lt;span class="n"&gt;Shoe&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="n"&gt;style&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nn"&gt;String&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"boot"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
            &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="p"&gt;];&lt;/span&gt;

        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;in_my_size&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;shoes_in_size&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;shoes&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="nd"&gt;assert_eq!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;in_my_size&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="nd"&gt;vec!&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;
                &lt;span class="n"&gt;Shoe&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="n"&gt;style&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nn"&gt;String&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"sneaker"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="p"&gt;},&lt;/span&gt;
                &lt;span class="n"&gt;Shoe&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="n"&gt;style&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nn"&gt;String&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"boot"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="p"&gt;},&lt;/span&gt;
            &lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The &lt;code&gt;Shoe&lt;/code&gt; struct has two fields: &lt;code&gt;size&lt;/code&gt;, which represents the shoe size and is of type &lt;code&gt;u32&lt;/code&gt;, and &lt;code&gt;style&lt;/code&gt;, which represents the style and is of type &lt;code&gt;String&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &lt;code&gt;shoes_in_size&lt;/code&gt; function takes two parameters: &lt;code&gt;shoes&lt;/code&gt;, whose type is &lt;code&gt;Vec&amp;lt;Shoe&amp;gt;&lt;/code&gt;, and &lt;code&gt;shoe_size&lt;/code&gt;, whose type is &lt;code&gt;u32&lt;/code&gt;, and it returns a &lt;code&gt;Vec&amp;lt;Shoe&amp;gt;&lt;/code&gt;. The function body first calls &lt;code&gt;into_iter&lt;/code&gt; on the incoming &lt;code&gt;Vector&lt;/code&gt; to create an iterator that takes ownership. Then it uses &lt;code&gt;filter&lt;/code&gt;, whose argument is a closure. The closure checks each element’s &lt;code&gt;size&lt;/code&gt; field to see whether it matches &lt;code&gt;shoe_size&lt;/code&gt;; if it does, the element is included in the new iterator. Finally, &lt;code&gt;collect&lt;/code&gt; is called to turn the result into a collection and return it.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>rust</category>
      <category>programming</category>
      <category>learning</category>
    </item>
    <item>
      <title>[Rust Guide] 13.6. Methods That Consume and Produce Iterators</title>
      <dc:creator>SomeB1oody</dc:creator>
      <pubDate>Mon, 06 Jul 2026 03:59:32 +0000</pubDate>
      <link>https://dev.to/someb1oody/rust-guide-136-methods-that-consume-and-produce-iterators-3faj</link>
      <guid>https://dev.to/someb1oody/rust-guide-136-methods-that-consume-and-produce-iterators-3faj</guid>
      <description>&lt;h2&gt;
  
  
  13.6.0 Before We Begin
&lt;/h2&gt;

&lt;p&gt;During its design, Rust drew inspiration from many languages, and functional programming had a particularly strong influence on Rust. Functional programming often includes passing functions as values to parameters, returning them from other functions, assigning them to variables for later execution, and so on.&lt;/p&gt;

&lt;p&gt;In this chapter, we will discuss some Rust features that are similar to what many languages call functional features:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Closures&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Iterators (this article)&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Improving the I/O Project with Closures and Iterators&lt;/li&gt;
&lt;li&gt;Performance of Closures and Iterators&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;If you find this helpful, please like, bookmark, and follow. To keep learning along, follow this series.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  13.6.1 Methods That Consume Iterators
&lt;/h2&gt;

&lt;p&gt;In the standard library, the &lt;code&gt;Iterator&lt;/code&gt; trait has some methods with default implementations. Some of them call &lt;code&gt;next&lt;/code&gt;, which is why &lt;strong&gt;implementing the &lt;code&gt;Iterator&lt;/code&gt; trait requires implementing &lt;code&gt;next&lt;/code&gt;.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Methods that call &lt;code&gt;next&lt;/code&gt; are called &lt;em&gt;consuming adaptors&lt;/em&gt; because &lt;code&gt;next&lt;/code&gt; consumes the iterator one element at a time until the iterator is exhausted.&lt;/p&gt;

&lt;p&gt;For example, the &lt;code&gt;sum&lt;/code&gt; method takes ownership of the iterator and repeatedly calls &lt;code&gt;next&lt;/code&gt; to traverse the items, thereby consuming the iterator. As it iterates, it adds each item to a running total and returns the total when iteration is complete.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="nd"&gt;#[cfg(test)]&lt;/span&gt;
&lt;span class="k"&gt;mod&lt;/span&gt; &lt;span class="n"&gt;tests&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;#[test]&lt;/span&gt;
    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;iterator_sum&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;v1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nd"&gt;vec!&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;v1_iter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;v1&lt;/span&gt;&lt;span class="nf"&gt;.iter&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;total&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;v1_iter&lt;/span&gt;&lt;span class="nf"&gt;.sum&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="nd"&gt;assert_eq!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;total&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  13.6.2 Methods That Produce Other Iterators
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;Iterator&lt;/code&gt; trait also defines other methods called &lt;em&gt;iterator adaptors&lt;/em&gt;. They turn the current iterator into a different kind of iterator. You can also chain multiple iterator adaptors together to perform complex operations, and this style of code is quite readable.&lt;/p&gt;

&lt;p&gt;Take &lt;code&gt;map&lt;/code&gt; as an example. It takes a closure that is applied to each element of the iterator. It transforms each element of the current iterator into another element, and those new elements form a new iterator.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;v1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Vec&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nd"&gt;vec!&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="n"&gt;v1&lt;/span&gt;&lt;span class="nf"&gt;.iter&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.map&lt;/span&gt;&lt;span class="p"&gt;(|&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code adds 1 to each element in the &lt;code&gt;Vector&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;There is nothing wrong with the code itself, but the compiler produces a warning:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="gp"&gt;$&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;cargo run
&lt;span class="go"&gt;   Compiling iterators v0.1.0 (file:///projects/iterators)
warning: unused `Map` that must be used
&lt;/span&gt;&lt;span class="gp"&gt; --&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;src/main.rs:4:5
&lt;span class="go"&gt;  |
&lt;/span&gt;&lt;span class="gp"&gt;4 |     v1.iter().map(|x| x + 1);&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="go"&gt;  |     ^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: iterators are lazy and do nothing unless consumed
&lt;/span&gt;&lt;span class="gp"&gt;  = note: `#&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;warn&lt;span class="o"&gt;(&lt;/span&gt;unused_must_use&lt;span class="o"&gt;)]&lt;/span&gt;&lt;span class="sb"&gt;`&lt;/span&gt; on by default
&lt;span class="go"&gt;help: use `let _ = ...` to ignore the resulting value
  |
&lt;/span&gt;&lt;span class="gp"&gt;4 |     let _ = v1.iter().map(|x| x + 1);&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="go"&gt;  |     +++++++

warning: `iterators` (bin "iterators") generated 1 warning
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.47s
     Running `target/debug/iterators`
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Because Rust iterators are lazy, if you do not consume them (that is, if you &lt;strong&gt;do not call consuming adaptor methods&lt;/strong&gt;), they do nothing. In other words, in this state it does not add 1 to the three elements in the &lt;code&gt;Vector&lt;/code&gt; unless a consuming method is called:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;v1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Vec&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nd"&gt;vec!&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;v2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nb"&gt;Vec&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;v1&lt;/span&gt;&lt;span class="nf"&gt;.iter&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.map&lt;/span&gt;&lt;span class="p"&gt;(|&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.collect&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here &lt;code&gt;collect&lt;/code&gt; is used as a consuming adaptor to collect the results into some kind of collection. Since &lt;code&gt;collect&lt;/code&gt; can produce many collection types, we need to explicitly annotate &lt;code&gt;v2&lt;/code&gt; as a &lt;code&gt;Vec&amp;lt;_&amp;gt;&lt;/code&gt;. The &lt;code&gt;_&lt;/code&gt; in &lt;code&gt;Vec&amp;lt;_&amp;gt;&lt;/code&gt; tells the compiler to infer the element type.&lt;/p&gt;

</description>
      <category>rust</category>
      <category>programming</category>
      <category>learning</category>
    </item>
    <item>
      <title>[Rust Guide] 13.5. Iterators - Definitions, the Iterator Trait, and the Next Method</title>
      <dc:creator>SomeB1oody</dc:creator>
      <pubDate>Mon, 22 Jun 2026 03:24:58 +0000</pubDate>
      <link>https://dev.to/someb1oody/rust-guide-135-iterators-definitions-the-iterator-trait-and-the-next-method-3h93</link>
      <guid>https://dev.to/someb1oody/rust-guide-135-iterators-definitions-the-iterator-trait-and-the-next-method-3h93</guid>
      <description>&lt;h2&gt;
  
  
  13.5.0 Before We Begin
&lt;/h2&gt;

&lt;p&gt;During its design, Rust drew inspiration from many languages, and functional programming had a particularly strong influence on Rust. Functional programming often includes passing functions as values to parameters, returning them from other functions, assigning them to variables for later execution, and so on.&lt;/p&gt;

&lt;p&gt;In this chapter, we will discuss some Rust features that are similar to what many languages call functional features:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Closures&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Iterators (this article)&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Improving the I/O Project with Closures and Iterators&lt;/li&gt;
&lt;li&gt;Performance of Closures and Iterators&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;If you find this helpful, please like, bookmark, and follow. To keep learning along, follow this series.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  13.5.1 What Is an Iterator
&lt;/h2&gt;

&lt;p&gt;To talk about iterators, we first need to talk about the iterator pattern. &lt;strong&gt;The iterator pattern allows you to perform a task on each element in a sequence, one by one.&lt;/strong&gt; In that process, the iterator is responsible for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Traversing each item&lt;/li&gt;
&lt;li&gt;Determining when the sequence has finished iterating&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Rust iterators are lazy: unless you call a method that consumes the iterator, the iterator itself does nothing. In other words, if you write an iterator in your code but never use it, it is as if it did nothing at all.&lt;/p&gt;

&lt;p&gt;Take a look at an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;v1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nd"&gt;vec!&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;v1_iter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;v1&lt;/span&gt;&lt;span class="nf"&gt;.iter&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;v1&lt;/code&gt; is a &lt;code&gt;Vector&lt;/code&gt;, and &lt;code&gt;v1.iter()&lt;/code&gt; creates an iterator for &lt;code&gt;v1&lt;/code&gt; and assigns it to &lt;code&gt;v1_iter&lt;/code&gt;. But &lt;code&gt;v1_iter&lt;/code&gt; is not used yet, so the iterator can be considered to have no effect.&lt;/p&gt;

&lt;p&gt;Now let’s use the iterator to traverse the values:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;v1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nd"&gt;vec!&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;v1_iter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;v1&lt;/span&gt;&lt;span class="nf"&gt;.iter&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;val&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;v1_iter&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Got: {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;val&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is equivalent to using each element in the iterator once in a loop.&lt;/p&gt;

&lt;h2&gt;
  
  
  13.5.2 The &lt;code&gt;Iterator&lt;/code&gt; Trait
&lt;/h2&gt;

&lt;p&gt;All iterators implement the &lt;code&gt;Iterator&lt;/code&gt; trait. This trait is defined in the standard library and looks roughly like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;trait&lt;/span&gt; &lt;span class="nb"&gt;Iterator&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;Item&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Option&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;Self&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Item&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// methods with default implementations elided&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two new pieces of syntax appear here: &lt;code&gt;type Item&lt;/code&gt; and &lt;code&gt;Self::Item&lt;/code&gt;. These syntax forms define types associated with the trait, and we will talk about that in a later article. For now, all you need to know is that implementing the &lt;code&gt;Iterator&lt;/code&gt; trait requires you to define an &lt;code&gt;Item&lt;/code&gt; type, and that type is used as the return type of &lt;code&gt;next&lt;/code&gt; (the iterator’s return type).&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;Iterator&lt;/code&gt; trait requires only one method: &lt;code&gt;next&lt;/code&gt;. Each time &lt;code&gt;next&lt;/code&gt; is called, it returns one item from the iterator, that is, one element of the sequence. Because the return type is &lt;code&gt;Option&lt;/code&gt;, the result is wrapped in the &lt;code&gt;Some&lt;/code&gt; variant. When iteration ends, &lt;code&gt;None&lt;/code&gt; is returned.&lt;/p&gt;

&lt;p&gt;In actual use, you can call &lt;code&gt;next&lt;/code&gt; directly on the iterator. Take a look at an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="nd"&gt;#[cfg(test)]&lt;/span&gt;
&lt;span class="k"&gt;mod&lt;/span&gt; &lt;span class="n"&gt;tests&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;#[test]&lt;/span&gt;
    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;iterator_demonstration&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;v1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nd"&gt;vec!&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;v1_iter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;v1&lt;/span&gt;&lt;span class="nf"&gt;.iter&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

        &lt;span class="nd"&gt;assert_eq!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v1_iter&lt;/span&gt;&lt;span class="nf"&gt;.next&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="nf"&gt;Some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
        &lt;span class="nd"&gt;assert_eq!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v1_iter&lt;/span&gt;&lt;span class="nf"&gt;.next&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="nf"&gt;Some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
        &lt;span class="nd"&gt;assert_eq!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v1_iter&lt;/span&gt;&lt;span class="nf"&gt;.next&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="nf"&gt;Some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
        &lt;span class="nd"&gt;assert_eq!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v1_iter&lt;/span&gt;&lt;span class="nf"&gt;.next&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="nb"&gt;None&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;v1&lt;/code&gt; is a &lt;code&gt;Vector&lt;/code&gt;, and &lt;code&gt;v1_iter&lt;/code&gt; is its iterator. Because the operations below are considered to change the iterator’s state, &lt;code&gt;mut&lt;/code&gt; must be used to make it mutable.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;assert_eq!(v1_iter.next(), Some(&amp;amp;1));&lt;/code&gt; is the first call to &lt;code&gt;next&lt;/code&gt;, so it returns the first element in the &lt;code&gt;Vector&lt;/code&gt;, wrapped in &lt;code&gt;Some&lt;/code&gt;, which is &lt;code&gt;Some(&amp;amp;1)&lt;/code&gt;. It is &lt;code&gt;&amp;amp;1&lt;/code&gt; because the iterator’s return value is an immutable reference wrapped by &lt;code&gt;Option&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;assert_eq!(v1_iter.next(), Some(&amp;amp;2));&lt;/code&gt; is the second call to &lt;code&gt;next&lt;/code&gt;, so it returns the second element in the &lt;code&gt;Vector&lt;/code&gt;, wrapped in &lt;code&gt;Some&lt;/code&gt;, which is &lt;code&gt;Some(&amp;amp;2)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;And so on...&lt;/li&gt;
&lt;li&gt;Calling &lt;code&gt;next&lt;/code&gt; on an iterator &lt;strong&gt;changes the iterator’s internal state that tracks its position in the sequence&lt;/strong&gt;. In other words, each call consumes one element from the iterator. The &lt;code&gt;for&lt;/code&gt; loop in the 13.5.1 example does not need &lt;code&gt;mut&lt;/code&gt; because the &lt;code&gt;for&lt;/code&gt; loop actually takes ownership of &lt;code&gt;v1_iter&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  13.5.3 Several Iteration Methods
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;iter&lt;/code&gt; method we just used generates an iterator over immutable references, so the values obtained through &lt;code&gt;next&lt;/code&gt; are actually immutable references to the elements in the &lt;code&gt;Vector&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;into_iter&lt;/code&gt; method creates an iterator that takes ownership. In other words, as it iterates through the elements, it moves them into the new scope and takes ownership of them.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;iter_mut&lt;/code&gt; method uses mutable references when traversing values.&lt;/p&gt;

</description>
      <category>rust</category>
      <category>programming</category>
      <category>learning</category>
    </item>
  </channel>
</rss>
