<?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: Francesco Ciulla</title>
    <description>The latest articles on DEV Community by Francesco Ciulla (@francescoxx).</description>
    <link>https://dev.to/francescoxx</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.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F344808%2F8986fcb1-61a0-4888-a85e-13c115030558.jpeg</url>
      <title>DEV Community: Francesco Ciulla</title>
      <link>https://dev.to/francescoxx</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/francescoxx"/>
    <language>en</language>
    <item>
      <title>Rust CRUD Rest API, using Axum, sqlx, Postgres, Docker and Docker Compose</title>
      <dc:creator>Francesco Ciulla</dc:creator>
      <pubDate>Tue, 16 Dec 2025 13:49:16 +0000</pubDate>
      <link>https://dev.to/francescoxx/rust-crud-rest-api-using-axum-sqlx-postgres-docker-and-docker-compose-152a</link>
      <guid>https://dev.to/francescoxx/rust-crud-rest-api-using-axum-sqlx-postgres-docker-and-docker-compose-152a</guid>
      <description>&lt;p&gt;Let's create a CRUD Rest API in Rust, using the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Axum (Rust web framework)&lt;/li&gt;
&lt;li&gt;sqlx (ORM)&lt;/li&gt;
&lt;li&gt;Postgres (database)&lt;/li&gt;
&lt;li&gt;Docker (containerization)&lt;/li&gt;
&lt;li&gt;Docker Compose (to run the application and the database in containers)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you prefer a video version:&lt;/p&gt;

&lt;p&gt;

  &lt;iframe src="https://www.youtube.com/embed/cJyl9e2oqHY"&gt;
  &lt;/iframe&gt;


&lt;/p&gt;

&lt;p&gt;All the code is available in the GitHub repository (link in the video description): &lt;/p&gt;




&lt;h2&gt;
  
  
  🏁 Intro
&lt;/h2&gt;

&lt;p&gt;Here is a schema of the architecture of the application we are going to create:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://youtu.be/cJyl9e2oqHY" rel="noopener noreferrer"&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.amazonaws.com%2Fuploads%2Farticles%2F5ab1620vkppzrm8iz9s2.png" alt="crud, read, update, delete, to a flask app and postgres service, connected with docker compose. Postman and tableplus to test it"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We will create 5 endpoints for basic CRUD operations:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create&lt;/li&gt;
&lt;li&gt;Read all&lt;/li&gt;
&lt;li&gt;Read one&lt;/li&gt;
&lt;li&gt;Update&lt;/li&gt;
&lt;li&gt;Delete&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We will also create a simple endpoint to test if the server is running.&lt;/p&gt;

&lt;p&gt;Here are the steps we are going through:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a compose.yml file and ru the Postgres instance&lt;/li&gt;
&lt;li&gt;Create an Axum application using sqlx as an ORM&lt;/li&gt;
&lt;li&gt;Dockerize the Axum application&lt;/li&gt;
&lt;li&gt;Run the Axum application using docker compose&lt;/li&gt;
&lt;li&gt;Test the application &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We will go with a step-by-step guide, so you can follow along.&lt;/p&gt;




&lt;h2&gt;
  
  
  🛑 Prerequisites
&lt;/h2&gt;

&lt;p&gt;Before starting, make sure you have the following installed on your machine:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Docker&lt;/li&gt;
&lt;li&gt;Rust and Cargo&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🏁 Project initialization
&lt;/h2&gt;

&lt;p&gt;Let's iitialize the project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;cargo new axumlive
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will create a new folder called &lt;code&gt;axumlive&lt;/code&gt; with the following structure:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Run the Postgres container
&lt;/h2&gt;

&lt;p&gt;Let's run the Postgres container first, so we have the database ready when we will run the Axum application.&lt;/p&gt;

&lt;p&gt;To do that, create a file called &lt;code&gt;compose.yml&lt;/code&gt; in the root of the project (&lt;code&gt;axumlive&lt;/code&gt; folder) with the following content:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;  &lt;span class="na"&gt;db&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;postgres:15&lt;/span&gt;
    &lt;span class="na"&gt;container_name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;db&lt;/span&gt;
    &lt;span class="na"&gt;ports&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;5432:5432"&lt;/span&gt;
    &lt;span class="na"&gt;environment&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;POSTGRES_USER&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;user&lt;/span&gt;
      &lt;span class="na"&gt;POSTGRES_PASSWORD&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;password&lt;/span&gt;
      &lt;span class="na"&gt;POSTGRES_DB&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;simple_api&lt;/span&gt;
    &lt;span class="na"&gt;volumes&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;pg_data:/var/lib/postgresql/data&lt;/span&gt;

&lt;span class="na"&gt;volumes&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;pg_data&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, to run the Postgres container, type:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker compose up &lt;span class="nt"&gt;-d&lt;/span&gt; db
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will run the Postgres container in detached mode (in the background).&lt;/p&gt;

&lt;p&gt;If you see something like this, it means that the container is running:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://youtu.be/cJyl9e2oqHY" rel="noopener noreferrer"&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.amazonaws.com%2Fuploads%2Farticles%2Froljcqf6ogzm8sq1topw.png" alt="docker downlading image - Build a CRUD Rest API in Python using Flask, SQLAlchemy, Postgres, Docker"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, to check if the container is running, type:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker ps &lt;span class="nt"&gt;-a&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If everything is ok, you should see something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://youtu.be/cJyl9e2oqHY" rel="noopener noreferrer"&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.amazonaws.com%2Fuploads%2Farticles%2F9gvz7t3wzo6hlogblylo.png" alt="one container running"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now before we write our Axum application, let's step inside the Postgres container:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker exec -it db psql -U user -d simple_api
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will open the Postgres shell.&lt;/p&gt;

&lt;p&gt;You should see something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://youtu.be/cJyl9e2oqHY" rel="noopener noreferrer"&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.amazonaws.com%2Fuploads%2Farticles%2Fsqzjblh1jqktgggyyjsu.png" alt="one container running"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now type:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;\dt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And you should see "didn't find any relations" because the database is empty.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://youtu.be/cJyl9e2oqHY" rel="noopener noreferrer"&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.amazonaws.com%2Fuploads%2Farticles%2Ftd59auzljbzoaj2n9bg2.png" alt="one container running"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is normal but let's keep this terminal open somewhere. We will use it later to check if the tables are created correctly.&lt;/p&gt;




&lt;h2&gt;
  
  
  📦 Add dependencies and the Sql migrations
&lt;/h2&gt;

&lt;p&gt;Let's add the dependencies we need to the &lt;code&gt;Cargo.toml&lt;/code&gt; file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;...
[dependencies]
axum = "0.8"
tokio = { version = "1", features = ["macros", "rt-multi-thread", "net"] }
sqlx = { version = "0.8", features = ["runtime-tokio-rustls", "postgres", "macros"] }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since we want a convenient way to create the tables in the database, we will use sqlx migrations. This allows us to create the table the first time we run the application. This is also convenient in case someone else wants to run the application, they just have to run the migrations and the tables will be created automatically.&lt;/p&gt;

&lt;p&gt;Now, let's create a folder called &lt;code&gt;migrations&lt;/code&gt; in the root of the project (&lt;code&gt;axumlive&lt;/code&gt; folder) and inside it create a file called &lt;code&gt;0001_users_table.sql&lt;/code&gt; with the following content:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;IF&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;EXISTS&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="nb"&gt;SERIAL&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="nb"&gt;TEXT&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;email&lt;/span&gt; &lt;span class="nb"&gt;TEXT&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;UNIQUE&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We are now ready to write the Axum application.&lt;/p&gt;

&lt;h2&gt;
  
  
  📦 Add The Dependncies
&lt;/h2&gt;

&lt;p&gt;Open the &lt;code&gt;Cargo.toml&lt;/code&gt; file and add the following dependencies:&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;axum&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"0.8"&lt;/span&gt;
&lt;span class="py"&gt;tokio&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&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;"1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="py"&gt;features&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"macros"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"rt-multi-thread"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"net"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="py"&gt;sqlx&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&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.8"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="py"&gt;features&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"runtime-tokio-rustls"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"postgres"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"macros"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="py"&gt;serde&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&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;"1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="py"&gt;features&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"derive"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="py"&gt;serde_json&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"1"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  🦀 Create the Axum backend
&lt;/h2&gt;

&lt;p&gt;Let's open the &lt;code&gt;main.rs&lt;/code&gt; file and populate it with the following 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;use&lt;/span&gt; &lt;span class="nn"&gt;axum&lt;/span&gt;&lt;span class="p"&gt;::{&lt;/span&gt; &lt;span class="nn"&gt;extract&lt;/span&gt;&lt;span class="p"&gt;::{&lt;/span&gt; &lt;span class="n"&gt;Path&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;State&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="nn"&gt;http&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;StatusCode&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nn"&gt;routing&lt;/span&gt;&lt;span class="p"&gt;::{&lt;/span&gt; &lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;post&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="n"&gt;Json&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Router&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;serde&lt;/span&gt;&lt;span class="p"&gt;::{&lt;/span&gt; &lt;span class="n"&gt;Deserialize&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Serialize&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;sqlx&lt;/span&gt;&lt;span class="p"&gt;::{&lt;/span&gt; &lt;span class="nn"&gt;postgres&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;PgPoolOptions&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;FromRow&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;PgPool&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;env&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nd"&gt;#[derive(Deserialize)]&lt;/span&gt;
&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;UserPayload&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;name&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="n"&gt;email&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="nd"&gt;#[derive(Serialize,&lt;/span&gt; &lt;span class="nd"&gt;FromRow)]&lt;/span&gt;
&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;User&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;id&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="n"&gt;name&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="n"&gt;email&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="nd"&gt;#[tokio::main]&lt;/span&gt;
&lt;span class="k"&gt;async&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;db_url&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;var&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"DATABASE_URL"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"DATABASE_URL must be set"&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;pool&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;PgPoolOptions&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;.connect&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;db_url&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="k"&gt;.await&lt;/span&gt;&lt;span class="nf"&gt;.expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Failed to connect to DB"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nn"&gt;sqlx&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nd"&gt;migrate!&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="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;pool&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="k"&gt;.await&lt;/span&gt;&lt;span class="nf"&gt;.expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Migrations failed"&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;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;Router&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;.route&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="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
        &lt;span class="nf"&gt;.route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/users"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;create_user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;list_users&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
        &lt;span class="nf"&gt;.route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/users/{id}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;get_user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.put&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;update_user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.delete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;delete_user&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
        &lt;span class="nf"&gt;.with_state&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pool&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;listener&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;tokio&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;net&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;TcpListener&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"0.0.0.0:8000"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="k"&gt;.await&lt;/span&gt;&lt;span class="nf"&gt;.unwrap&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;"🚀 Server running on port 8000"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nn"&gt;axum&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;serve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;listener&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="k"&gt;.await&lt;/span&gt;&lt;span class="nf"&gt;.unwrap&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;//Endpoint Handlers&lt;/span&gt;
&lt;span class="c1"&gt;//test endpoint&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;root&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;-&amp;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="p"&gt;{&lt;/span&gt;
    &lt;span class="s"&gt;"Welcome to the User Management API!"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;//GET ALL&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;list_users&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;State&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pool&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="n"&gt;State&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;PgPool&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;-&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;Json&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&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;User&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="n"&gt;StatusCode&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nn"&gt;sqlx&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;query_as&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;User&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"SELECT * FROM users"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;.fetch_all&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;pool&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="k"&gt;.await&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;Json&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;.map_err&lt;/span&gt;&lt;span class="p"&gt;(|&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="nn"&gt;StatusCode&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;INTERNAL_SERVER_ERROR&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;//CREATE USER&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;create_user&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nf"&gt;State&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pool&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="n"&gt;State&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;PgPool&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nf"&gt;Json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="n"&gt;Json&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;UserPayload&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;-&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="n"&gt;StatusCode&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Json&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;User&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;StatusCode&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nn"&gt;sqlx&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;query_as&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;User&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"INSERT INTO users (name, email) VALUES ($1, $2) RETURNING *"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;.bind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="py"&gt;.name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;.bind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="py"&gt;.email&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;.fetch_one&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;pool&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="k"&gt;.await&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;u&lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nn"&gt;StatusCode&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;CREATED&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;Json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;
        &lt;span class="nf"&gt;.map_err&lt;/span&gt;&lt;span class="p"&gt;(|&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="nn"&gt;StatusCode&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;INTERNAL_SERVER_ERROR&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;//GET USER BY ID&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;get_user&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nf"&gt;State&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pool&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="n"&gt;State&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;PgPool&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nf"&gt;Path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="n"&gt;Path&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="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;Json&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;User&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;StatusCode&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nn"&gt;sqlx&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;query_as&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;User&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"SELECT * FROM users WHERE id = $1"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;.bind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;.fetch_one&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;pool&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="k"&gt;.await&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;Json&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;.map_err&lt;/span&gt;&lt;span class="p"&gt;(|&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="nn"&gt;StatusCode&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;NOT_FOUND&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;//UPDATE USER&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;update_user&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nf"&gt;State&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pool&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="n"&gt;State&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;PgPool&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nf"&gt;Path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="n"&gt;Path&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="p"&gt;,&lt;/span&gt;
    &lt;span class="nf"&gt;Json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="n"&gt;Json&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;UserPayload&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;-&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;Json&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;User&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;StatusCode&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nn"&gt;sqlx&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;query_as&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;User&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"UPDATE users SET name = $1, email = $2 WHERE id = $3 RETURNING *"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;.bind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="py"&gt;.name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;.bind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="py"&gt;.email&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;.bind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;.fetch_one&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;pool&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="k"&gt;.await&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;Json&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;.map_err&lt;/span&gt;&lt;span class="p"&gt;(|&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="nn"&gt;StatusCode&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;INTERNAL_SERVER_ERROR&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;//DELETE USER&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;delete_user&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nf"&gt;State&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pool&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="n"&gt;State&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;PgPool&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nf"&gt;Path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="n"&gt;Path&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="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;StatusCode&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;StatusCode&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="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sqlx&lt;/span&gt;
        &lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"DELETE FROM users WHERE id = $1"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;.bind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;.execute&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;pool&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="k"&gt;.await&lt;/span&gt;
        &lt;span class="nf"&gt;.map_err&lt;/span&gt;&lt;span class="p"&gt;(|&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="nn"&gt;StatusCode&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;INTERNAL_SERVER_ERROR&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;if&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="nf"&gt;.rows_affected&lt;/span&gt;&lt;span class="p"&gt;()&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;Err&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nn"&gt;StatusCode&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;NOT_FOUND&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;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nn"&gt;StatusCode&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;NO_CONTENT&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;Explanation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We define the data structures for the user payload and the user model using Serde for serialization and deserialization.&lt;/li&gt;
&lt;li&gt;We set up the database connection using sqlx and run the migrations to create the users table&lt;/li&gt;
&lt;li&gt;We define the Axum routes for the CRUD operations and associate them with their respective handler functions&lt;/li&gt;
&lt;li&gt;Each handler function interacts with the database using sqlx to perform the required operations and returns appropriate HTTP responses.&lt;/li&gt;
&lt;li&gt;Finally, we start the Axum server on port 8000.
___
## 🐳 Dockerize the Axum application&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It's time to Dockerize the Rust application. To do this, first let's create a .dockerignore file in the root of the project (&lt;code&gt;axumlive&lt;/code&gt; folder) with the following content:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;target
.git
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will prevent Docker from copying the target folder and the .git folder into the Docker image, which are not needed.&lt;/p&gt;

&lt;p&gt;In the root folder of the project (&lt;code&gt;axumlive&lt;/code&gt;), create a file called &lt;code&gt;Dockerfile&lt;/code&gt;&lt;br&gt;
This one will be a multi-stage Dockerfile, to keep the final image as small as possible.&lt;/p&gt;

&lt;p&gt;Let's populate the &lt;code&gt;Dockerfile&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Dockerfile:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="c"&gt;#stage 1&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;rust:1.91&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;as&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;builder&lt;/span&gt;
&lt;span class="k"&gt;WORKDIR&lt;/span&gt;&lt;span class="s"&gt; /app&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; . .&lt;/span&gt;
&lt;span class="k"&gt;RUN &lt;/span&gt;cargo build &lt;span class="nt"&gt;--release&lt;/span&gt;


&lt;span class="c"&gt;#stage 2&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="s"&gt; debian:bookworm-slim&lt;/span&gt;
&lt;span class="k"&gt;WORKDIR&lt;/span&gt;&lt;span class="s"&gt; /app&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; --from=builder /app/target/release/axumlive .&lt;/span&gt;
&lt;span class="k"&gt;EXPOSE&lt;/span&gt;&lt;span class="s"&gt; 8000&lt;/span&gt;
&lt;span class="k"&gt;CMD&lt;/span&gt;&lt;span class="s"&gt; ["./axumlive"]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  🐳🐳Docker compose
&lt;/h2&gt;

&lt;p&gt;Since now we defined the Axum application and we dockerized it, it's time to add this service to the compose.yml file, so we can run both the Axum application and the Postgres database with a single command.&lt;/p&gt;

&lt;p&gt;Populate the &lt;code&gt;docker-compose.yml&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;services&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;app&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;container_name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;simple_axum&lt;/span&gt;
    &lt;span class="na"&gt;build&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;.&lt;/span&gt;
    &lt;span class="na"&gt;ports&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;8000:8000"&lt;/span&gt;
    &lt;span class="na"&gt;environment&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;DATABASE_URL&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;postgres://user:password@db:5432/simple_api&lt;/span&gt;
  &lt;span class="na"&gt;db&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;postgres:15&lt;/span&gt;
    &lt;span class="na"&gt;container_name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;db&lt;/span&gt;
    &lt;span class="na"&gt;ports&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;5432:5432"&lt;/span&gt;
    &lt;span class="na"&gt;environment&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;POSTGRES_USER&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;user&lt;/span&gt;
      &lt;span class="na"&gt;POSTGRES_PASSWORD&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;password&lt;/span&gt;
      &lt;span class="na"&gt;POSTGRES_DB&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;simple_api&lt;/span&gt;
    &lt;span class="na"&gt;volumes&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;pg_data:/var/lib/postgresql/data&lt;/span&gt;

&lt;span class="na"&gt;volumes&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;pg_data&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We defined a new service called "app" which is the Axum application. We set the build context to the current directory (&lt;code&gt;.&lt;/code&gt;) and we map the port 8000 of the container to the port 8000 of the host machine.&lt;/p&gt;




&lt;h2&gt;
  
  
  👟 Build and Run the Axum appliation
&lt;/h2&gt;

&lt;p&gt;Now let's build so we can run the Axum application.&lt;/p&gt;

&lt;p&gt;Let's type&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker compose build
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And you should see something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://youtu.be/cJyl9e2oqHY" rel="noopener noreferrer"&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.amazonaws.com%2Fuploads%2Farticles%2Fnjfxm3fenpistd4pocmr.png" alt="docker build"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This should BUILD the app image, with the name defined in the "image" value, in this case, ""simple_axum".&lt;/p&gt;

&lt;p&gt;You can also see all the steps docker did to build the image, layer by layer. You might recognize some of them, because we defined them in the Dockerfile.&lt;/p&gt;

&lt;p&gt;Now let's run the app service:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker compose up -d app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will run the app service in detached mode (in the background).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://youtu.be/cJyl9e2oqHY" rel="noopener noreferrer"&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.amazonaws.com%2Fuploads%2Farticles%2Fs41id9r74bnfjm8ovp69.png" alt="two containers running"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's check if both the containrs are running:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker ps -a
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Important step: is you go back to the psql terminal where we stepped into the Postgres container, and type:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;\dt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;you should now see the &lt;code&gt;users&lt;/code&gt; table created by the migration:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://youtu.be/cJyl9e2oqHY" rel="noopener noreferrer"&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.amazonaws.com%2Fuploads%2Farticles%2Fgeh5zhbkdiotw7np06km.png" alt="users table created"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Of course, this talbe is currenlty empty, but at least we know that the migration ran successfully when the Axum application started.&lt;/p&gt;




&lt;h2&gt;
  
  
  🔍 Test the application
&lt;/h2&gt;

&lt;p&gt;Now let's  test our application.&lt;/p&gt;

&lt;p&gt;The first step is to visit &lt;code&gt;http://localhost:8000/&lt;/code&gt; in your browser:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://youtu.be/cJyl9e2oqHY" rel="noopener noreferrer"&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.amazonaws.com%2Fuploads%2Farticles%2Fs9zq0wzem752i6413kva.png" alt="users table created"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can tet it in different ways. In this specific case, I will use a VS Code extension called &lt;code&gt;REST Client&lt;/code&gt;, but you can use Postman, curl or any other tool you prefer.&lt;/p&gt;

&lt;p&gt;You can test the application in the way you prefer, but for convenience I will add a file you can copy paste in your application at the root level, called &lt;code&gt;request.hhtp&lt;/code&gt;, with all the requests we need to test the applicationL&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="err"&gt;// requests.http
@baseUrl = http://localhost:8000

### 1. Test Root Endpoint (Health Check)
GET {{baseUrl}}/

### 2. Get All Users (Should be empty initially)
GET {{baseUrl}}/users

### 3. Create User 1
POST {{baseUrl}}/users
Content-Type: application/json

{
    "name": "Alice Smith",
    "email": "alice@example.com"
}

### 4. Create User 2
POST {{baseUrl}}/users
Content-Type: application/json

{
    "name": "Bob Jones",
    "email": "bob@example.com"
}

### 5. Create User 3
POST {{baseUrl}}/users
Content-Type: application/json

{
    "name": "Charlie Day",
    "email": "charlie@example.com"
}

### 6. Get All Users (Should see 3 users now)
GET {{baseUrl}}/users

### 7. Get Single User (Assuming ID 1 exists)
GET {{baseUrl}}/users/1

### 8. Get User that does not exist (Test 404)
GET {{baseUrl}}/users/9999

### 9. Update User 2
# Note: Ensure your backend handles PUT or PATCH for updates
PUT {{baseUrl}}/users/2
Content-Type: application/json

{
    "name": "Bob James",
    "email": "bobjames@example.com"
}

### 10. Get All Users (Verify Update)
GET {{baseUrl}}/users

### 11. Delete User 2
DELETE {{baseUrl}}/users/3

### 12. Get All Users (Final check - Bob should be gone)
GET {{baseUrl}}/users
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can test the example Endoiunt by runnig the first request in the file&lt;br&gt;
, which is a GET request to &lt;code&gt;localhost:8000/&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://youtu.be/cJyl9e2oqHY" rel="noopener noreferrer"&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.amazonaws.com%2Fuploads%2Farticles%2Fp78co3s64xyqgx9cthlv.png" alt="GET request to localhost:4000/"&gt;&lt;/a&gt;&lt;/p&gt;


&lt;h3&gt;
  
  
  📝 Create a user
&lt;/h3&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
Now let's create a user, making a POST request to &lt;code&gt;localhost:8000/users&lt;/code&gt; with the body below as a request body:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://youtu.be/cJyl9e2oqHY" rel="noopener noreferrer"&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.amazonaws.com%2Fuploads%2Farticles%2Ffo6anl38jwh6cy3gjy4u.png" alt="POST request to localhost:4000/users"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's create another one:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://youtu.be/cJyl9e2oqHY" rel="noopener noreferrer"&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.amazonaws.com%2Fuploads%2Farticles%2Fp040uo7qmbtn3qkd9mz3.png" alt="POST request to localhost:4000/users"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;One more:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://youtu.be/cJyl9e2oqHY" rel="noopener noreferrer"&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.amazonaws.com%2Fuploads%2Farticles%2Fq4wb9gq0vh3uym0ct7ip.png" alt="POST request to localhost:4000/users"&gt;&lt;/a&gt;&lt;/p&gt;


&lt;h3&gt;
  
  
  📝 Get all users
&lt;/h3&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
Now, let's make a GET request to &lt;code&gt;localhost:8000/users&lt;/code&gt; to get all the users:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://youtu.be/cJyl9e2oqHY" rel="noopener noreferrer"&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.amazonaws.com%2Fuploads%2Farticles%2Fnhahlxfcf5ofyhjw0drt.png" alt="GET request to localhost:4000/users"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We just created 3 users.&lt;/p&gt;

&lt;p&gt;From the browser, we can see:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://youtu.be/cJyl9e2oqHY" rel="noopener noreferrer"&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.amazonaws.com%2Fuploads%2Farticles%2Fd9rw9wf30wt1mpmbgttu.png" alt="GET request to localhost:4000/users from browser"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If we go back to the psql terminal where we stepped into the Postgres container, and type:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT * FROM users;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;we should see the 3 users we just created:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://youtu.be/cJyl9e2oqHY" rel="noopener noreferrer"&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.amazonaws.com%2Fuploads%2Farticles%2F8k5m8s3miyruni3i2zk4.png" alt="3 users in the users table"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  📝 Get a specific user
&lt;/h3&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
If you want to get a specific user, you can make a GET request to &lt;code&gt;localhost:4000/users/&amp;lt;user_id&amp;gt;&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;For example, to get the user with id 1, you can make a GET request to &lt;code&gt;localhost:8000/users/1&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://youtu.be/cJyl9e2oqHY" rel="noopener noreferrer"&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.amazonaws.com%2Fuploads%2Farticles%2Fukaama9u1epr6lj6abfa.png" alt="GET request to localhost:4000/users/2"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If we try to get a user that does not exist, for example the user with id 9999, we should get a 404 error:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://youtu.be/cJyl9e2oqHY" rel="noopener noreferrer"&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.amazonaws.com%2Fuploads%2Farticles%2Fc0tehuoj1qb8ogb32v5j.png" alt="GET request to localhost:4000/users/9999"&gt;&lt;/a&gt;&lt;/p&gt;


&lt;h3&gt;
  
  
  📝 Update a user
&lt;/h3&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
If you want to update a user, you can make a PUT request to &lt;code&gt;localhost:8000/users/&amp;lt;user_id&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For example, to update the user with id 2, you can make a PUT request to &lt;code&gt;localhost:8000/users/2&lt;/code&gt; with the body below as a request body:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://youtu.be/cJyl9e2oqHY" rel="noopener noreferrer"&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.amazonaws.com%2Fuploads%2Farticles%2F612fe6qf75k35plvw4y7.png" alt="PUT request to localhost:4000/users/2"&gt;&lt;/a&gt;&lt;/p&gt;


&lt;h3&gt;
  
  
  📝 Delete a user
&lt;/h3&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
To delete a user, you can make a DELETE request to &lt;code&gt;localhost:4000/users/&amp;lt;user_id&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For Example, to delete the user with id 3, you can make a DELETE request to &lt;code&gt;localhost:8000/users/3&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://youtu.be/cJyl9e2oqHY" rel="noopener noreferrer"&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.amazonaws.com%2Fuploads%2Farticles%2Fqvnene1mqrph0u2d7bu8.png" alt="DELETE request to localhost:4000/users/2"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To check if the user has been deleted, you can make a GET request to &lt;code&gt;localhost:8000/users&lt;/code&gt; or check directly in the browser:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://youtu.be/cJyl9e2oqHY" rel="noopener noreferrer"&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.amazonaws.com%2Fuploads%2Farticles%2Fdovcxkyvzy2ep2x9zk87.png" alt="GET request to localhost:4000/users"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see the user with id 3 is not there anymore.&lt;/p&gt;


&lt;h2&gt;
  
  
  🏁 Conclusion
&lt;/h2&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
We made it! We have built a CRUD rest API in Rust using Axum, sqlx, and Postgres, and we dockerized the application.&lt;/p&gt;

&lt;p&gt;This is just an example, but you can use this as a starting point to build your own application.&lt;/p&gt;

&lt;p&gt;If you prefer a video version:&lt;/p&gt;

&lt;p&gt;

  &lt;iframe src="https://www.youtube.com/embed/cJyl9e2oqHY"&gt;
  &lt;/iframe&gt;


&lt;/p&gt;

&lt;p&gt;All the code is available in the GitHub repository (link in the video description): &lt;a href="https://youtu.be/cJyl9e2oqHY" rel="noopener noreferrer"&gt;https://youtu.be/cJyl9e2oqHY&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That's all. &lt;/p&gt;

&lt;p&gt;If you have any question, drop a comment below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://francescociulla.com" rel="noopener noreferrer"&gt;Francesco&lt;/a&gt;&lt;/p&gt;

</description>
      <category>rust</category>
      <category>webdev</category>
      <category>programming</category>
      <category>postgres</category>
    </item>
    <item>
      <title>Redox OS: Is the Future of Operating Systems Written in Rust?</title>
      <dc:creator>Francesco Ciulla</dc:creator>
      <pubDate>Thu, 11 Dec 2025 04:11:01 +0000</pubDate>
      <link>https://dev.to/francescoxx/redox-os-is-the-future-of-operating-systems-written-in-rust-d2n</link>
      <guid>https://dev.to/francescoxx/redox-os-is-the-future-of-operating-systems-written-in-rust-d2n</guid>
      <description>&lt;p&gt;Hi! Have you ever wondered what an operating system would look like if it were built today, prioritizing memory safety and modern design patterns from the ground up? Meet Redox OS.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Redox?
&lt;/h2&gt;

&lt;p&gt;Redox is a Unix-like operating system written in Rust. Unlike the monolithic kernels of Linux or Windows, Redox utilizes a microkernel design. This means the core kernel is tiny, and most services (like drivers and filesystems) run in userspace.&lt;/p&gt;

&lt;p&gt;The result? A system that is incredibly stable and secure. Because it is written in Rust, it effectively eliminates entire classes of bugs and security vulnerabilities related to memory management that have plagued older OSs for decades.&lt;/p&gt;

&lt;h2&gt;
  
  
  A Chat with the Creator
&lt;/h2&gt;

&lt;p&gt;I recently had the incredible opportunity to attend RustConf, where I had the chance to catch up with Jeremy Soller, the creator of Redox OS.&lt;/p&gt;

&lt;p&gt;We chatted about the challenges of building an OS from scratch, the benefits of the microkernel architecture, and where the project is heading next. It was fascinating to hear directly from the source how Rust is enabling a new era of system development.&lt;/p&gt;

&lt;p&gt;Let me know what you think!&lt;br&gt;
Francesco&lt;/p&gt;

&lt;p&gt;

  &lt;iframe src="https://www.youtube.com/embed/NAck7dPKk7c"&gt;
  &lt;/iframe&gt;


&lt;/p&gt;

</description>
      <category>rust</category>
      <category>programming</category>
      <category>techtalks</category>
      <category>linux</category>
    </item>
    <item>
      <title>AI vs. Machine Learning vs. Deep Learning</title>
      <dc:creator>Francesco Ciulla</dc:creator>
      <pubDate>Thu, 04 Dec 2025 18:36:55 +0000</pubDate>
      <link>https://dev.to/francescoxx/ai-vs-machine-learning-vs-deep-learning-2ph0</link>
      <guid>https://dev.to/francescoxx/ai-vs-machine-learning-vs-deep-learning-2ph0</guid>
      <description>&lt;p&gt;&lt;strong&gt;Read time:&lt;/strong&gt; ~1 min&lt;/p&gt;

&lt;p&gt;You hear the terms Artificial Intelligence, Machine Learning, and Deep Learning every day. People often use them interchangeably, but they are not the same thing. Using them incorrectly can make it sound like you don't know what you're talking about.&lt;/p&gt;

&lt;p&gt;If you prefer a video version, you can watch it here:&lt;/p&gt;

&lt;p&gt;

  &lt;iframe src="https://www.youtube.com/embed/ilPgBZhGSTw"&gt;
  &lt;/iframe&gt;


&lt;/p&gt;

&lt;p&gt;You hear the terms Artificial Intelligence, Machine Learning, and Deep Learning every day. People often use them interchangeably, but they are not the same thing. Using them incorrectly can make it sound like you don't know what you're talking about.&lt;/p&gt;

&lt;p&gt;To fix this, let's visualize the concepts using a &lt;strong&gt;Russian nesting doll&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. The Outer Shell: Artificial Intelligence (AI)
&lt;/h2&gt;

&lt;p&gt;Imagine the biggest doll. This is &lt;strong&gt;AI&lt;/strong&gt;.&lt;br&gt;
It is the broad umbrella term for any technique that enables computers to mimic human intelligence. This includes everything from the super-smart robots in sci-fi movies to the ghosts in Pac-Man.&lt;/p&gt;

&lt;p&gt;If you write a program with a thousand &lt;code&gt;if/else&lt;/code&gt; statements to play chess, that is technically AI. It mimics intelligence, but it doesn't strictly "learn."&lt;/p&gt;
&lt;h2&gt;
  
  
  2. The Middle Doll: Machine Learning (ML)
&lt;/h2&gt;

&lt;p&gt;Open the big doll, and inside is &lt;strong&gt;Machine Learning&lt;/strong&gt;.&lt;br&gt;
This is where things get interesting. In traditional AI, you explicitly tell the computer rules; in Machine Learning, you give it &lt;strong&gt;data&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;If you show the machine 10,000 pictures of cats and dogs, it figures out the rules itself using statistics. It learns from experience. This technology is what powers your Netflix recommendations and email spam filters.&lt;/p&gt;
&lt;h2&gt;
  
  
  3. The Smallest Doll: Deep Learning (DL)
&lt;/h2&gt;

&lt;p&gt;Inside the middle doll is the smallest, most complex one: &lt;strong&gt;Deep Learning&lt;/strong&gt;.&lt;br&gt;
This is a specialized subset of Machine Learning. While ML relies on statistics, Deep Learning uses &lt;strong&gt;neural networks&lt;/strong&gt;—structures inspired by the human brain.&lt;/p&gt;

&lt;p&gt;It uses layers of artificial neurons to process massive amounts of complex data. This requires huge computing power but achieves results that traditional ML never could, powering self-driving cars, facial recognition, and ChatGPT.&lt;/p&gt;


&lt;h3&gt;
  
  
  The Summary
&lt;/h3&gt;

&lt;p&gt;Here is the hierarchy: &lt;strong&gt;Deep Learning is a type of Machine Learning, which is a type of AI&lt;/strong&gt;. Not all AI learns, but all Deep Learning is AI.&lt;/p&gt;

&lt;p&gt;Now you know the difference. Next time someone tries to sell you an "AI toothbrush," you can ask them if it uses a neural network or just a simple &lt;code&gt;if/else&lt;/code&gt; statement.&lt;/p&gt;

&lt;p&gt;If you prefer a video version, you can watch it here:&lt;/p&gt;

&lt;p&gt;

  &lt;iframe src="https://www.youtube.com/embed/ilPgBZhGSTw"&gt;
  &lt;/iframe&gt;


&lt;/p&gt;

</description>
      <category>ai</category>
      <category>beginners</category>
      <category>basic</category>
      <category>machinelearning</category>
    </item>
    <item>
      <title>Rust Hello World: The Hard and the Smart Way</title>
      <dc:creator>Francesco Ciulla</dc:creator>
      <pubDate>Wed, 26 Nov 2025 05:48:52 +0000</pubDate>
      <link>https://dev.to/francescoxx/rust-hello-world-the-hard-and-the-smart-way-36l7</link>
      <guid>https://dev.to/francescoxx/rust-hello-world-the-hard-and-the-smart-way-36l7</guid>
      <description>&lt;p&gt;How do we make a Hello World in Rust??&lt;/p&gt;

&lt;p&gt;If you prefer watching over reading, you can watch the video version of this bootcamp segment right here:&lt;br&gt;


  &lt;iframe src="https://www.youtube.com/embed/tgJ8d9yQtKY"&gt;
  &lt;/iframe&gt;


&lt;/p&gt;




&lt;h2&gt;
  
  
  The Prerequisites
&lt;/h2&gt;

&lt;p&gt;The first thing you should do is &lt;a href="https://rust-lang.org/tools/install/" rel="noopener noreferrer"&gt;install Rust&lt;/a&gt;. You should end up on a page that guides you through using &lt;code&gt;rustup&lt;/code&gt;, which is an excellent tool for managing Rust versions.&lt;/p&gt;

&lt;p&gt;To verify your installation, type &lt;code&gt;rustc --version&lt;/code&gt; in your terminal.&lt;/p&gt;

&lt;p&gt;Rust is a &lt;strong&gt;compiled&lt;/strong&gt; programming language. &lt;code&gt;rustc&lt;/code&gt; is the compiler, and &lt;code&gt;cargo&lt;/code&gt; is the package manager. If you have both of these, you are good to go.&lt;/p&gt;

&lt;h2&gt;
  
  
  The "Hard Way" (Manual Compilation) 🔨
&lt;/h2&gt;

&lt;p&gt;We will start with a very simple "Hello, World!" because this step is very important to understand what happens behind the scenes.&lt;/p&gt;

&lt;p&gt;This approach uses &lt;code&gt;rustc&lt;/code&gt; directly. This is something you usually don't see in tutorials, but it helps to understand what automated tools do for us later.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Create a new folder and construct a file named &lt;code&gt;main.rs&lt;/code&gt; (&lt;code&gt;.rs&lt;/code&gt; is the extension for Rust files).&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Open it in your favorite IDE (VS Code, RustRover, etc.) and add the code:&lt;br&gt;
&lt;/p&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="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello, world!"&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;/li&gt;
&lt;li&gt;
&lt;p&gt;How do you run this? There isn't a play button. You need to compile it.&lt;br&gt;
Run this command in your terminal:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;rustc main.rs
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;&lt;p&gt;This compiles the code and creates an executable in your folder (e.g., just &lt;code&gt;main&lt;/code&gt; on Linux/macOS or &lt;code&gt;main.exe&lt;/code&gt; on Windows).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Run the executable:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;./main
&lt;span class="c"&gt;# Output: Hello, world!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Why is this the "Hard Way"?&lt;/strong&gt;&lt;br&gt;
If you make a change to the file—for example, adding a crab emoji 🦀 to the string—and save it, running &lt;code&gt;./main&lt;/code&gt; again won't show the change. You must manually run &lt;code&gt;rustc main.rs&lt;/code&gt; again to recompile the changes every single time. This is obviously not ideal for real development.&lt;/p&gt;

&lt;p&gt;I've done this just because I want you to understand what cargo does behind the scenes.&lt;/p&gt;
&lt;h2&gt;
  
  
  The "Smart Way" (Cargo) 🚀
&lt;/h2&gt;

&lt;p&gt;Usually, you will initialize a Rust project using Cargo. Cargo is basically the npm of the Rust world.&lt;/p&gt;

&lt;p&gt;Let's initialize a new project the standard way:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Run the following command to create a new project:&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight shell"&gt;&lt;code&gt;cargo new rust_live_one
&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;Cargo will create the directory structure for you. It creates a &lt;code&gt;src&lt;/code&gt; folder containing &lt;code&gt;main.rs&lt;/code&gt;, sets up a git repository, and creates a &lt;code&gt;Cargo.toml&lt;/code&gt; file (which is similar to &lt;code&gt;package.json&lt;/code&gt; in JS or &lt;code&gt;requirements.txt&lt;/code&gt; in Python).&lt;/p&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;To run this project, you don't use &lt;code&gt;rustc&lt;/code&gt;. You use:&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight shell"&gt;&lt;code&gt;cargo run
&lt;/code&gt;&lt;/pre&gt;




&lt;/li&gt;

&lt;/ol&gt;

&lt;p&gt;If you change your code and run &lt;code&gt;cargo run&lt;/code&gt; again, Cargo realizes the code has changed and recompiles the necessary files automatically before running it. Thank you, Cargo!&lt;/p&gt;

&lt;h3&gt;
  
  
  🦀 Pro Tip: Quiet Mode
&lt;/h3&gt;

&lt;p&gt;By default, Cargo prints outputs like "Compiling..." and "Finished..." before showing your program's output.&lt;/p&gt;

&lt;p&gt;If you want the exact same clean output we had with the manual method, use the quiet flag:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;cargo &lt;span class="nt"&gt;-q&lt;/span&gt; run
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is a handy cheat sheet summarizing the setup process (click the image to watch the video!):&lt;/p&gt;

&lt;p&gt;&lt;a href="https://youtu.be/tgJ8d9yQtKY" rel="noopener noreferrer"&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.amazonaws.com%2Fuploads%2Farticles%2F3czrddi3bpxun3cp9ych.png" alt="hello world infographic"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you prefer watching over reading, you can watch the video version of this bootcamp segment right here:&lt;br&gt;


  &lt;iframe src="https://www.youtube.com/embed/tgJ8d9yQtKY"&gt;
  &lt;/iframe&gt;


&lt;/p&gt;

</description>
      <category>rust</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>programming</category>
    </item>
    <item>
      <title>Rust vs. Your Next JavaScript Framework: Which Should You Learn?</title>
      <dc:creator>Francesco Ciulla</dc:creator>
      <pubDate>Fri, 12 Sep 2025 15:02:30 +0000</pubDate>
      <link>https://dev.to/francescoxx/rust-vs-your-next-javascript-framework-which-should-you-learn-24pj</link>
      <guid>https://dev.to/francescoxx/rust-vs-your-next-javascript-framework-which-should-you-learn-24pj</guid>
      <description>&lt;p&gt;Rust vs. Your Next JavaScript Framework: Which Should You Learn?&lt;/p&gt;

&lt;p&gt;Hey everyone! Today, we're tackling a big question for developers: Should you dive into the world of Rust, or is it smarter to just pick up the next popular JavaScript framework? Let's break it down.&lt;/p&gt;

&lt;p&gt;If you prefer a video version&lt;/p&gt;

&lt;p&gt;  &lt;iframe src="https://www.youtube.com/embed/--uiLxOFDPY"&gt;
  &lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;For nine consecutive years, Rust has been voted the "most admired" programming language in developer surveys. It's like a rock band that keeps topping the charts, but its fan base is system engineers and those who love a good compiler error message. Major companies such as Microsoft, Google, and AWS are adopting it, especially for projects where tricky C++ memory bugs just won't cut it at 3:00 a.m.&lt;/p&gt;

&lt;p&gt;But is it worth your time? Let's find out.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Exactly Is Rust? 🤔
&lt;/h2&gt;

&lt;p&gt;Rust is a systems programming language designed for three main priorities: speed, safety, and concurrency.&lt;/p&gt;

&lt;p&gt;Think of it this way: Rust is like C++ after it went to therapy, worked through its trust issues, and came back with a very strict rule book about relationships (we call them ownership and borrowing). This rule book aims to prevent common bugs and crashes before your code even runs.&lt;/p&gt;

&lt;p&gt;You can use Rust for a wide range of exciting projects, including:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Operating systems&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;WebAssembly (Wasm) applications&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;High-performance backend APIs&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Blockchain projects&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Command-line tools that make you feel like a hacker from the future&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The Good Stuff: Why You'll Love Rust ❤️
&lt;/h2&gt;

&lt;p&gt;Let's explore why you'd want to add Rust to your skillset.&lt;/p&gt;

&lt;p&gt;Blazing Performance: Rust operates at speeds nearly identical to C and C++, making languages like Python look slow in comparison. Your applications will be highly efficient.&lt;/p&gt;

&lt;p&gt;Memory Safety Without a Garbage Collector: Rust guarantees memory safety without needing a garbage collector running in the background. That means no dangling pointers ruining your day and no performance hits from a garbage collector pausing your program.&lt;/p&gt;

&lt;p&gt;Fearless Concurrency: Writing multi-threaded code can be challenging. Rust's ownership rules eliminate many common concurrency bugs at compile time, so you can write parallel code with confidence.&lt;/p&gt;

&lt;p&gt;Amazing Ecosystem and Tooling: Cargo, Rust's package manager and build tool, is a pleasure to work with. It's like npm, but it doesn't fall apart when you look at it the wrong way. The compiler (rustc) and tools like Clippy are also fantastic—they don't just point out your mistakes; they often give you helpful advice and suggest the exact fix.&lt;/p&gt;

&lt;p&gt;The "You Might Regret This" Part: The Challenges 🥵&lt;br&gt;
Before you update your LinkedIn profile, let's talk about the reality. Rust isn't all sunshine and rainbows.&lt;/p&gt;

&lt;p&gt;The Learning Curve is a Cliff: Rust can be tough at first. Concepts like ownership, borrowing, and lifetimes aren't just new; they test your patience. The compiler is notoriously strict.&lt;/p&gt;

&lt;p&gt;Slow Compile Times: Be prepared to wait. Rust compiles slower than your Wi-Fi on an airplane. You might either get used to it or develop a new hobby, like making tea between builds.&lt;/p&gt;

&lt;p&gt;Growing Ecosystem: While Rust's ecosystem is expanding rapidly, you won't always find a ready-made library (crate) for every problem, unlike the large ecosystems of Python or JavaScript.&lt;/p&gt;
&lt;h2&gt;
  
  
  Who Should Learn Rust?
&lt;/h2&gt;

&lt;p&gt;So, is Rust for you?&lt;/p&gt;

&lt;p&gt;✅ You should learn Rust if...&lt;br&gt;
You love control: If you're into understanding exactly what your code does at a low level, Rust is perfect.&lt;/p&gt;

&lt;p&gt;You care about safety and reliability: If you've ever lost production data to a memory leak, Rust will feel like a warm, safe hug.&lt;/p&gt;

&lt;p&gt;You want in-demand skills for the future: Top tech companies are hiring Rust developers now, and demand will only grow.&lt;/p&gt;

&lt;p&gt;❌ You should probably skip Rust (for now) if...&lt;br&gt;
Just want to build an MVP quickly: If your goal is to ship a project by next weekend, stick with Python, JavaScript, or other options. Rust emphasizes correctness over speed of development.&lt;/p&gt;

&lt;p&gt;You dislike strict rules: Rust is a rulebook in programming language form. Breaking a rule will invite the compiler to roast you more than a Stack Overflow comment section.&lt;/p&gt;

&lt;p&gt;You're allergic to semicolons: Sorry, Rust uses plenty of them.&lt;/p&gt;

&lt;p&gt;The Verdict: Is Rust Here to Stay?&lt;br&gt;
Absolutely. Rust is no longer just hype; it's becoming part of modern infrastructure. It's now supported in the Linux kernel, Microsoft is rewriting parts of Windows with it, and even modern JavaScript tools like Deno and Bun are built with Rust.&lt;/p&gt;
&lt;h2&gt;
  
  
  So, should you learn it?
&lt;/h2&gt;

&lt;p&gt;If you're looking for a challenge, want to build high-performance software, and are interested in a language that can significantly enhance your development skills, then yes, learn Rust.&lt;/p&gt;

&lt;p&gt;If you need to get something working by Friday, maybe hold off for now.&lt;/p&gt;

&lt;p&gt;But if you do decide to dive in, remember: the compiler isn't your enemy. It's a very strict friend (or an annoying grandma) who genuinely wants you to succeed.&lt;/p&gt;

&lt;p&gt;If you prefer a video version&lt;/p&gt;

&lt;p&gt;  &lt;iframe src="https://www.youtube.com/embed/--uiLxOFDPY"&gt;
  &lt;/iframe&gt;
&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Open AI Releases Open Models</title>
      <dc:creator>Francesco Ciulla</dc:creator>
      <pubDate>Wed, 06 Aug 2025 04:25:57 +0000</pubDate>
      <link>https://dev.to/francescoxx/open-ai-releases-open-models-1m19</link>
      <guid>https://dev.to/francescoxx/open-ai-releases-open-models-1m19</guid>
      <description>&lt;p&gt;Open AI is now finally Open AI.&lt;/p&gt;

&lt;p&gt;OpenAI has just released two new advanced open-weight models, &lt;strong&gt;gpt-oss-120b&lt;/strong&gt; and &lt;strong&gt;gpt-oss-20b&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;These models are designed for high-end reasoning tasks and offer flexibility for customization and deployment.&lt;/p&gt;

&lt;p&gt;Sam Altman is very bullish about 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.amazonaws.com%2Fuploads%2Farticles%2Fu4ofoi3l2y0f6zpzd5tx.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.amazonaws.com%2Fuploads%2Farticles%2Fu4ofoi3l2y0f6zpzd5tx.png" alt="someday soon something smarter than the smartest person you know will be running on a device in your pocket, helping you with whatever you want. this is a very remarkable thing." width="800" height="216"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  What is an Open-Weight Model?
&lt;/h2&gt;

&lt;p&gt;An &lt;strong&gt;open-weight model&lt;/strong&gt; is an AI, often an LLM, with publicly shared parameters or "weights" learned during training. &lt;/p&gt;

&lt;p&gt;Sharing them allows users to easily run, explore, and customize the model on their own hardware, unlike closed models, which are only accessible through an API with proprietary weights.&lt;/p&gt;




&lt;h2&gt;
  
  
  Two Model Sizes
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;gpt-oss-120b:&lt;/strong&gt; Powerful, large model with 120 billion parameters, specially optimized for smooth performance in data centers and on high-end desktops and laptops.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;gpt-oss-20b:&lt;/strong&gt; Versatile, medium-sized model with 20 billion parameters designed to be accessible to many, efficiently running on most modern desktops and laptops.&lt;/p&gt;

&lt;p&gt;To try out these models, you can visit: &lt;a href="https://gpt-oss.com/" rel="noopener noreferrer"&gt;https://gpt-oss.com/&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Apache 2.0 License
&lt;/h2&gt;

&lt;p&gt;Both models are provided under the &lt;strong&gt;Apache 2.0 license&lt;/strong&gt;, so you can easily use, modify, and share them. This opens up exciting possibilities for us to create, experiment, personalize, and launch commercial applications without worrying about copyleft requirements or patent issues. &lt;/p&gt;

&lt;p&gt;GitHub repository: &lt;a href="https://github.com/openai/gpt-oss" rel="noopener noreferrer"&gt;https://github.com/openai/gpt-oss&lt;/a&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Designed for Agentic Tasks
&lt;/h2&gt;

&lt;p&gt;The claim is that these models are really good at handling complex, multi-step tasks easily and that they shine in following instructions and make great use of their impressive tools, such as web searches and Python coding, to help solve problems more effectively.&lt;/p&gt;




&lt;h2&gt;
  
  
  Deep Customization &amp;amp; Fine-Tuning
&lt;/h2&gt;

&lt;p&gt;Users have granular control over the models' performance. The &lt;strong&gt;reasoning effort&lt;/strong&gt; can be adjusted to low, medium, or high, balancing performance with resource consumption.&lt;/p&gt;

&lt;p&gt;The models support &lt;strong&gt;full-parameter fine-tuning&lt;/strong&gt;, allowing developers to adapt them precisely to specific use cases and achieve optimal results.&lt;/p&gt;

&lt;p&gt;This sounds great, but of course, it needs to be tested.&lt;/p&gt;




&lt;h2&gt;
  
  
  Model Performance
&lt;/h2&gt;

&lt;p&gt;The models' performance seems promising. 📈&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Benchmark&lt;/th&gt;
&lt;th&gt;gpt-oss-120b&lt;/th&gt;
&lt;th&gt;gpt-oss-20b&lt;/th&gt;
&lt;th&gt;OpenAI o3&lt;/th&gt;
&lt;th&gt;OpenAI o4-mini&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Reasoning &amp;amp; Knowledge&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;MMLU&lt;/td&gt;
&lt;td&gt;90.0&lt;/td&gt;
&lt;td&gt;85.3&lt;/td&gt;
&lt;td&gt;93.4&lt;/td&gt;
&lt;td&gt;93.0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;GPQA Diamond&lt;/td&gt;
&lt;td&gt;80.1&lt;/td&gt;
&lt;td&gt;71.5&lt;/td&gt;
&lt;td&gt;83.3&lt;/td&gt;
&lt;td&gt;81.4&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Humanity's Last Exam&lt;/td&gt;
&lt;td&gt;19.0&lt;/td&gt;
&lt;td&gt;17.3&lt;/td&gt;
&lt;td&gt;24.9&lt;/td&gt;
&lt;td&gt;17.7&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Competition math&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;AIME 2024&lt;/td&gt;
&lt;td&gt;96.6&lt;/td&gt;
&lt;td&gt;96.0&lt;/td&gt;
&lt;td&gt;95.2&lt;/td&gt;
&lt;td&gt;98.7&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;AIME 2025&lt;/td&gt;
&lt;td&gt;97.9&lt;/td&gt;
&lt;td&gt;98.7&lt;/td&gt;
&lt;td&gt;98.4&lt;/td&gt;
&lt;td&gt;99.5&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Research blog: &lt;a href="https://openai.com/index/introducing-gpt-oss/" rel="noopener noreferrer"&gt;https://openai.com/index/introducing-gpt-oss/&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Chain-of-Thought (CoT) Access
&lt;/h2&gt;

&lt;p&gt;OpenAI now provides complete access to the model's reasoning process. &lt;/p&gt;

&lt;p&gt;Finally, some transparency. That's great for easier debugging, understanding how the model arrived at an output, and building higher trust in its results.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Official Blog Post:&lt;/strong&gt; &lt;a href="https://openai.com/index/introducing-gpt-oss/" rel="noopener noreferrer"&gt;https://openai.com/index/introducing-gpt-oss/&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;I hope this overview of OpenAI's new open-weight reasoning models has been helpful. These models represent a significant step in making advanced AI more accessible and customizable for developers and researchers. &lt;/p&gt;

&lt;p&gt;If you have any questions or need further information, comment below or contact me directly.&lt;/p&gt;

&lt;p&gt;You can find me here: &lt;a href="https://francescociulla.com" rel="noopener noreferrer"&gt;Francesco Ciulla&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ai</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Should you join a community to learn Rust?</title>
      <dc:creator>Francesco Ciulla</dc:creator>
      <pubDate>Tue, 13 May 2025 08:06:56 +0000</pubDate>
      <link>https://dev.to/francescoxx/should-you-join-a-community-to-learn-rust-j16</link>
      <guid>https://dev.to/francescoxx/should-you-join-a-community-to-learn-rust-j16</guid>
      <description>&lt;p&gt;Short answer: &lt;a href="https://x.com/i/communities/1922156050284679207" rel="noopener noreferrer"&gt;yes&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Over the past 5 years, I've learned a lot about communities.&lt;/p&gt;

&lt;p&gt;I've made many mistakes: I could probably create a 'Community Mistakes 101' crash course! Unfortunately, they didn't teach that in high school, which would have been useful.&lt;/p&gt;

&lt;p&gt;I learned that a community absolutely must have a specific goal. I also learned that while having an open community is good at the beginning, it becomes nearly impossible to handle and moderate long-term unless you dedicate an insane amount of time to it (trust me, I tried) or you are a bored millionaire lying on the couch all day (which is not my case).&lt;/p&gt;

&lt;p&gt;Regarding promotion, I've made the mistake in the past of overpromoting communities I created, simply because I was overjoyed about the new adventure. I now think this approach is wrong. it's better if people discover the community on their own.&lt;/p&gt;

&lt;p&gt;But now I really feel I am ready to do it right.&lt;/p&gt;

&lt;p&gt;So, why am I writing this? The reason is simple: I've just a new community, and I've decided to use Twitter (sorry, X) for it.&lt;/p&gt;

&lt;p&gt;The goal is to introduce curious developers to the Rust programming language and help them understand as much as possible.&lt;/p&gt;

&lt;p&gt;Joining &lt;a href="https://x.com/i/communities/1922156050284679207" rel="noopener noreferrer"&gt;this community&lt;/a&gt; is free, just request to join. The link is in my profile, but if you're lazy and want to be invited, just reply below.&lt;/p&gt;

&lt;p&gt;For now, I am not accepting moderators, but moderators might be chosen from among the active members in the future.&lt;/p&gt;

&lt;p&gt;Let's Crush it !!!🔥&lt;/p&gt;

</description>
      <category>rust</category>
      <category>opensource</category>
      <category>beginners</category>
      <category>community</category>
    </item>
    <item>
      <title>Should you learn Rust as your next programming language?</title>
      <dc:creator>Francesco Ciulla</dc:creator>
      <pubDate>Fri, 09 May 2025 07:29:53 +0000</pubDate>
      <link>https://dev.to/francescoxx/should-i-learn-rust-as-a-my-next-programming-language-ach</link>
      <guid>https://dev.to/francescoxx/should-i-learn-rust-as-a-my-next-programming-language-ach</guid>
      <description>&lt;p&gt;Hi, I want to focus on why you might be interested in learning Rust in this short article.&lt;/p&gt;

&lt;p&gt;If you're reading this, you may already be interested in learning Rust. However, if not, I understand you. I am also very skeptical about learning new technologies. But I am here to try to change your mind. &lt;/p&gt;

&lt;p&gt;Here are some reasons to consider it:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Performance&lt;/strong&gt;: The main reason Rust has become popular is that &lt;strong&gt;it is&lt;/strong&gt; designed to be fast and efficient. This makes it an excellent choice for performance-critical applications. It can often match (or exceed) the performance of C and C++ while providing additional safety features. I believe it strikes a good balance between performance and safety.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Memory Safety&lt;/strong&gt;: Rust is designed to be memory-safe. This helps avoid common programming errors like null pointer dereferences and buffer overflows. Rust achieves this through its ownership system, which enforces strict rules about how memory is accessed and shared. This is a valuable feature for developers who want to write safe and reliable code without worrying about low-level memory management!&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Concurrency&lt;/strong&gt;: Writing concurrent code is usually a nightmare; however, Rust simplifies creating concurrent programs. Its strong type system and ownership model help prevent data races.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Helpful (and annoying) Compiler &amp;amp; Tooling&lt;/strong&gt;: Rust's compiler is well-known for being very helpful. It does not just report random errors (I see you Java), it often explains them thoroughly and suggests how to fix your code. I like to compare it to that old annoying grandma who wants the best for you. You know she's annoying, but she is right. Plus, Rust comes with a fantastic tool called 'Cargo' that makes it easy to manage your project's dependencies and build your programs, simplifying the development process.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Are you not convinced yet? Well, I kept the best shot as the last one:&lt;/p&gt;

&lt;p&gt;Rust has been voted the most-admired programming language in the latest Stack Overflow survey. Do you want to base your future learning choices on what you read on Twitter, what a tech influencer says, or real data? &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.amazonaws.com%2Fuploads%2Farticles%2Fi7vd2oq9aos6ewshsz90.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.amazonaws.com%2Fuploads%2Farticles%2Fi7vd2oq9aos6ewshsz90.png" alt="Stack Overflow Survey"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you are interested in learning Rust, I just &lt;strong&gt;published&lt;/strong&gt; a &lt;a href="https://youtu.be/gAX3Zj-JGE0" rel="noopener noreferrer"&gt;3.5-hour video on YouTube&lt;/a&gt;; it's free and might help you understand the core concepts of the language. Good luck!&lt;/p&gt;

&lt;p&gt;  &lt;iframe src="https://www.youtube.com/embed/gAX3Zj-JGE0"&gt;
  &lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Find me here: &lt;a href="https://francescociulla.com" rel="noopener noreferrer"&gt;https://francescociulla.com&lt;/a&gt;&lt;/p&gt;

</description>
      <category>programming</category>
      <category>opensource</category>
      <category>rust</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Actix Web - The Rust Framework for Web Development - Hello World</title>
      <dc:creator>Francesco Ciulla</dc:creator>
      <pubDate>Tue, 05 Nov 2024 14:19:00 +0000</pubDate>
      <link>https://dev.to/francescoxx/actix-web-the-rust-framework-for-web-development-hello-world-2n2d</link>
      <guid>https://dev.to/francescoxx/actix-web-the-rust-framework-for-web-development-hello-world-2n2d</guid>
      <description>&lt;p&gt;Hi! &lt;a href="https://francescociulla.com" rel="noopener noreferrer"&gt;Francesco&lt;/a&gt; here.&lt;/p&gt;

&lt;p&gt;Today, we will look at Actix, a high-performance framework for building web applications in Rust. &lt;/p&gt;

&lt;p&gt;Actix Web site: &lt;a href="https://actix.rs/" rel="noopener noreferrer"&gt;https://actix.rs/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Whether you’re just starting with Rust or are an experienced developer exploring web capabilities, Actix offers a great way to dive into web development in Rust. &lt;/p&gt;

&lt;p&gt;Known for its speed and efficiency, Actix consistently ranks among the fastest web frameworks, leveraging asynchronous execution by default to handle many requests concurrently. This makes it well-suited for applications requiring responsiveness under load. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://youtu.be/o5IP71BqO58" rel="noopener noreferrer"&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.amazonaws.com%2Fuploads%2Farticles%2F2ppw1scjdajjd2yugffz.png" alt="Actix Hello World" width="800" height="449"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A great plus for JavaScript Developers is that Actix looks similar to Express, so if you are familiar with Express, you will find Actix easy to learn.&lt;/p&gt;

&lt;p&gt;In this article, we’ll build a simple "Hello World" web app with Actix, featuring three routes. This will give us a quick overview of the syntax and allow us to see if Actix might be a good fit for our projects.&lt;/p&gt;

&lt;p&gt;If you prefer a Video version:&lt;br&gt;
&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/o5IP71BqO58"&gt;
&lt;/iframe&gt;
&lt;/p&gt;




&lt;h2&gt;
  
  
  Setting Up the Project
&lt;/h2&gt;

&lt;p&gt;First, let’s create a new Rust project and set up our dependencies.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Create the Project&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;cargo new actix_hello_world
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Add Actix Dependency&lt;/strong&gt;:&lt;br&gt;
Open &lt;code&gt;Cargo.toml&lt;/code&gt; and add the &lt;code&gt;actix-web&lt;/code&gt; dependency:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight toml"&gt;&lt;code&gt;&lt;span class="nn"&gt;[dependencies]&lt;/span&gt;
&lt;span class="py"&gt;actix-web&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"4"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ol&gt;




&lt;p&gt;You project structure should look like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://youtu.be/o5IP71BqO58" rel="noopener noreferrer"&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.amazonaws.com%2Fuploads%2Farticles%2Fxtxigg00s1s9ga8w3nns.png" alt="Actix Hello World" width="800" height="439"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Writing the Code
&lt;/h2&gt;

&lt;p&gt;Next, open &lt;code&gt;main.rs&lt;/code&gt; and start building our application. Here’s the complete 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;use&lt;/span&gt; &lt;span class="nn"&gt;actix_web&lt;/span&gt;&lt;span class="p"&gt;::{&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;post&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;web&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;App&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;HttpServer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Responder&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;HttpResponse&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nd"&gt;#[get(&lt;/span&gt;&lt;span class="s"&gt;"/"&lt;/span&gt;&lt;span class="nd"&gt;)]&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;index&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;impl&lt;/span&gt; &lt;span class="n"&gt;Responder&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nn"&gt;HttpResponse&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="nf"&gt;.body&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello world!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nd"&gt;#[post(&lt;/span&gt;&lt;span class="s"&gt;"/echo"&lt;/span&gt;&lt;span class="nd"&gt;)]&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;echo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;req_body&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="k"&gt;impl&lt;/span&gt; &lt;span class="n"&gt;Responder&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nn"&gt;HttpResponse&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="nf"&gt;.body&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;req_body&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;manual_hello&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;impl&lt;/span&gt; &lt;span class="n"&gt;Responder&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nn"&gt;HttpResponse&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="nf"&gt;.body&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hey there!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nd"&gt;#[actix_web::main]&lt;/span&gt;
&lt;span class="k"&gt;async&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="k"&gt;-&amp;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;io&lt;/span&gt;&lt;span class="p"&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="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nn"&gt;HttpServer&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="p"&gt;{&lt;/span&gt;
        &lt;span class="nn"&gt;App&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;.service&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="nf"&gt;.service&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;echo&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="nf"&gt;.route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/hey"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nn"&gt;web&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.to&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;manual_hello&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="nf"&gt;.bind&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="s"&gt;"127.0.0.1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8080&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;
    &lt;span class="nf"&gt;.run&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;.await&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Code Walkthrough
&lt;/h3&gt;

&lt;p&gt;Let’s break down what each part of this code does:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We define our routes using Actix’s #[get("/")] and #[post("/echo")] attributes. &lt;/li&gt;
&lt;li&gt;Each route maps to an asynchronous function.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;index&lt;/code&gt; function responds with "Hello world!" when accessed at the root (/) via a GET request (we will test it soon).&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;echo&lt;/code&gt; function allows us to send a POST request to /echo. It returns the request body as the response, a simple echo server implementation.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;manual_hello&lt;/code&gt; function is another way to define a route. Instead of an attribute, we add it directly in the App setup using .route("/hey", web::get().to(manual_hello)).&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;main&lt;/code&gt; function is where our server is created and configured. &lt;/li&gt;
&lt;li&gt;Using HttpServer::new, we create an instance of App with our defined routes.&lt;/li&gt;
&lt;li&gt;We then bind it to 127.0.0.1 on port 8080 and run it asynchronously.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Running and Testing the Application
&lt;/h2&gt;

&lt;p&gt;Now, let’s test our Actix app (This might take a while the first time you run it):&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://youtu.be/o5IP71BqO58" rel="noopener noreferrer"&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.amazonaws.com%2Fuploads%2Farticles%2Fay64uzj181jupnsz1hj6.png" alt="Actix Hello World" width="800" height="443"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Testing the Routes
&lt;/h3&gt;

&lt;p&gt;Test GET Requests:&lt;/p&gt;

&lt;p&gt;Open a browser and navigate to &lt;code&gt;http://127.0.0.1:8080/&lt;/code&gt;, and you should see "Hello world!".&lt;/p&gt;

&lt;p&gt;&lt;a href="https://youtu.be/o5IP71BqO58" rel="noopener noreferrer"&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.amazonaws.com%2Fuploads%2Farticles%2Fml2abge7xpy511pbarxr.png" alt="Actix Hello World" width="800" height="444"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For the route /hey, go to &lt;code&gt;http://127.0.0.1:8080/hey&lt;/code&gt;, and you’ll get "Hey there!".&lt;/p&gt;

&lt;p&gt;&lt;a href="https://youtu.be/o5IP71BqO58" rel="noopener noreferrer"&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.amazonaws.com%2Fuploads%2Farticles%2F2oc1v8vr3rlpwue0n82h.png" alt="Actix Hello World" width="800" height="438"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Test POST Requests:&lt;/p&gt;

&lt;p&gt;For the POST /echo route, use a tool like Postman to send a POST request to &lt;a href="http://127.0.0.1:8080/echo" rel="noopener noreferrer"&gt;http://127.0.0.1:8080/echo&lt;/a&gt; with a body. &lt;/p&gt;

&lt;p&gt;For example, if you send:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"test"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="w"&gt;
 &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://youtu.be/o5IP71BqO58" rel="noopener noreferrer"&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.amazonaws.com%2Fuploads%2Farticles%2Fulgktifof91edwt2fh09.png" alt="Actix Hello World" width="800" height="428"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;you’ll receive the same response body back (It work with a simple String, too).&lt;/p&gt;

&lt;h2&gt;
  
  
  Exploring More with Actix
&lt;/h2&gt;

&lt;p&gt;What's my opinion about Actix? Actix’s syntax is concise, and it provides a great introduction to web programming with Rust. As we’ve seen, we can create an app with multiple routes in only a few lines of code. &lt;/p&gt;

&lt;p&gt;Also, Actix’s documentation is extensive and easy to follow, which is always a positive sign for any project.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://actix.rs/" rel="noopener noreferrer"&gt;https://actix.rs/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The Actix GitHub repository has almost 30,000 stars, showing its popularity and active community.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://youtu.be/o5IP71BqO58" rel="noopener noreferrer"&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.amazonaws.com%2Fuploads%2Farticles%2F7uce2b8bvju5bnj9x3nv.png" alt="Actix Hello World" width="800" height="390"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I hope this was helpful, feel free to leave a comment if you have any questions or suggestions.&lt;/p&gt;

&lt;p&gt;If you prefer a Video version:&lt;br&gt;
&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/o5IP71BqO58"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Find me here: &lt;a href="https://francescociulla.com" rel="noopener noreferrer"&gt;Francesco&lt;/a&gt;&lt;/p&gt;

</description>
      <category>programming</category>
      <category>opensource</category>
      <category>rust</category>
      <category>beginners</category>
    </item>
    <item>
      <title>All the Rust Features</title>
      <dc:creator>Francesco Ciulla</dc:creator>
      <pubDate>Fri, 01 Nov 2024 14:48:00 +0000</pubDate>
      <link>https://dev.to/francescoxx/all-the-rust-features-1l1o</link>
      <guid>https://dev.to/francescoxx/all-the-rust-features-1l1o</guid>
      <description>&lt;p&gt;In this article, we’ll explore all (or nearly all) of the key features that make the Rust programming language unique. &lt;/p&gt;

&lt;p&gt;If you want to understand what sets Rust apart, dive into each feature and see how it helps make Rust an efficient, safe, and powerful language.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Fun fact: Rust’s name wasn’t inspired by iron oxidation but by a resilient fungus that thrives in extreme conditions!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Video Version&lt;br&gt;
&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/AiIsdA2i_7o"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;You can find me here: &lt;a href="https://francescociulla.com" rel="noopener noreferrer"&gt;https://francescociulla.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Memory Safety without Garbage Collection
&lt;/h2&gt;

&lt;p&gt;Rust’s memory management is one of its standout features. Unlike languages that rely on a garbage collector, Rust ensures memory safety &lt;strong&gt;at compile time&lt;/strong&gt;. This means there’s no runtime cost for managing memory. Instead, Rust uses a powerful system of &lt;strong&gt;ownership and borrowing&lt;/strong&gt; that checks for potential issues before your program even runs.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example
&lt;/h3&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;data&lt;/span&gt; &lt;span class="o"&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;"Hello, Rust!"&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;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="c1"&gt;// `data` is dropped here automatically, freeing memory&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Rust's compiler guarantees that memory is freed without a garbage collector, resulting in more efficient memory usage.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Ownership System
&lt;/h2&gt;

&lt;p&gt;The ownership model is fundamental to Rust’s safety. Each piece of data in Rust has a single owner, and when the owner goes out of scope, the data is automatically cleaned up. This prevents issues like dangling pointers and memory leaks.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example
&lt;/h3&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;s1&lt;/span&gt; &lt;span class="o"&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;"Hello"&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;s2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// `s1` is moved to `s2` and is no longer accessible&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;s2&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 Rust, once data is "moved," the original variable is no longer valid, ensuring no double frees or memory leaks.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Borrowing and References
&lt;/h2&gt;

&lt;p&gt;When you need multiple parts of your program to access the same data without transferring ownership, borrowing allows this. Rust’s references let you share data safely and lifetimes ensure that borrowed data doesn’t outlive its owner.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example
&lt;/h3&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;s&lt;/span&gt; &lt;span class="o"&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;"Hello"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nf"&gt;print_length&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="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;print_length&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="o"&gt;&amp;amp;&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="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Length: {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;s&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="c1"&gt;// `s` is borrowed, so ownership isn’t transferred&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Borrowing keeps your program safe by controlling who can access what, preventing bugs and runtime errors.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Pattern Matching and Enums
&lt;/h2&gt;

&lt;p&gt;Rust’s pattern matching and enums provide a powerful way to write clear, exhaustive logic that handles all possible cases. This is especially useful in error handling, where enums like Result and Option allow you to handle outcomes explicitly.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;Direction&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;North&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;South&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;East&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;West&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;move_character&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dir&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Direction&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="n"&gt;dir&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nn"&gt;Direction&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;North&lt;/span&gt; &lt;span class="k"&gt;=&amp;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;"Moving North"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="nn"&gt;Direction&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;South&lt;/span&gt; &lt;span class="k"&gt;=&amp;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;"Moving South"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="nn"&gt;Direction&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;East&lt;/span&gt; &lt;span class="k"&gt;=&amp;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;"Moving East"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="nn"&gt;Direction&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;West&lt;/span&gt; &lt;span class="k"&gt;=&amp;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;"Moving West"&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 ensures that every possible case is handled, making your code safe and predictable.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Error Handling with Result and Option
&lt;/h2&gt;

&lt;p&gt;Rust avoids exceptions by making error handling a first-class citizen through Result and Option. These enums enforce safe and predictable handling, so you always consider both successful and error scenarios.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example
&lt;/h3&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;divide&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="nb"&gt;f64&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="nb"&gt;f64&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="nb"&gt;f64&lt;/span&gt;&lt;span class="p"&gt;,&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="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mf"&gt;0.0&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;Err&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;"Cannot divide by zero"&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;Ok&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="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;With Result, the caller must handle both the happy path (Ok) and the potential error (Err), promoting robust error handling.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Type Inference
&lt;/h2&gt;

&lt;p&gt;Although Rust is a statically typed language, you don’t need to specify types everywhere. Rust’s type inference system is smart, allowing for clean and readable code without sacrificing safety.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example
&lt;/h3&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;x&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="c1"&gt;// Rust infers `x` as an `i32`&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;20.5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Rust infers `y` as an `f64`&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;"x: {}, y: {}"&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;y&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;Rust&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;inference&lt;/span&gt; &lt;span class="n"&gt;provides&lt;/span&gt; &lt;span class="n"&gt;both&lt;/span&gt; &lt;span class="n"&gt;flexibility&lt;/span&gt; &lt;span class="n"&gt;and&lt;/span&gt; &lt;span class="n"&gt;clarity&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  7. Traits and Trait Objects
&lt;/h2&gt;

&lt;p&gt;Rust doesn’t use traditional inheritance. Instead, it has traits to define shared behavior, enabling polymorphism without complex inheritance chains. Trait objects, like dyn Trait, provide dynamic dispatch for flexibility.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;trait&lt;/span&gt; &lt;span class="n"&gt;Greet&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;say_hello&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;self&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;Person&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;Greet&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;Person&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;say_hello&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;self&lt;/span&gt;&lt;span class="p"&gt;)&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!"&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;fn&lt;/span&gt; &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;g&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;dyn&lt;/span&gt; &lt;span class="n"&gt;Greet&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="nf"&gt;.say_hello&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;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;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;greet&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;p&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;Traits allow shared behavior without needing inheritance, making Rust’s approach flexible and safe.&lt;/p&gt;

&lt;h2&gt;
  
  
  8. Concurrency without Data Races
&lt;/h2&gt;

&lt;p&gt;Rust’s concurrency model prevents data races. By enforcing Send and Sync traits, Rust enables safe concurrency, letting you write highly concurrent code without bugs from unexpected data races.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example
&lt;/h3&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;thread&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;handle&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;thread&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;spawn&lt;/span&gt;&lt;span class="p"&gt;(||&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 from a thread!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;

    &lt;span class="n"&gt;handle&lt;/span&gt;&lt;span class="nf"&gt;.join&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Rust’s compiler guarantees thread safety, making concurrent programming safer.&lt;/p&gt;

&lt;h2&gt;
  
  
  9. Macros for Code Generation
&lt;/h2&gt;

&lt;p&gt;Rust macros support metaprogramming with two types: declarative macros for pattern matching and procedural macros for generating code. Macros help reduce boilerplate and increase code reuse.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="nd"&gt;macro_rules!&lt;/span&gt; &lt;span class="n"&gt;say_hello&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&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, macro!"&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;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="nd"&gt;say_hello!&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;Macros make code concise and reusable, keeping your Rust codebase efficient.&lt;/p&gt;

&lt;h2&gt;
  
  
  10. Lifetimes and Borrow Checking
&lt;/h2&gt;

&lt;p&gt;Lifetimes prevent dangling references by ensuring references are valid for as long as necessary. They’re key to Rust’s safety guarantees.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example
&lt;/h3&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="n"&gt;longest&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;s1&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="n"&gt;s2&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="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;if&lt;/span&gt; &lt;span class="n"&gt;s1&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;gt;&lt;/span&gt; &lt;span class="n"&gt;s2&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="n"&gt;s1&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="n"&gt;s2&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;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;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;longest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"apple"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"banana"&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;"Longest: {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&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;Rust’s borrow checker ensures that lifetimes are always safe and references are valid.&lt;/p&gt;

&lt;h2&gt;
  
  
  11. Generics and Type System
&lt;/h2&gt;

&lt;p&gt;Rust’s generic system allows you to write flexible, reusable, and type-safe code. With trait bounds, you can define flexible functions and structures without sacrificing safety.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example
&lt;/h3&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="n"&gt;largest&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;PartialOrd&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;list&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="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;T&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;largest&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;list&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="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;list&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;item&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;largest&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;largest&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;item&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;largest&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Generics make it easy to write safe, reusable code without duplicating functionality.&lt;/p&gt;

&lt;h2&gt;
  
  
  12. Unsafe Code
&lt;/h2&gt;

&lt;p&gt;Rust is safe by default, but in cases where performance or low-level access is needed, unsafe code lets you bypass Rust’s safety checks. Use it sparingly.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example
&lt;/h3&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;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;unsafe&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;"Value at x: {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;x&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;Unsafe&lt;/span&gt; &lt;span class="n"&gt;code&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="n"&gt;powerful&lt;/span&gt; &lt;span class="n"&gt;but&lt;/span&gt; &lt;span class="n"&gt;requires&lt;/span&gt; &lt;span class="n"&gt;caution&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt; &lt;span class="n"&gt;bypasses&lt;/span&gt; &lt;span class="n"&gt;Rust&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="n"&gt;guarantees&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  13. Async/Await for Concurrency
&lt;/h2&gt;

&lt;p&gt;With async/await, Rust provides non-blocking concurrency, useful for high-performance I/O operations.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example
&lt;/h3&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;tokio&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;time&lt;/span&gt;&lt;span class="p"&gt;::{&lt;/span&gt;&lt;span class="n"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Duration&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nd"&gt;#[tokio::main]&lt;/span&gt;
&lt;span class="k"&gt;async&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="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Starting..."&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nf"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nn"&gt;Duration&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from_secs&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="k"&gt;.await&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;"Done!"&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;Async programming in Rust allows efficient and scalable applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  14. Cargo for Dependency and Package Management
&lt;/h2&gt;

&lt;p&gt;Cargo is Rust’s build system and package manager, helping manage dependencies, build projects, and run tests.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;cargo new my_project
&lt;span class="nb"&gt;cd &lt;/span&gt;my_project
cargo build
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Cargo simplifies project management, and the extensive ecosystem of crates makes development in Rust easy.&lt;/p&gt;

&lt;p&gt;Here is a video Version:&lt;br&gt;
&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/AiIsdA2i_7o"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;You can find me here: &lt;a href="https://francescociulla" rel="noopener noreferrer"&gt;https://francescociulla&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>opensource</category>
      <category>rust</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Iterators in Rust - Map, Filter, Reduce</title>
      <dc:creator>Francesco Ciulla</dc:creator>
      <pubDate>Tue, 15 Oct 2024 13:34:16 +0000</pubDate>
      <link>https://dev.to/francescoxx/iterators-in-rust-2o0b</link>
      <guid>https://dev.to/francescoxx/iterators-in-rust-2o0b</guid>
      <description>&lt;p&gt;Iterators in Rust provide a powerful and flexible way to process data efficiently by transforming, filtering, and aggregating elements in a collection. &lt;/p&gt;

&lt;p&gt;Unlike traditional loops, Rust’s iterators are lazy—meaning they don't perform any actions until explicitly instructed to. This laziness makes iterators efficient because they only evaluate elements when needed, often combining multiple transformations into a single pass over the data.&lt;/p&gt;

&lt;p&gt;In this lesson, we’ll dive into the core of Rust's iterator system and explore how to use methods like .map(), .filter(), and .fold() (similar to reduce) to create expressive, functional code.&lt;/p&gt;

&lt;p&gt;Video Version&lt;br&gt;
&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/Dtkv5i2nelk"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;All the code is available on &lt;a href="https://youtube.com/live/Dtkv5i2nelk" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt; (link in video description)&lt;/p&gt;

&lt;p&gt;You can find me here: &lt;a href="https://francescociulla.com" rel="noopener noreferrer"&gt;https://francescociulla.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction to Iterators
&lt;/h2&gt;

&lt;p&gt;The iterator pattern allows you to perform tasks on a sequence of items in turn. An iterator handles the logic of moving from one item to the next and determines when the sequence has finished, freeing you from implementing that logic manually. In Rust, iterators are lazy, meaning they don’t do any work until you call methods that consume the iterator.&lt;/p&gt;

&lt;p&gt;For 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="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;In this code, we create an iterator over a vector using the .iter() method. We then use a for loop to iterate over each value in the iterator and print it. The iterator pattern allows us to abstract away the logic of iterating through the vector, making our code more readable and expressive.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Iterator Trait and the next Method
&lt;/h2&gt;

&lt;p&gt;The Iterator trait is the core trait for iterators in Rust. It defines the behavior of the .next() method, which returns an Option containing the next value in the sequence. When the iterator is finished, .next() returns None.&lt;/p&gt;

&lt;p&gt;All iterators in Rust implement the Iterator trait, which defines a single required method: next.&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;type Item: This is an associated type that specifies the type of items the iterator will yield.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;next(): Advances the iterator and returns the next item, or None when the iterator is exhausted.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For 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="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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this code, we create an iterator over a vector and call the .next() method to get the next value in the sequence. We use the assert_eq! macro to check that the values returned by .next() are correct.&lt;/p&gt;

&lt;h2&gt;
  
  
  Core Types of Iterators
&lt;/h2&gt;

&lt;p&gt;Rust provides three main types of iterators: consuming adaptors, iterator adaptors, and iterators themselves. Consuming adaptors take ownership of the iterator and consume it, producing a single value. Iterator adaptors take an iterator and return a new iterator with a different behavior. Iterators themselves are the base trait for all iterators and define the behavior of the .next() method.&lt;/p&gt;

&lt;p&gt;The three main types of iterators are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;iter(): This method creates an iterator that borrows each element of the collection.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;into_iter(): This method creates an iterator that takes ownership of each element of the collection.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;iter_mut(): This method creates an iterator that mutably borrows each element of the collection.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's see the purpose, ownership and use case of each iterator type:&lt;/p&gt;

&lt;h3&gt;
  
  
  iter()
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Purpose: This method creates an iterator that borrows each element of the collection.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Ownership: The original collection remains intact after calling iter().&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use Case: Use iter() when you want to iterate over the collection without taking ownership of it.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For 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;let&lt;/span&gt; &lt;span class="n"&gt;numbers&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;for&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;numbers&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;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;num&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Outputs each number&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;"after iter: {:?}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Outputs: [1, 2, 3]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  iter_mut()
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Purpose: This method creates an iterator that mutably borrows each element of the collection.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Ownership: The original collection remains intact after calling iter_mut().&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use Case: Use iter_mut() when you want to iterate over the collection and modify its elements.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For 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;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;numbers&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;for&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="nf"&gt;.iter_mut&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="o"&gt;*&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;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Mutates each element by adding 1&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;num&lt;/span&gt;&lt;span class="p"&gt;);&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;numbers&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Outputs: [2, 3, 4]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  into_iter()
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Purpose: This method creates an iterator that takes ownership of each element of the collection.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Ownership: The original collection is consumed after calling into_iter().&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use Case: Use into_iter() when you want to move each element out of the collection.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For 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;let&lt;/span&gt; &lt;span class="n"&gt;numbers&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;for&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="nf"&gt;.into_iter&lt;/span&gt;&lt;span class="p"&gt;()&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;num&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Outputs each number&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// println!("{:?}", numbers); // Error: `numbers` is no longer accessible&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Summary table of iterator types:
&lt;/h3&gt;

&lt;p&gt;Here is a summary table of the three main types of iterators in Rust:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Iterator Type&lt;/th&gt;
&lt;th&gt;Purpose&lt;/th&gt;
&lt;th&gt;Ownership&lt;/th&gt;
&lt;th&gt;Use Case&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;iter()&lt;/td&gt;
&lt;td&gt;Creates an iterator that borrows each element of the collection.&lt;/td&gt;
&lt;td&gt;Original collection remains intact.&lt;/td&gt;
&lt;td&gt;Iterate over the collection without taking ownership.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;iter_mut()&lt;/td&gt;
&lt;td&gt;Creates an iterator that mutably borrows each element of the collection.&lt;/td&gt;
&lt;td&gt;Original collection remains intact.&lt;/td&gt;
&lt;td&gt;Iterate over the collection and modify its elements.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;into_iter()&lt;/td&gt;
&lt;td&gt;Creates an iterator that takes ownership of each element of the collection.&lt;/td&gt;
&lt;td&gt;Original collection is consumed.&lt;/td&gt;
&lt;td&gt;Move each element out of the collection.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Methods to Modify or Consume Iterators: .map(), .filter(), and .fold()
&lt;/h2&gt;

&lt;p&gt;Rust provides a variety of methods to modify or consume iterators, allowing you to transform, filter, and aggregate elements efficiently. These methods are called iterator adaptors and are chained together to create expressive, functional code.&lt;/p&gt;

&lt;p&gt;Some common iterator adaptors include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;.map(): Transforms each element in the iterator.&lt;/li&gt;
&lt;li&gt;.filter(): Filters elements based on a predicate function.&lt;/li&gt;
&lt;li&gt;.fold(): Aggregates elements into a single value.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's explore each of these methods in more detail:&lt;/p&gt;

&lt;h3&gt;
  
  
  .map() - Transforming Elements
&lt;/h3&gt;

&lt;p&gt;The .map() method transforms each element in the iterator by applying a closure to it. The closure takes an element as input and returns a new value, which is then yielded by the 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;numbers&lt;/span&gt; &lt;span class="o"&gt;=&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="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&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;let&lt;/span&gt; &lt;span class="n"&gt;squares&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;numbers&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="o"&gt;&amp;amp;&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="n"&gt;x&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="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Map - Squares: {:?}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;squares&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Outputs: [1, 4, 9, 16, 25]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this code, we use the .map() method to square each element in the numbers array. The closure |&amp;amp;x| x * x squares the input x, and the resulting values are collected into a new vector.&lt;/p&gt;

&lt;h3&gt;
  
  
  .filter() - Filtering Elements
&lt;/h3&gt;

&lt;p&gt;The .filter() method filters elements in the iterator based on a predicate function. The closure takes an element as input and returns a boolean value indicating whether the element should be included in the output.&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;numbers&lt;/span&gt; &lt;span class="o"&gt;=&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="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&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;let&lt;/span&gt; &lt;span class="n"&gt;evens&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;numbers&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;.filter&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;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;2&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;.collect&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;"Filter - Evens: {:?}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;evens&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Outputs: [2, 4]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this code, we use the .filter() method to select only the even numbers from the numbers array. The closure |&amp;amp;x| x % 2 == 0 checks if the input x is even, and the resulting values are collected into a new vector.&lt;/p&gt;

&lt;h3&gt;
  
  
  .fold() - Aggregating Elements
&lt;/h3&gt;

&lt;p&gt;The .fold() method aggregates elements in the iterator into a single value. It takes an initial value and a closure that combines the current accumulator value with each element in the iterator. It's similar to the reduce function in other languages.&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;numbers&lt;/span&gt; &lt;span class="o"&gt;=&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="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&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;let&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;numbers&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;.fold&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="n"&gt;acc&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;x&lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="n"&gt;acc&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;x&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;"Fold - Sum: {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Outputs: 15&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this code, we use the .fold() method to calculate the sum of all elements in the numbers array. The initial value 0 is passed as the first argument, and the closure |acc, &amp;amp;x| acc + x adds each element x to the accumulator acc.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;Rust’s iterators provide a flexible way to process data efficiently by allowing transformations, filtering, and aggregations over collections. Key takeaways:&lt;/p&gt;

&lt;p&gt;Laziness: Iterators do nothing until explicitly consumed.&lt;/p&gt;

&lt;p&gt;Ownership and Mutability Control: Choose between iter(), iter_mut(), and into_iter() for precise control over element handling.&lt;/p&gt;

&lt;p&gt;Functional Methods: Methods like .map(), .filter(), and .fold() allow expressive, functional transformations.&lt;/p&gt;

&lt;p&gt;Iterators give fine-grained control over data ownership and mutability during iteration, making Rust efficient and powerful in processing collections.&lt;/p&gt;

&lt;p&gt;Video Version&lt;br&gt;
&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/Dtkv5i2nelk"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;All the code is available on &lt;a href="https://youtube.com/live/Dtkv5i2nelk" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt; (link in video description)&lt;/p&gt;

&lt;p&gt;You can find me here: &lt;a href="https://francescociulla.com" rel="noopener noreferrer"&gt;https://francescociulla.com&lt;/a&gt;&lt;/p&gt;

</description>
      <category>programming</category>
      <category>opensource</category>
      <category>rust</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Iterators in Rust</title>
      <dc:creator>Francesco Ciulla</dc:creator>
      <pubDate>Tue, 15 Oct 2024 13:34:16 +0000</pubDate>
      <link>https://dev.to/francescoxx/iterators-in-rust-fm</link>
      <guid>https://dev.to/francescoxx/iterators-in-rust-fm</guid>
      <description>&lt;p&gt;Iterators in Rust provide a powerful and flexible way to process data efficiently by transforming, filtering, and aggregating elements in a collection. &lt;/p&gt;

&lt;p&gt;Unlike traditional loops, Rust’s iterators are lazy—meaning they don't perform any actions until explicitly instructed to. This laziness makes iterators efficient because they only evaluate elements when needed, often combining multiple transformations into a single pass over the data.&lt;/p&gt;

&lt;p&gt;In this lesson, we’ll dive into the core of Rust's iterator system and explore how to use methods like .map(), .filter(), and .fold() (similar to reduce) to create expressive, functional code.&lt;/p&gt;

&lt;p&gt;Video Version&lt;br&gt;
&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/Dtkv5i2nelk"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;All the code is available on &lt;a href="https://youtube.com/live/Dtkv5i2nelk" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt; (link in video description)&lt;/p&gt;

&lt;p&gt;You can find me here: &lt;a href="https://francescociulla.com" rel="noopener noreferrer"&gt;https://francescociulla.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction to Iterators
&lt;/h2&gt;

&lt;p&gt;The iterator pattern allows you to perform tasks on a sequence of items in turn. An iterator handles the logic of moving from one item to the next and determines when the sequence has finished, freeing you from implementing that logic manually. In Rust, iterators are lazy, meaning they don’t do any work until you call methods that consume the iterator.&lt;/p&gt;

&lt;p&gt;For 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="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;In this code, we create an iterator over a vector using the .iter() method. We then use a for loop to iterate over each value in the iterator and print it. The iterator pattern allows us to abstract away the logic of iterating through the vector, making our code more readable and expressive.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Iterator Trait and the next Method
&lt;/h2&gt;

&lt;p&gt;The Iterator trait is the core trait for iterators in Rust. It defines the behavior of the .next() method, which returns an Option containing the next value in the sequence. When the iterator is finished, .next() returns None.&lt;/p&gt;

&lt;p&gt;All iterators in Rust implement the Iterator trait, which defines a single required method: next.&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;type Item: This is an associated type that specifies the type of items the iterator will yield.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;next(): Advances the iterator and returns the next item, or None when the iterator is exhausted.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For 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="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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this code, we create an iterator over a vector and call the .next() method to get the next value in the sequence. We use the assert_eq! macro to check that the values returned by .next() are correct.&lt;/p&gt;

&lt;h2&gt;
  
  
  Core Types of Iterators
&lt;/h2&gt;

&lt;p&gt;Rust provides three main types of iterators: consuming adaptors, iterator adaptors, and iterators themselves. Consuming adaptors take ownership of the iterator and consume it, producing a single value. Iterator adaptors take an iterator and return a new iterator with a different behavior. Iterators themselves are the base trait for all iterators and define the behavior of the .next() method.&lt;/p&gt;

&lt;p&gt;The three main types of iterators are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;iter(): This method creates an iterator that borrows each element of the collection.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;into_iter(): This method creates an iterator that takes ownership of each element of the collection.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;iter_mut(): This method creates an iterator that mutably borrows each element of the collection.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's see the purpose, ownership and use case of each iterator type:&lt;/p&gt;

&lt;h3&gt;
  
  
  iter()
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Purpose: This method creates an iterator that borrows each element of the collection.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Ownership: The original collection remains intact after calling iter().&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use Case: Use iter() when you want to iterate over the collection without taking ownership of it.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For 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;let&lt;/span&gt; &lt;span class="n"&gt;numbers&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;for&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;numbers&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;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;num&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Outputs each number&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;"after iter: {:?}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Outputs: [1, 2, 3]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  iter_mut()
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Purpose: This method creates an iterator that mutably borrows each element of the collection.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Ownership: The original collection remains intact after calling iter_mut().&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use Case: Use iter_mut() when you want to iterate over the collection and modify its elements.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For 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;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;numbers&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;for&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="nf"&gt;.iter_mut&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="o"&gt;*&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;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Mutates each element by adding 1&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;num&lt;/span&gt;&lt;span class="p"&gt;);&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;numbers&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Outputs: [2, 3, 4]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  into_iter()
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Purpose: This method creates an iterator that takes ownership of each element of the collection.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Ownership: The original collection is consumed after calling into_iter().&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use Case: Use into_iter() when you want to move each element out of the collection.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For 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;let&lt;/span&gt; &lt;span class="n"&gt;numbers&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;for&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="nf"&gt;.into_iter&lt;/span&gt;&lt;span class="p"&gt;()&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;num&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Outputs each number&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// println!("{:?}", numbers); // Error: `numbers` is no longer accessible&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Summary table of iterator types:
&lt;/h3&gt;

&lt;p&gt;Here is a summary table of the three main types of iterators in Rust:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Iterator Type&lt;/th&gt;
&lt;th&gt;Purpose&lt;/th&gt;
&lt;th&gt;Ownership&lt;/th&gt;
&lt;th&gt;Use Case&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;iter()&lt;/td&gt;
&lt;td&gt;Creates an iterator that borrows each element of the collection.&lt;/td&gt;
&lt;td&gt;Original collection remains intact.&lt;/td&gt;
&lt;td&gt;Iterate over the collection without taking ownership.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;iter_mut()&lt;/td&gt;
&lt;td&gt;Creates an iterator that mutably borrows each element of the collection.&lt;/td&gt;
&lt;td&gt;Original collection remains intact.&lt;/td&gt;
&lt;td&gt;Iterate over the collection and modify its elements.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;into_iter()&lt;/td&gt;
&lt;td&gt;Creates an iterator that takes ownership of each element of the collection.&lt;/td&gt;
&lt;td&gt;Original collection is consumed.&lt;/td&gt;
&lt;td&gt;Move each element out of the collection.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Methods to Modify or Consume Iterators: .map(), .filter(), and .fold()
&lt;/h2&gt;

&lt;p&gt;Rust provides a variety of methods to modify or consume iterators, allowing you to transform, filter, and aggregate elements efficiently. These methods are called iterator adaptors and are chained together to create expressive, functional code.&lt;/p&gt;

&lt;p&gt;Some common iterator adaptors include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;.map(): Transforms each element in the iterator.&lt;/li&gt;
&lt;li&gt;.filter(): Filters elements based on a predicate function.&lt;/li&gt;
&lt;li&gt;.fold(): Aggregates elements into a single value.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's explore each of these methods in more detail:&lt;/p&gt;

&lt;h3&gt;
  
  
  .map() - Transforming Elements
&lt;/h3&gt;

&lt;p&gt;The .map() method transforms each element in the iterator by applying a closure to it. The closure takes an element as input and returns a new value, which is then yielded by the 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;numbers&lt;/span&gt; &lt;span class="o"&gt;=&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="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&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;let&lt;/span&gt; &lt;span class="n"&gt;squares&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;numbers&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="o"&gt;&amp;amp;&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="n"&gt;x&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="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Map - Squares: {:?}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;squares&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Outputs: [1, 4, 9, 16, 25]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this code, we use the .map() method to square each element in the numbers array. The closure |&amp;amp;x| x * x squares the input x, and the resulting values are collected into a new vector.&lt;/p&gt;

&lt;h3&gt;
  
  
  .filter() - Filtering Elements
&lt;/h3&gt;

&lt;p&gt;The .filter() method filters elements in the iterator based on a predicate function. The closure takes an element as input and returns a boolean value indicating whether the element should be included in the output.&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;numbers&lt;/span&gt; &lt;span class="o"&gt;=&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="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&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;let&lt;/span&gt; &lt;span class="n"&gt;evens&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;numbers&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;.filter&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;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;2&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;.collect&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;"Filter - Evens: {:?}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;evens&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Outputs: [2, 4]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this code, we use the .filter() method to select only the even numbers from the numbers array. The closure |&amp;amp;x| x % 2 == 0 checks if the input x is even, and the resulting values are collected into a new vector.&lt;/p&gt;

&lt;h3&gt;
  
  
  .fold() - Aggregating Elements
&lt;/h3&gt;

&lt;p&gt;The .fold() method aggregates elements in the iterator into a single value. It takes an initial value and a closure that combines the current accumulator value with each element in the iterator. It's similar to the reduce function in other languages.&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;numbers&lt;/span&gt; &lt;span class="o"&gt;=&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="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&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;let&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;numbers&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;.fold&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="n"&gt;acc&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;x&lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="n"&gt;acc&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;x&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;"Fold - Sum: {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Outputs: 15&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this code, we use the .fold() method to calculate the sum of all elements in the numbers array. The initial value 0 is passed as the first argument, and the closure |acc, &amp;amp;x| acc + x adds each element x to the accumulator acc.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;Rust’s iterators provide a flexible way to process data efficiently by allowing transformations, filtering, and aggregations over collections. Key takeaways:&lt;/p&gt;

&lt;p&gt;Laziness: Iterators do nothing until explicitly consumed.&lt;/p&gt;

&lt;p&gt;Ownership and Mutability Control: Choose between iter(), iter_mut(), and into_iter() for precise control over element handling.&lt;/p&gt;

&lt;p&gt;Functional Methods: Methods like .map(), .filter(), and .fold() allow expressive, functional transformations.&lt;/p&gt;

&lt;p&gt;Iterators give fine-grained control over data ownership and mutability during iteration, making Rust efficient and powerful in processing collections.&lt;/p&gt;

&lt;p&gt;Video Version&lt;br&gt;
&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/Dtkv5i2nelk"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;All the code is available on &lt;a href="https://youtube.com/live/Dtkv5i2nelk" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt; (link in video description)&lt;/p&gt;

&lt;p&gt;You can find me here: &lt;a href="https://francescociulla.com" rel="noopener noreferrer"&gt;https://francescociulla.com&lt;/a&gt;&lt;/p&gt;

</description>
      <category>programming</category>
      <category>opensource</category>
      <category>rust</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
