<?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: Fady GA 😎</title>
    <description>The latest articles on DEV Community by Fady GA 😎 (@fadygrab).</description>
    <link>https://dev.to/fadygrab</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%2F1006587%2Fbb8be8a4-85ea-4403-aa9e-a0e03e416a30.png</url>
      <title>DEV Community: Fady GA 😎</title>
      <link>https://dev.to/fadygrab</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/fadygrab"/>
    <language>en</language>
    <item>
      <title>How I've implemented the Medallion architecture using Apache Spark and Apache Hdoop</title>
      <dc:creator>Fady GA 😎</dc:creator>
      <pubDate>Mon, 17 Jun 2024 13:35:05 +0000</pubDate>
      <link>https://dev.to/fadygrab/how-ive-implemented-the-medallion-architecture-using-apache-spark-and-apache-hdoop-3lmb</link>
      <guid>https://dev.to/fadygrab/how-ive-implemented-the-medallion-architecture-using-apache-spark-and-apache-hdoop-3lmb</guid>
      <description>&lt;h2&gt;
  
  
  Table of contents
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;What is this project all about&lt;/li&gt;
&lt;li&gt;
System architecture

&lt;ul&gt;
&lt;li&gt;The Retail app&lt;/li&gt;
&lt;li&gt;Apache Spark Cluster&lt;/li&gt;
&lt;li&gt;The Development Jupyter server&lt;/li&gt;
&lt;li&gt;The Orchestrator&lt;/li&gt;
&lt;li&gt;Spark incremental database loading&lt;/li&gt;
&lt;li&gt;Hadoop cluster&lt;/li&gt;
&lt;li&gt;The dashboard app&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

Spark Operations

&lt;ul&gt;
&lt;li&gt;Bronze stage&lt;/li&gt;
&lt;li&gt;Silver stage&lt;/li&gt;
&lt;li&gt;Gold stage&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Exposed ports and services:&lt;/li&gt;

&lt;li&gt;Instructions&lt;/li&gt;

&lt;li&gt;Stopping the project&lt;/li&gt;

&lt;li&gt;Things to consider if you want to make this into real-world project&lt;/li&gt;

&lt;li&gt;Key takeaways&lt;/li&gt;

&lt;li&gt;References&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  What is this project all about
&lt;/h2&gt;

&lt;p&gt;In this project, I'm exploring the &lt;em&gt;&lt;a href="https://learn.microsoft.com/en-us/azure/databricks/lakehouse/medallion" rel="noopener noreferrer"&gt;Medallion Architecture&lt;/a&gt;&lt;/em&gt; which is a data design pattern that organizes data into different layers based on structure and/or quality. I'm creating a fictional scenario where a large enterprise that has several branches across the country. Each branch receives purchase orders from an app and deliver the goods to their customers. The enterprise wants to identify the branch that receives the most purchase requests and the branch that has the minimum average delivery time.&lt;br&gt;&lt;br&gt;
To achieve that, I've used &lt;a href="https://spark.apache.org/" rel="noopener noreferrer"&gt;Apache Spark&lt;/a&gt; as a distributed compute engine and &lt;a href="https://hadoop.apache.org/" rel="noopener noreferrer"&gt;Apache Hadoop&lt;/a&gt;, in particular HDFS, as my data storage layer. Apache Spark ingest, processes, and stores the app's data on HDFS to be served to a custom dashboard app. You can find all about it, in &lt;a href="https://github.com/FadyGrAb/data-engineering-mini-projects/tree/main/mini-porjects/03-medallion-architecture-with-spark-and-hadoop" rel="noopener noreferrer"&gt;this Github repo&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fwzv7eb6ynbl1xau94s5b.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fwzv7eb6ynbl1xau94s5b.jpg" alt="project"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  System architecture
&lt;/h2&gt;

&lt;h3&gt;
  
  
  The Retail app
&lt;/h3&gt;

&lt;p&gt;I've simulated the retail app with a python script that randomly generates purchases and handle their lifecycle as NEW -&amp;gt; INTRANSIT -&amp;gt; DELIVERED. The app then stores that in a &lt;a href="https://www.postgresql.org/" rel="noopener noreferrer"&gt;Postgresql&lt;/a&gt; database named &lt;em&gt;retail&lt;/em&gt; that has two tables, &lt;em&gt;orders&lt;/em&gt; and &lt;em&gt;ordershistory&lt;/em&gt;. Separate users with appropriate privileges are created to the app to write the data to the database and to Spark, to read the data. The database is initiated with &lt;code&gt;postgresql/init.sql&lt;/code&gt; script.&lt;/p&gt;

&lt;h3&gt;
  
  
  Apache Spark Cluster
&lt;/h3&gt;

&lt;p&gt;A &lt;a href="https://spark.apache.org/docs/latest/spark-standalone.html" rel="noopener noreferrer"&gt;standalone &lt;/a&gt; cluster with one master none (spark-master) and two worker nodes (spark-worker-1 and spark-worker-2). The REST API endpoint and history server are enabled. I've build its docker image and didn't use any image from Docker Hub.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Development Jupyter server
&lt;/h3&gt;

&lt;p&gt;A jupyter server on the Master Node of the spark cluster running on port 8888 that is exposed to localhost in the Docker compose file. This is how I developed the pipeline's PySpark scripts. This allowed rapid and convenient development for the Spark jobs. The server is configures to have a password &lt;code&gt;pass&lt;/code&gt; that can be changed from &lt;code&gt;spark/jupyter/jupyter_notebook_config.py&lt;/code&gt; or via &lt;code&gt;JUPYTER_NOTEBOOK_PASSWORD&lt;/code&gt; if passed to the Docker compose file.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Orchestrator
&lt;/h3&gt;

&lt;p&gt;A python script that communicates with Apache Spark via its REST API endpoint to submit &lt;em&gt;PySpark&lt;/em&gt; scripts stored on HDFS in &lt;code&gt;hdfs:&amp;lt;namenode&amp;gt;/jobs/*&lt;/code&gt; and "orchestrate" the pipeline &lt;em&gt;Ingestion&lt;/em&gt; -&amp;gt; &lt;em&gt;Transformation&lt;/em&gt; -&amp;gt; &lt;em&gt;Serving&lt;/em&gt; via Spark's drivers status API. All the PySpark files are in &lt;code&gt;spark/pipeline/&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Spark incremental database loading
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;ingestion.py&lt;/code&gt; script implements a &lt;em&gt;bookmarking&lt;/em&gt; mechanism which allows Spark to check for and sets bookmarks during the database data ingestion so it only loads new data and not to strain the app's databases with long running queries each time the pipeline is executed. The bookmark is stored in HDFS in &lt;code&gt;hdfs:&amp;lt;namenode&amp;gt;/data/bronze/bookmark&lt;/code&gt; as JSON.&lt;/p&gt;

&lt;h3&gt;
  
  
  Hadoop cluster
&lt;/h3&gt;

&lt;p&gt;The official image on Docker Hub with minimal configuration changes on &lt;code&gt;hadoop/config&lt;/code&gt; where WebHDFS is enabled.&lt;/p&gt;

&lt;h3&gt;
  
  
  The dashboard app
&lt;/h3&gt;

&lt;p&gt;A Flask app the read the csv file stored in the &lt;em&gt;Gold&lt;/em&gt; folder in HDFS and outputs the resulting data as graphs. It meant to be the data consumer for this pipeline where business users can get their metrics.&lt;/p&gt;

&lt;h2&gt;
  
  
  Spark Operations
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Bronze stage
&lt;/h3&gt;

&lt;p&gt;Spark is incrementally ingesting the &lt;em&gt;orders&lt;/em&gt; and &lt;em&gt;ordershistory&lt;/em&gt; table partitions by date in &lt;em&gt;parquet&lt;/em&gt; format in the &lt;em&gt;Bronze&lt;/em&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Silver stage
&lt;/h3&gt;

&lt;p&gt;Spark joins the two table from the &lt;em&gt;Bronze&lt;/em&gt; stage and filter out "INTRANSIT" records and stores them in the &lt;em&gt;Silver&lt;/em&gt; directory in &lt;em&gt;parquet&lt;/em&gt; format.&lt;/p&gt;

&lt;h3&gt;
  
  
  Gold stage
&lt;/h3&gt;

&lt;p&gt;Spark pivot the joined table to get one record per order that contains the order date and delivery date, subtracts then and get the delivery time in hours.&lt;br&gt;
Then aggregates the result to get the orders count and average delivery time per branch. Finally, it stores them as CSV files in the &lt;em&gt;Gold&lt;/em&gt; directory.&lt;/p&gt;

&lt;h2&gt;
  
  
  Exposed ports and services:
&lt;/h2&gt;

&lt;p&gt;I've configured the the majority of the the services in the Docker compose file to be accessible from localhost:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Dashboard App&lt;/strong&gt;: &lt;a href="http://localhost:5000" rel="noopener noreferrer"&gt;http://localhost:5000&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Spark master node UI&lt;/strong&gt;: &lt;a href="http://localhost:8080" rel="noopener noreferrer"&gt;http://localhost:8080&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Spark worker node 1 UI&lt;/strong&gt;: &lt;a href="http://localhost:8081" rel="noopener noreferrer"&gt;http://localhost:8081&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Spark worker node 2 UI&lt;/strong&gt;: &lt;a href="http://localhost:8082" rel="noopener noreferrer"&gt;http://localhost:8082&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Spark history server&lt;/strong&gt;: &lt;a href="http://localhost:18080" rel="noopener noreferrer"&gt;http://localhost:18080&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Spark running driver UI&lt;/strong&gt;: &lt;a href="http://localhost:4040" rel="noopener noreferrer"&gt;http://localhost:4040&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Jupyter server running on Spark cluster&lt;/strong&gt;: &lt;a href="http://localhost:8888" rel="noopener noreferrer"&gt;http://localhost:8888&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Hadoop namenode UI&lt;/strong&gt;: &lt;a href="http://localhost:9870" rel="noopener noreferrer"&gt;http://localhost:9870&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Instructions
&lt;/h2&gt;

&lt;p&gt;I assume that you have Python and Docker installed in your system.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Go to the project's root directory.&lt;/li&gt;
&lt;li&gt;Create a Python virtual env and install the requirements in &lt;code&gt;./requirements.txt&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Build the custom Spark docker image (this could take a while)
```bash
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;docker build spark:homemade ./spark/&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- Build the Dashboard app docker image
  ```bash


  docker build dashboard:homemade ./dashboard/


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;Spin up the system
```bash
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;docker compose up -d&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- Activate the virtual env and run the retail app. You will see simulated transactions in your terminal.
  ```bash


  python ./retailapp/retailapp.py


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;Open a new terminal and initiate HDFS directories and copy the pipeline PySpark scripts to HDFS
```bash
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;docker compose exec hadoop-namenode bash -c "/opt/hadoop/init.sh"&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- Go to the dashboard's app url (http://localhost:5000) and validate that there aren't any data yet.
- Wait a while for the retail app's database to populate then run the Spark pipeline withe the orchestrator
  ```bash


  python ./orchestrator/scheduler.py


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;(Optional) You can check the progress from Spark's master node UI.&lt;/li&gt;
&lt;li&gt;After the pipeline finishes successfully (all stages are ended with FINISHED), visit the dashboard's app url again and refresh. You will get the processed data now.&lt;/li&gt;
&lt;li&gt;(Optional) You can rerun the pipeline and notice the dashboard's data changes.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Stopping the project
&lt;/h2&gt;

&lt;p&gt;I didn't configure any Named volumes on Docker, so once you stop the project, all the data is lost.&lt;br&gt;
To stop the project just execute &lt;code&gt;docker compose down&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Things to consider if you want to make this into real-world project
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Instead of the custom &lt;em&gt;orchestrator&lt;/em&gt; I used, a proper orchestration tool should replace it like &lt;a href="https://airflow.apache.org/" rel="noopener noreferrer"&gt;Apache Airflow&lt;/a&gt;, &lt;a href="https://dagster.io/" rel="noopener noreferrer"&gt;Dagster&lt;/a&gt;, ..., etc.&lt;/li&gt;
&lt;li&gt;Also, instead of the custom Dashboard app, a proper BI tool like &lt;a href="https://app.powerbi.com/home" rel="noopener noreferrer"&gt;Power BI&lt;/a&gt;, &lt;a href="https://www.tableau.com/" rel="noopener noreferrer"&gt;Tableau&lt;/a&gt;, &lt;a href="https://superset.apache.org/" rel="noopener noreferrer"&gt;Apache Superset&lt;/a&gt;, ..., etc. will be more powerful and flexible.&lt;/li&gt;
&lt;li&gt;Bookmarking should be in the &lt;em&gt;Silver&lt;/em&gt; stage as well.&lt;/li&gt;
&lt;li&gt;In a multi-tenant environment, controlling access to the Spark and Hadoop cluster is a must. Tool like  &lt;a href="https://ranger.apache.org/" rel="noopener noreferrer"&gt;Apache Ranger&lt;/a&gt; are usually used to serve this purpose.&lt;/li&gt;
&lt;li&gt;This architecture isn't limited to ingesting from one source, multiple sources can be ingested too and enriched from other sources in the &lt;em&gt;Silver&lt;/em&gt; stage.&lt;/li&gt;
&lt;li&gt;The Spark operation in this project are rather simple. More complex operations can be done leveraging Spark's powerful analytics engine.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Key takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;You can control almost everything in a spark cluster from the &lt;code&gt;spark-defaults.conf&lt;/code&gt; file.&lt;/li&gt;
&lt;li&gt;Spark has a REST endpoint to submit jobs and manage the cluster but it isn't enabled by default.&lt;/li&gt;
&lt;li&gt;Spark history server isn't enabled by default.&lt;/li&gt;
&lt;li&gt;Don't use the latest Java version for Spark even if the docs says it's supported. (I had compatibility issue that appeared down the road as I was using Java 17).&lt;/li&gt;
&lt;li&gt;Developing Spark driver apps in a jupyter notebook will make your life way easier.&lt;/li&gt;
&lt;li&gt;Hadoop also has an http endpoint called WebHDFS which isn't enabled by default too.&lt;/li&gt;
&lt;li&gt;docker-entrypoint-initdb.d is very useful for Postgresql initiation. &lt;/li&gt;
&lt;li&gt;Never use plaintext passwords in your code, instead you env vars or cloud secrets services.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;p&gt;All the used tool official docs.&lt;/p&gt;

</description>
      <category>dataengineering</category>
      <category>apachespark</category>
      <category>data</category>
      <category>opensource</category>
    </item>
    <item>
      <title>Groovy 🎷 Cheat Sheet - 01 Say "Hello" from Groovy</title>
      <dc:creator>Fady GA 😎</dc:creator>
      <pubDate>Thu, 07 Mar 2024 10:43:03 +0000</pubDate>
      <link>https://dev.to/fadygrab/groovy-cheat-sheet-01-say-hello-from-groovy-11ge</link>
      <guid>https://dev.to/fadygrab/groovy-cheat-sheet-01-say-hello-from-groovy-11ge</guid>
      <description>&lt;p&gt;&lt;strong&gt;"Java is great, but I don't like it!"&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This was my first impression when I started to learn the language. Maybe if my first language I learnt was C++ instead of Python, I'd have another opinion, but who knows 🤷‍♂️.&lt;/p&gt;

&lt;p&gt;Recently I had to revisit the "&lt;a href="https://en.wikipedia.org/wiki/Java_virtual_machine" rel="noopener noreferrer"&gt;JVM&lt;/a&gt; languages universe" again. Yes, language(s), plural! Java isn't the only language that uses the JVM. I previously used &lt;a href="https://www.scala-lang.org/" rel="noopener noreferrer"&gt;Scala&lt;/a&gt;, which is a JVM language, to use &lt;a href="https://spark.apache.org/" rel="noopener noreferrer"&gt;Apache Spark&lt;/a&gt; for Data Engineering workloads, but this is for another post 😉.&lt;/p&gt;

&lt;p&gt;This time, I visited &lt;a href="https://groovy-lang.org/" rel="noopener noreferrer"&gt;Groovy&lt;/a&gt;. Given my biased first impression about Java and the JVM, I wasn't too exited! But I couldn't have been more wrong! 😁&lt;/p&gt;

&lt;h2&gt;
  
  
  Table of Contents:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;My first impressions about Groovy&lt;/li&gt;
&lt;li&gt;
Setting up your "Groovy" environment

&lt;ul&gt;
&lt;li&gt;Installation&lt;/li&gt;
&lt;li&gt;Groovy tools&lt;/li&gt;
&lt;li&gt;IDEs buffet&lt;/li&gt;
&lt;li&gt;Groovy "Hello, World"&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;[BONUS] Running Groovy in a Jupyter Notebook&lt;/li&gt;

&lt;li&gt;Conclusion&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  My first impressions about Groovy
&lt;/h3&gt;

&lt;p&gt;I don't consider myself a professional software developer. I'm merely a data person who mainly uses Python (among other things) in my daily work and trying my best to follow the principals of Software Engineering in whatever I build. That was a mouthful 😂&lt;/p&gt;

&lt;p&gt;When I started to pick up Groovy, I instantly noticed the similarities between it and Python despite being a JVM language. I could see that Groovy was built to supplement Java with "scripting" capabilities. When I started to dig deeper into the learning resources, I found out that Groovy wasn't meant to replace Java! But to supercharge your Java codebase and to make the "Ceremonious boiler plate code" redundant (a phrase I stole from Dan Vega 😁) while still using the JVM. I also read that "Groovy is Python for Java" 😉 &lt;/p&gt;

&lt;p&gt;To demonstrate this, it took me around 2 minutes to create a very simple class that just print "Hello" using java with this code. Now imagine creating a whole app like this! We will see how it is significantly faster (and shorter) with Groovy:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SayHello&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;[])&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello, from JAVA"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fni8wab43a4gqw64gi3fl.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fni8wab43a4gqw64gi3fl.gif" alt="java"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I'm a bit of a slow typer and other super devs out there might do the same in a heartbeat, but you get the idea 😉&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;But that said, - and again I might be a bit biased - Groovy is too slow for me! I compared it to &lt;a href="https://www.rust-lang.org/" rel="noopener noreferrer"&gt;Rust&lt;/a&gt; in this &lt;a href="https://www.linkedin.com/posts/fady-abdelmassih_just-a-little-test-to-see-how-groovy-compares-activity-7164699667891523584-czx7?utm_source=share&amp;amp;utm_medium=member_desktop" rel="noopener noreferrer"&gt;LinkedIn post&lt;/a&gt; and it was waaaaay slow. Keep in mind that subjectively comparing programming languages might be a tricky business. But at the end, it will be up to your use case/project to prefer a language over the other.&lt;/p&gt;

&lt;p&gt;Now, a trick I picked up earlier, is that&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you need to learn something, teach it!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The logic behind that, is whenever you need to teach anything, you are either an expert in that topic or not. When you aren't an expert, you will need to excel at that topic in order to offer credible and high value knowledge to the ones you teach.&lt;/p&gt;

&lt;p&gt;So, I want to learn Groovy, then I'll teach it! I'll post a short series (3-4 posts at most) about Groovy. It won't be an in-depth series, it will only give you what you need to get up and running with Groovy. A &lt;strong&gt;&lt;em&gt;Cheat Sheet&lt;/em&gt;&lt;/strong&gt; if you will... &lt;/p&gt;

&lt;p&gt;In this one, I'll start with the basics, enjoy 😎&lt;/p&gt;

&lt;h3&gt;
  
  
  Setting up your "Groovy" environment
&lt;/h3&gt;

&lt;p&gt;As with any programming language, your development environment is basically installing the language itself and an &lt;a href="https://en.wikipedia.org/wiki/Integrated_development_environment" rel="noopener noreferrer"&gt;IDE&lt;/a&gt;. In the following section we will see how to setup our environment and the different options that we have to do so.&lt;/p&gt;

&lt;h4&gt;
  
  
  Installation
&lt;/h4&gt;

&lt;p&gt;As Groovy is a JVM language, you will need 2 things to start coding with it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;a href="https://en.wikipedia.org/wiki/Java_Development_Kit" rel="noopener noreferrer"&gt;JDK&lt;/a&gt; (Java Development Kit)&lt;/li&gt;
&lt;li&gt;The Groovy jar files.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The manual setup process contains:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Download and install you favorite JDK. (I use &lt;a href="https://openjdk.org/" rel="noopener noreferrer"&gt;OpenJDK&lt;/a&gt; 17. You can use the 21 stable version, it won't really matter at this stage which JDK provider you choose (yes, there are many) and the version as long as it's relatively new 😁)&lt;/li&gt;
&lt;li&gt;Depending on your OS, make sure that there is a &lt;code&gt;JAVA_HOME&lt;/code&gt; env var pointing to the &lt;code&gt;bin&lt;/code&gt; directory insider your JDK installation directory.&lt;/li&gt;
&lt;li&gt;Download and install the latest &lt;a href="https://groovy.apache.org/download.html" rel="noopener noreferrer"&gt;Groovy version&lt;/a&gt; either using the installers or the binary zip files. It all falls down to your personal preference.&lt;/li&gt;
&lt;li&gt;Like with the JDK, make sure that you have a &lt;code&gt;GROOVY_HOME&lt;/code&gt; env var pointing to the &lt;code&gt;bin&lt;/code&gt; directory in your Groovy installation directory.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If you survive all of this, you should be more or less good to go! 😂&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Alternatively&lt;/em&gt;&lt;/strong&gt;, you can use &lt;a href="https://sdkman.io/" rel="noopener noreferrer"&gt;sdkman&lt;/a&gt;. A great tool to install your Software Development Kit. The downside is that it only works on *nix systems. So for &lt;em&gt;Widnows&lt;/em&gt; users, you will have to use &lt;a href="https://learn.microsoft.com/en-us/windows/wsl/install" rel="noopener noreferrer"&gt;WSL&lt;/a&gt; or &lt;a href="https://cygwin.com/" rel="noopener noreferrer"&gt;Cygwin&lt;/a&gt; as the official page suggests.&lt;br&gt;
It is really simple to use &lt;code&gt;sdkman&lt;/code&gt;. after a successful &lt;a href="https://sdkman.io/install" rel="noopener noreferrer"&gt;installation&lt;/a&gt;, just type those commands into your *nix shell:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

sdk &lt;span class="nb"&gt;install &lt;/span&gt;java


&lt;/code&gt;&lt;/pre&gt;

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

sdk &lt;span class="nb"&gt;install &lt;/span&gt;groovy


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;And that's it. You are good to go 😃  &lt;/p&gt;

&lt;p&gt;For a complete usage guide, check its &lt;a href="https://sdkman.io/usage" rel="noopener noreferrer"&gt;official page&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;You can check if you have a correct environment istallation by typing those commands into your system's shell:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

java &lt;span class="nt"&gt;--version&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

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

groovy &lt;span class="nt"&gt;--version&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fkdi2issd50uorurlqxxu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fkdi2issd50uorurlqxxu.png" alt="successful installation"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Groovy tools
&lt;/h4&gt;

&lt;p&gt;Groovy installation comes with 3 main cli-ish tools:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;groovy&lt;/code&gt;: The Groovy command line processor&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.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%2Flqyg6d6z2jbox13lnofa.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Flqyg6d6z2jbox13lnofa.gif" alt="groovy cli"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;groovyc&lt;/code&gt;: The Groovy compiler (I rarely use that, at least for now)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;groovyConsole&lt;/code&gt;: The Groovy REPL (Read Evaluate Print Loop). In other words, a minimalist IDE. While learning, the &lt;code&gt;groovyConsole&lt;/code&gt; will be all what you need. It's very basic and can't work within WSL.
&lt;img src="https://media.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%2Fxqicsqwgfyqv4dva2jqy.gif" alt="groovyConsole"&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  IDEs buffet
&lt;/h4&gt;

&lt;p&gt;While the &lt;code&gt;groovyConsole&lt;/code&gt; will satisfy all of our learning needs, if you ever wanted to go the next "groovy" level, you will most probably need an IDE. IDEs comes with all the bells and whistles that should make you more productive and grooving 😁.&lt;/p&gt;

&lt;p&gt;I personally prefer &lt;a href="https://code.visualstudio.com/" rel="noopener noreferrer"&gt;VS Code&lt;/a&gt;. But for Groovy, it didn't offer the right extensions.&lt;/p&gt;

&lt;p&gt;Next, I've tried &lt;a href="https://netbeans.apache.org/front/main/" rel="noopener noreferrer"&gt;Apache Netbeans&lt;/a&gt;. It felt like it's built for Java rather than Groovy. I couldn't make its basic "IntelliSense" (code auto-completion) feature correctly work for Groovy so I ended up abandoning it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.jetbrains.com/idea/" rel="noopener noreferrer"&gt;IntelliJ&lt;/a&gt; (look for the community edition) on the other hand offered great out-of-box Groovy support including IntelliSence, building, and running features. So, I sticked with it 😊&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Ft84dapbhagwgoc265c49.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Ft84dapbhagwgoc265c49.gif" alt="intellij"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Groovy "Hello, World"
&lt;/h4&gt;

&lt;p&gt;Now this won't be an introductory programming language post without the classic "Hello, World" example, right? 😉&lt;/p&gt;

&lt;p&gt;I'll use the most basic commands using &lt;code&gt;bash&lt;/code&gt;:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"println 'Hello, World'"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; helloWorld.groovy
groovy helloWorld


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fmjidy0kfhdgbdu6nyecq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fmjidy0kfhdgbdu6nyecq.png" alt="Hello world cli"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And another "Hello, World" from the &lt;code&gt;groovyConsole&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fx06m94u4gzxd5xc339gv.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fx06m94u4gzxd5xc339gv.gif" alt="console hello"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Yup, that's it! Compare that to the Java class I wrote earlier!&lt;/p&gt;

&lt;h3&gt;
  
  
  [BONUS] Running Groovy in a Jupyter Notebook
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://jupyter.org/" rel="noopener noreferrer"&gt;Jupyter Notebooks&lt;/a&gt; is very popular among data people specially Python users. So, I tried to find a way to run the Groovy kernel inside a Jupyter Notebook, and to my surprise, I found a way, &lt;a href="http://beakerx.com/" rel="noopener noreferrer"&gt;BeakerX&lt;/a&gt;!&lt;/p&gt;

&lt;p&gt;The easiest way to try it is to use its &lt;code&gt;Docker&lt;/code&gt; container as follows (you must have docker installed in your system):&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

docker run &lt;span class="nt"&gt;-p&lt;/span&gt; 8888:8888 beakerx/beakerx
&lt;span class="c"&gt;# Image pulling will take sometime if it's the first time you run it.&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The open up the url that you will be presented with and you will be good to go. (you will have to replace the funky server name with &lt;code&gt;localhost&lt;/code&gt; for it to work, or at least this is how it worked for me)&lt;/p&gt;

&lt;p&gt;This is another "Hello, World" example using Jupyter with a Groovy kernel:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2F9ezk1hfofcp3hu84kv3r.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2F9ezk1hfofcp3hu84kv3r.gif" alt="Jupyter"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Groovy is very close to Python that runs in the JVM universe. It's not meant to replace Java, but to supplement it. Groovy already comes with all the tools you need to get started. In this one, I didn't go into Groovy language specifics. I'll do that in later posts. See you then!&lt;/p&gt;

</description>
      <category>groovy</category>
      <category>java</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Meet Bob 🙂 and his Databases exploration journey 🗺️! [Relational Databases - Basics - Theory]</title>
      <dc:creator>Fady GA 😎</dc:creator>
      <pubDate>Tue, 24 Oct 2023 14:08:42 +0000</pubDate>
      <link>https://dev.to/fadygrab/meet-bob-and-his-databases-exploration-journey-relational-databases-basics-theory-4l1i</link>
      <guid>https://dev.to/fadygrab/meet-bob-and-his-databases-exploration-journey-relational-databases-basics-theory-4l1i</guid>
      <description>&lt;p&gt;In this article, we will meet Bob, the proud owner of a promising retail store, and see his journey that led him to discover the &lt;strong&gt;Relational Databases&lt;/strong&gt; 😯! Let's begin.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This article is part of my &lt;a href="https://www.fadyinthecloud.click/data-analytics-made-simpler/" rel="noopener noreferrer"&gt;Data Analytics Made Simpler series&lt;/a&gt;. On its website, I'm giving more details about what exactly this series is, and what's my motivation to do something like that, and how I plan to release its content!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Table of Contents:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Meet Bob! Our store owner&lt;/li&gt;
&lt;li&gt;Bob's first attempt&lt;/li&gt;
&lt;li&gt;How Bob improved his data model&lt;/li&gt;
&lt;li&gt;The introduction of Keys&lt;/li&gt;
&lt;li&gt;Relationships Cardinality&lt;/li&gt;
&lt;li&gt;Enter: RDBMS&lt;/li&gt;
&lt;li&gt;Conclusion&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Meet Bob! Our store owner:
&lt;/h3&gt;

&lt;p&gt;Bob has taken responsibility for his family business, a retail store in his home town! He is very creative and business-oriented; he managed to expand his customer base threefold over the last year alone! In order to retain his current customers and attract new ones, he had the idea of implementing a "Loyality Program", where he awards his "loyal" customers with discounts based on their number of store visits and the amount they spent.&lt;br&gt;
The problem is, he doesn't have any database knowledge, and he thought that he could pull this off using a spreadsheet application that he is very proficient with.&lt;/p&gt;

&lt;h3&gt;
  
  
  Bob's first attempt:
&lt;/h3&gt;

&lt;p&gt;As Bob is a spreadsheet guru, he created one with the following columns:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Date&lt;/li&gt;
&lt;li&gt;Customer Name&lt;/li&gt;
&lt;li&gt;Customer Phone&lt;/li&gt;
&lt;li&gt;Customer Email&lt;/li&gt;
&lt;li&gt;First Purchase&lt;/li&gt;
&lt;li&gt;Item Description&lt;/li&gt;
&lt;li&gt;Quantity&lt;/li&gt;
&lt;li&gt;Unit Price&lt;/li&gt;
&lt;li&gt;Total Price&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;From his point of view, this spreadsheet captures everything he needs! The date of the purchase, the customer data, the purchased items, and the amount spent by the customers.&lt;br&gt;
It worked fine for a couple of weeks until he was faced with some challenges that made it very hard to continue this way:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It was very daunting to enter the same date and the customer's data with each item he/she purchases, knowing that one customer is more likely to purchase several items at once.&lt;/li&gt;
&lt;li&gt;Customers started complaining as it takes a long time for their entire purchase to be registered, especially if there are a lot of purchased items.&lt;/li&gt;
&lt;li&gt;He noticed that the data for the same customer might be entered differently for separate purchases on different days (or even on the same day!). It may be due to typos; the data is entirely different or completely missing.&lt;/li&gt;
&lt;li&gt;Similar to the customer data, the &lt;strong&gt;Items&lt;/strong&gt; data might suffer from similar inconsistencies, like description changes or price changes, which make something like tracking the performance of a single item hard to track.&lt;/li&gt;
&lt;li&gt;As he kept entering the same customer and item data over and over again, the spreadsheet file's size exploded very quickly! &lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  How Bob improved his data model:
&lt;/h3&gt;

&lt;p&gt;Bob has realized that his current data model isn't scalable! Like when the data has accumulated for a couple of weeks, it becomes difficult to maintain and get accurate insights from it.&lt;br&gt;
The first thing he really wanted to change was entering the same customer data with each purchased item. As this is redundant and makes modifying any piece of data, like the customer's phone, very challenging, as he must check all the previous purchases for this particular customer and change the phone number in each of his/her related records!&lt;br&gt;
He thought of separating the &lt;strong&gt;Customers&lt;/strong&gt; in their own "worksheet" (table) and renaming the original worksheet to &lt;strong&gt;Purchases&lt;/strong&gt; and removing all the customer data columns from it, leaving only the "Customer Name". He first sketched it out as follows before he performed the actual change to his current file:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fe9z68unrtnzr84eebuow.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fe9z68unrtnzr84eebuow.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Bob has unknowingly created his own version of an "Entity Relationship Diagram" (ERD) which still missing some key ERD features but is close enough!&lt;br&gt;
What he did is that he has created two &lt;strong&gt;Entities&lt;/strong&gt;, "Customers" and "Purchases", and he has used the "Customer Name" as the &lt;strong&gt;Link&lt;/strong&gt; AKA "relationship" between the two entities.&lt;br&gt;&lt;br&gt;
Now his model has become more maintainable and far less in size as if he needed to do the customer phone number modification we discussed earlier, he only needed to do it once in one record in the "Customers" table. Also, as he now doesn't include the full customer's data at each purchase record and only include the Customer Name, the size of his workbook has become substantially smaller.  &lt;/p&gt;

&lt;p&gt;But soon he encountered another problem!  &lt;/p&gt;

&lt;p&gt;He noticed, that some customers might share the same name! So this will become problimatic in his "Loyality Program" implementation as for such cases, he wouldn't be able to tell wich is the correct customer to award the discounts to!  &lt;/p&gt;

&lt;h3&gt;
  
  
  The introduction of keys:
&lt;/h3&gt;

&lt;p&gt;So, he realized that seting the "Customer Name" as the "&lt;strong&gt;Key&lt;/strong&gt;" to identify the relationship between the "Customers" and "Purchases" entities might not be ideal as it might not be &lt;strong&gt;&lt;em&gt;Unique&lt;/em&gt;&lt;/strong&gt;. He inspected the columns in the Customers table to identify another column that is 100% unique for each customer, he found some good candidates like phone number or email address. He thought that those should be unique for every customer! As one phone number can be owned by one customer and the same goes for the email address, right?  &lt;/p&gt;

&lt;p&gt;Then he discoverd that if he used those columns, he might introduce further problems down the road! Customers are allowed to change their phone numbers and emails. So, if he had used the phone number for example as the identifying field for the customers and later customers changed their numbers, he then would have different purchase records with diffirent phone numbers that could be expired and not refering to any customers in his "Customers" table!&lt;/p&gt;

&lt;p&gt;He decided to introduce an new column to his "Customers" table to act as the unique ID for every customer he has. He though that this column should be unique and can't be empty. He wanted it to be simple, so he chose it to be an incrementing number starting at 1. Then instead of the "Customer Name" to link the two table, he would use this newly introduced "Key" column. He updated his model diagram adding a "Purchase ID" key column as he felt he could use it later:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fh0i2214tqfzwh5bsv2kw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fh0i2214tqfzwh5bsv2kw.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Again without knowing, Bob has created something called a "Primary Key" by introducing the "Customer ID" column in the "Customers" table. The main function of a "Primary Key" in a table is to be able to uniquely identify each record in this table. Also, by using the same column in the "Purchases" table to define the relationship between the "Customers" table, he used it this time as a "Foreign Key" (foreign to the table). In contrast to the "Primary Keys" that must be unique in a table, "Foreign Keys" can be non-unique in the same table.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In a real ERD, "Primary Keys" (PK) and "Foreign Keys" (FK) should be annotated on the diagram and the "relationship" should be named, conventionally with a "verb". Also, something called relationship "cardinality" must be annotated too on the line related to the relationship. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This way, Bob managed to enhanced his data model a lot. All he has to do to register a new purchase, is to lookup the customer's record, retreive its PK (Primary Key), and write it in front of each purchased item.&lt;br&gt;
But he felt that there is more to be done, so he invited his friend Ben, a database designer, for checking what Bob has done so far with his model.&lt;/p&gt;

&lt;h3&gt;
  
  
  Relationships Cardinality:
&lt;/h3&gt;

&lt;p&gt;Ben has really admired what Bob has done so far without any prior database knowledge! He saw the diagram that Bob has made and told him that it looked pretty much like a real ERD and explained how he could make it so. He also explained the concept of "Primary Keys" and "Foreing Keys" that Bob has used unknowingly.&lt;br&gt;
He felt that he needed to explain to Bob, the concept of "cardinality" of the relationships between the entities as he saw room for improvements for the current data model.  &lt;/p&gt;

&lt;p&gt;Relationship Cardinalty is a fairly simple concept. In general, it defines how the records from one table are related to the records from the other table at both ends of a relationship. It can be "One-to-One", "One-to-Many", or "Many-to-Many".&lt;br&gt;&lt;br&gt;
For "One-to-One", it means that one record for the first table is linked to only one record from the second table. For example, if we split the "Customers" table into two table, with "Name" and "Customer ID" in one table and "Customer ID" and the rest of the columns in the other table. In that case, each record from any table will be linked to one and only one record from the other table using the "Customer ID". "One-to-One" has its application in databases but it isn't commonly used.&lt;/p&gt;

&lt;p&gt;Similary for "One-to-Many", it indicates that one record for the "One" side of the relationship can be linked to many records from the "Many" side. The relationship between the "Customers" and the "Purchases" tables is "One-to-many" as "One" customer can perform "Many" purchases but one purchase can't be done by many customers.&lt;/p&gt;

&lt;p&gt;"Many-to-Many" relationships might need some time to grasp but they aren't hard to understand! It means that "Many" records from the first table can be linked with many records from the second table. Think of "Students" and "Classes" tables. One Student can register to "Many" classes and one class can contain "Many" students.  &lt;/p&gt;

&lt;p&gt;Ben has recommended to Bob that he could seperate the "Items" entity similar to what he has done to the "Customers" entity (table). He also identified the cardinality between "Customers" and "Items" as "Many-to-Many" as one customer can purchase "Many" items and one Item can be purchased by "Many" customers.  &lt;/p&gt;

&lt;p&gt;Finally, he modified Bob's diagram to show PKs, FKs, and relationships cardinalities. He separated "Items" into their own table and used the "Purchases" table as an intermediate table to implement the "Many-to-Many" relationship between "Customers" and "Items". The final ERD became as follows:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2F78wgn3dhc4sqd0gsfamp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2F78wgn3dhc4sqd0gsfamp.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Again, this is a real ERD but in its simpleset form. ERDs aren't in the scope of this post and I might discuss them in later posts.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Enter: RDBMS
&lt;/h3&gt;

&lt;p&gt;Bob has thanked Ben for his advice! And indeed acknowledged that the overall size of the workbook file has become smaller and the whole model has become more maintainable than before those changes. But he rememebered that he still uses &lt;em&gt;spreadsheets&lt;/em&gt; app to manage his data! He realized that there is a lot of manual work to be done like looking up the IDs of  customers and items, ensuring that the IDs in the "Customers" and "Items" tables are unique and present, the "quantity" in the "Purchases" table is positive and present, and so much more "rules" that he must enforce at data entry time! Also, he noticed that the "Total Price" column is not straight forward to calculate as before he splitted the "Items" from "Purchases" and the "Loyality Program" analysis now requires a lot more work!  &lt;/p&gt;

&lt;p&gt;He told his friend Ben all of his concerns. Ben smiled and told Bob, "Why don't you use a &lt;strong&gt;&lt;em&gt;Relational Database Management System&lt;/em&gt;&lt;/strong&gt; (RDBMS)?"  &lt;/p&gt;

&lt;p&gt;Ben continued to list all the benifits of using a dedicated RDBMS software like &lt;em&gt;MySQL&lt;/em&gt;, &lt;em&gt;Postgresql&lt;/em&gt;, &lt;em&gt;MariaDB&lt;/em&gt;, or many others over the use of spreadsheets app:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;With RDBMSs, you won't manage any files. The RDBMS will do it for you.&lt;/li&gt;
&lt;li&gt;The "rules" that you want to enforce are called in the RDBMS world, "Constraints" and the RDBMS can enforce them for you. For example, for a "Primary Key" column you can enforce the &lt;code&gt;UNIQUE&lt;/code&gt; and &lt;code&gt;NOT NULL&lt;/code&gt; constaints. "Foreign Keys" are a from of constraint too. RDBMSs check the existance of the key used as a foreing key in a table as a primary keyin the other table in a "One-to-Many" relationship.&lt;/li&gt;
&lt;li&gt;Some RDBMS has the "Calculated Columns" feature and you can calculate the "Total Price" with relative ease like in a spreadsheet application.&lt;/li&gt;
&lt;li&gt;You won't have to "join" the tables manualy like in a spreadsheet appplication as RDBMSs uses &lt;strong&gt;SQL&lt;/strong&gt; (Structured Query Language) and "joining" all the tables can be joined with a statement as simple as:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; 
    &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="k"&gt;c&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="k"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Phone&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="k"&gt;c&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="k"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;FirstPurchase&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Description&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;UnitPrice&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;TotalPrice&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;Purchases&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;
&lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;Customers&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="k"&gt;c&lt;/span&gt;
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CustomerID&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CustomerID&lt;/span&gt;
&lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;Items&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ItemID&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ItemID&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;This may look intemidating if you don't know SQL but trust me it is very simple and easy to understand! 😉&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ol&gt;
&lt;li&gt;Similar to joining tables together, you can have your "Loyality Program" analysis using a stored &lt;strong&gt;SQL Query&lt;/strong&gt; with just a click of a button!&lt;/li&gt;
&lt;li&gt;You can host your RDBMS on a dedicated server and build an application on top of it leveraging &lt;em&gt;SQL&lt;/em&gt; for more user friendly environment while entring the purchases data.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That and many other benefits!&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion:
&lt;/h3&gt;

&lt;p&gt;Bob was really happy with his discovery of Relational Databases and RDBMSs! He even hired a developer to build his purchases application and integrating his inventory systems with it!&lt;br&gt;
He managed to keep his current customers happy and win more customers! He expanded his familly business into other districts and his store became a well known franchise! 😎&lt;br&gt;&lt;br&gt;
The bottom line, Relational Database makes handling data a very easy job! SQL is used by RDBMSs and it is a very powerfull tool to query and analyse your data. The model we have explored today is call &lt;strong&gt;&lt;em&gt;Online Transactional Processing&lt;/em&gt;&lt;/strong&gt; (OLTP) as it is optimized for "writes". Meaning, you can write data very quickly but reading it will take time as you will probably need to join several table to get a meaningfull analysis. And it is called "Transctional" as it supports transactions that can be one or more operation that are done together or not done at all. More on OLTP and ACID compliance (that describes transactions) in later posts. See you then 👋&lt;/p&gt;

</description>
      <category>dataengineering</category>
      <category>database</category>
      <category>beginners</category>
      <category>learning</category>
    </item>
    <item>
      <title>Learning Rust 🦀: 18 - Rust Collections: HashMaps, accessing values with keys instead of indices</title>
      <dc:creator>Fady GA 😎</dc:creator>
      <pubDate>Fri, 20 Oct 2023 14:48:37 +0000</pubDate>
      <link>https://dev.to/fadygrab/learning-rust-18-rust-collections-hashmaps-accessing-values-with-keys-instead-of-indices-4kg2</link>
      <guid>https://dev.to/fadygrab/learning-rust-18-rust-collections-hashmaps-accessing-values-with-keys-instead-of-indices-4kg2</guid>
      <description>&lt;p&gt;I'll continue Rust's collections discusion that started two articles ago with Vectors and Strings as a special kind of Vectors. In this last dedicated article about collections, I'll bring out the &lt;strong&gt;HashMaps&lt;/strong&gt;! Let's begin.&lt;/p&gt;

&lt;blockquote&gt;
&lt;h4&gt;
  
  
  ⚠️ Remember!
&lt;/h4&gt;

&lt;p&gt;You can find all the code snippets for this series in its &lt;a href="https://github.com/FadyGrAb/rust-learning-series"&gt;accompanying repo&lt;/a&gt;&lt;br&gt;&lt;br&gt;
If you don't want to install Rust locally, you can play with all the code of this series in the official &lt;a href="https://play.rust-lang.org/?version=stable&amp;amp;mode=debug&amp;amp;edition=2021"&gt;Rust Playground&lt;/a&gt; that can be found on its official page.&lt;/p&gt;

&lt;p&gt;⚠️⚠️ The articles in this series are loosely following the contents of "The Rust Programming Language, 2nd Edition" by Steve Klabnik and Carol Nichols in a way that reflects my understanding from a Python developer's perspective.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;⭐ I try to publish a new article every week (maybe more if the Rust gods 🙌 are generous 😁) so stay tuned 😉. I'll be posting "new articles updates" on my &lt;a href="https://www.linkedin.com/in/fady-abdelmassih"&gt;LinkedIn&lt;/a&gt; and &lt;a href="https://twitter.com/fadyGrAb"&gt;X (fromarly Twitter)&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Table of Contents:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;What is a Hash Map&lt;/li&gt;
&lt;li&gt;Creating a Hash Map&lt;/li&gt;
&lt;li&gt;Accessing values in a Hash Map&lt;/li&gt;
&lt;li&gt;Updating a Hash Map&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  What is a Hash Map:
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Hash Maps&lt;/strong&gt; aren't something native to Rust. Other programming languages has similar structures but with different names, like &lt;em&gt;Dictionaries&lt;/em&gt; in Python, &lt;em&gt;Objects&lt;/em&gt; in Javascrip, &lt;em&gt;Hash Tables&lt;/em&gt; in Powershell (althoug not a programming language per-sey), and many more.&lt;br&gt;
They work more or less the same way as you would expect! You retreive a &lt;em&gt;Value&lt;/em&gt; with a &lt;em&gt;Key&lt;/em&gt; instead of and &lt;em&gt;index&lt;/em&gt; as in &lt;strong&gt;Vectors&lt;/strong&gt;.&lt;br&gt;
Rust's &lt;strong&gt;HashMap&lt;/strong&gt; is pretty similar to Python's &lt;strong&gt;Dictionary&lt;/strong&gt; except that in Rust, the &lt;strong&gt;HashMap&lt;/strong&gt; must have the keys of the same  type and the values also must be of the same type but the keys and values can be of similar of different types than eachother.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Hash Maps is written in Rust as &lt;code&gt;HashMap&amp;lt;K, V&amp;gt;&lt;/code&gt; where &lt;code&gt;K&lt;/code&gt; is the keys' type and &lt;code&gt;V&lt;/code&gt; is the values' type which can be the same.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I'll demonstrate here the basic API of Rust's &lt;strong&gt;Hash Maps&lt;/strong&gt; but as always, the documentations contains a lot of other metods that can be very useful.&lt;/p&gt;
&lt;h3&gt;
  
  
  Creating a Hash Map:
&lt;/h3&gt;

&lt;p&gt;Appearently &lt;strong&gt;Hash Maps&lt;/strong&gt; isn't used much in Rust that they aren't loaded in the &lt;em&gt;prelude&lt;/em&gt; like &lt;strong&gt;Vectors&lt;/strong&gt; and &lt;strong&gt;Strings&lt;/strong&gt;. So, we have to "import" them first wiht the &lt;code&gt;use&lt;/code&gt; keyword then use the &lt;code&gt;new&lt;/code&gt; method to create a new one. In the Example below, I'me creating a &lt;code&gt;map&lt;/code&gt; &lt;strong&gt;HashMap&lt;/strong&gt; with the keys of type &lt;code&gt;String&lt;/code&gt; and the values of type &lt;code&gt;u8&lt;/code&gt;. It should represent the scores that students have obtained after taking and exam. I then inserted the scores of two students, &lt;code&gt;Bob&lt;/code&gt; and &lt;code&gt;Ben&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;collections&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;HashMap&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="c1"&gt;// Create a Hash Map&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;map&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;HashMap&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;u8&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;HashMap&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="n"&gt;map&lt;/span&gt;&lt;span class="nf"&gt;.insert&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;"Bob"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;map&lt;/span&gt;&lt;span class="nf"&gt;.insert&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;"Ben"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="mi"&gt;25&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:?}"&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;Here, I've used the &lt;code&gt;insert&lt;/code&gt; method to insert the student names (keys) and there exam scores (values). The &lt;code&gt;println&lt;/code&gt; output will be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{"Bob": 30, "Ben": 25}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Accessing values in a Hash Map:
&lt;/h3&gt;

&lt;p&gt;To access a certain value in a &lt;strong&gt;Hash Map&lt;/strong&gt;, we can use the &lt;code&gt;get&lt;/code&gt; method wich takes the key as its parameter. The &lt;code&gt;get&lt;/code&gt; method retun an &lt;strong&gt;Option&amp;lt;&amp;amp;V&amp;gt;&lt;/strong&gt; which will return &lt;strong&gt;Some&amp;lt;&amp;amp;V&amp;gt;&lt;/strong&gt; in case if the key exists or &lt;strong&gt;None&lt;/strong&gt; if it doesn't. For Example, to get &lt;code&gt;Bob&lt;/code&gt;'s score we can use the follwing code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;bob_score&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;map&lt;/span&gt;&lt;span class="nf"&gt;.get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Bob"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.copied&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.unwrap_or&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="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Bob scored {bob_score}"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;OK, this looks a little strange. Let's break it down.&lt;br&gt;
As I've mentioned, the &lt;code&gt;get&lt;/code&gt; method returns the &lt;strong&gt;Option&amp;lt;&amp;amp;V&amp;gt;&lt;/strong&gt; enum of a reference for the values type (here it is &lt;code&gt;&amp;amp;u8&lt;/code&gt;). The &lt;code&gt;copied&lt;/code&gt; method will return &lt;strong&gt;Option&amp;lt;V&amp;gt;&lt;/strong&gt; instead of &lt;strong&gt;Option&amp;lt;&amp;amp;V&amp;gt;&lt;/strong&gt; i.e. &lt;strong&gt;Option&amp;lt;u8&amp;gt;&lt;/strong&gt; as I don't want to return a reference of the value. Then the &lt;code&gt;unwrap_or&lt;/code&gt; method will return &lt;code&gt;V&lt;/code&gt; if the &lt;strong&gt;Option&lt;/strong&gt; enum is &lt;em&gt;Some&lt;/em&gt; or a &lt;code&gt;default&lt;/code&gt; value that it takes as a parameter. So, &lt;code&gt;bob_score&lt;/code&gt; will hold eventually the value of the "Bob" key with &lt;code&gt;u8&lt;/code&gt; type and it will be printed as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Bob scored 30
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;If we haven't used &lt;code&gt;copied&lt;/code&gt; here, we would write something strange as the paramtere of &lt;code&gt;unwrap_or&lt;/code&gt; like &lt;code&gt;unwrap_or(&amp;amp;0)&lt;/code&gt; and &lt;code&gt;bob_scrore&lt;/code&gt; will be a refrence of the retrieved value.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Another way to access values inside a &lt;strong&gt;Hash Map&lt;/strong&gt; is to use a &lt;code&gt;for&lt;/code&gt; loop as follows:&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;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;map&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;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&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 will print the following as expected:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Ben: 25
Bob: 30
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One more thing before we can leave this section is that &lt;strong&gt;Mash Maps&lt;/strong&gt; takes ownership over the passed keys and values. Look at the follwing 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;jane&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;"Jane"&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;jane_score&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;29&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;map&lt;/span&gt;&lt;span class="nf"&gt;.insert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;jane&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;jane_score&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;"{jane} scored {jane_score}"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Error: borrow of moved value: `jane`&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we have declared &lt;code&gt;jane&lt;/code&gt; as &lt;strong&gt;String&lt;/strong&gt; and her score &lt;code&gt;jane_score&lt;/code&gt; as &lt;strong&gt;i32&lt;/strong&gt;. As we have used the &lt;code&gt;insert&lt;/code&gt; method, &lt;code&gt;jane&lt;/code&gt; has "moved" into the &lt;strong&gt;Hash Map&lt;/strong&gt; as it's a &lt;strong&gt;String&lt;/strong&gt; while &lt;code&gt;jane_score&lt;/code&gt; is "copied" as it's a simple type. The &lt;code&gt;println&lt;/code&gt; will cause a compilation error as &lt;code&gt;jane&lt;/code&gt; isn't valid anymore after the usage of &lt;code&gt;insert&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Updating a Hash Map:
&lt;/h3&gt;

&lt;p&gt;We usualy update a &lt;strong&gt;Hash Map&lt;/strong&gt; in one of the following scenarios:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Overwite the old values&lt;/li&gt;
&lt;li&gt;Only update if the key isn't present&lt;/li&gt;
&lt;li&gt;Update a value based on its old value
Let's see how we can perform each.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Overwrite the values:
&lt;/h4&gt;

&lt;p&gt;This is probably the simplest implementation. We just have to use &lt;code&gt;insert&lt;/code&gt; using a pre-existing key.&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="n"&gt;map&lt;/span&gt;&lt;span class="nf"&gt;.insert&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;"Jane"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;29&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;jane_score&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;map&lt;/span&gt;&lt;span class="nf"&gt;.get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Jane"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.copied&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="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Jane score before the update {jane_score}"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="n"&gt;map&lt;/span&gt;&lt;span class="nf"&gt;.insert&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;"Jane"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="mi"&gt;30&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;jane_score&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;map&lt;/span&gt;&lt;span class="nf"&gt;.get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Jane"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.copied&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="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Jane score after the update {jane_score}"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we have updated &lt;code&gt;Jane&lt;/code&gt;'s score from &lt;code&gt;29&lt;/code&gt; to &lt;code&gt;30&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Jane score before the update 29
Jane score after the update 30
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Only update if the key isn't present:
&lt;/h4&gt;

&lt;p&gt;In this use case, we use the &lt;code&gt;entry&lt;/code&gt; method that takes a key as a parameter and returns &lt;strong&gt;Entry&lt;/strong&gt; enum representing an entry that could exist or not. Then we use a similarly useful method, &lt;code&gt;or_insert&lt;/code&gt; that insert the passed value if the entry doesn't exist. Let's look at this 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="n"&gt;map&lt;/span&gt;&lt;span class="nf"&gt;.entry&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;"Jane"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="nf"&gt;.or_insert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;map&lt;/span&gt;&lt;span class="nf"&gt;.entry&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;"Windy"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="nf"&gt;.or_insert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;25&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:?}"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we were retrieving the &lt;code&gt;Jane&lt;/code&gt; and &lt;code&gt;Windy&lt;/code&gt; entries and inserting &lt;code&gt;25&lt;/code&gt; as their exam score if they arent present. As &lt;code&gt;Jane&lt;/code&gt; entry already exist from the last section, it will remain unchanged with the value of &lt;code&gt;30&lt;/code&gt;. As for &lt;code&gt;Windy&lt;/code&gt;, because it's a new key, it will be inserted with the value of &lt;code&gt;25&lt;/code&gt; and the &lt;code&gt;println&lt;/code&gt; will print the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{"Ben": 25, "Windy": 25, "Bob": 30, "Jane": 30}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Update a value based on its old value:
&lt;/h4&gt;

&lt;p&gt;Let's say that we will give all students a 5 points bonus. So, we will have a new value of a certain key that is the result of the addition of its old value and the 5 points bonus. One way to do this is as follows:&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;for&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="n"&gt;score&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;map&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;score&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;5&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;"{map:?}"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's explain what's going on here.&lt;br&gt;
First, I've used the &lt;code&gt;iter_mut&lt;/code&gt; method that returns "mutable" references of the keys and values of the &lt;strong&gt;Hash Map&lt;/strong&gt; because I want to change the values. And as I don't care about the keys, I've used the underscore in its place in the &lt;code&gt;for&lt;/code&gt; expression. Now, &lt;code&gt;score&lt;/code&gt; is a &lt;code&gt;&amp;amp;mut u8&lt;/code&gt; or a mutable reference of the values of &lt;code&gt;u8&lt;/code&gt; type. Next, I've used the "de-referencing" operator "*" on &lt;code&gt;score&lt;/code&gt;. We will have a look at de-referencing later, but let's just say it's a way to get the value that the reference is "referencing". &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If I didn't use the "*" here, the compilier will complain about not being able to add {integer} refering to the &lt;code&gt;5&lt;/code&gt; and &lt;code&gt;&amp;amp;mut u8&lt;/code&gt; (the &lt;code&gt;score&lt;/code&gt;) as it considers them of different types.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Then I've added the 5 points bonus to the old values.&lt;br&gt;
The result of this will be all the values have increase by 5 from their old values (compare them to the last println output):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{"Ben": 30, "Windy": 30, "Bob": 35, "Jane": 35}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We have now looked at &lt;strong&gt;Hash Maps&lt;/strong&gt; and saw how they are almost similar to Python's Dictionaries except for the types restrictions.&lt;br&gt;
Next we will look at Rust's Error Handling. See you then 👋&lt;/p&gt;

</description>
      <category>learning</category>
      <category>rust</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Data Engineering Lifecycle - Basics - Theory</title>
      <dc:creator>Fady GA 😎</dc:creator>
      <pubDate>Sun, 15 Oct 2023 15:34:51 +0000</pubDate>
      <link>https://dev.to/fadygrab/data-engineering-lifecycle-basics-theory-2e60</link>
      <guid>https://dev.to/fadygrab/data-engineering-lifecycle-basics-theory-2e60</guid>
      <description>&lt;p&gt;This is the first article in my &lt;strong&gt;Data Analytics Made Simpler&lt;/strong&gt; series. To know more about it, please visit it's &lt;a href="https://www.fadyinthecloud.click/data-analytics-made-simpler/" rel="noopener noreferrer"&gt;website&lt;/a&gt; where I'm giving more details about what's exactly this series and what's my motivation to do something like that and how I plan to release its content!&lt;/p&gt;

&lt;p&gt;I wanted to start my series by drawing a mental image of what it will cover. In this article, I'll explain what I mean by "data analytics" in the series title and will give more details on what exactly is &lt;em&gt;Data Engineering&lt;/em&gt; and will explain its general components through the &lt;em&gt;Data Engineering Lifecycle&lt;/em&gt; framework.&lt;/p&gt;

&lt;p&gt;For the same article but in Arabic, please click &lt;a href="https://dev.to/fadygrab/dwr-hy-hnds-lbynt-lssyt-nzry-2ii5"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Table of Content:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;What I mean by Data Analytics&lt;/li&gt;
&lt;li&gt;What is Data Engineering&lt;/li&gt;
&lt;li&gt;The Data Engineering Lifecycle&lt;/li&gt;
&lt;li&gt;What a Data Engineer is not&lt;/li&gt;
&lt;li&gt;Conclusion&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  What I mean by Data Analytics:
&lt;/h3&gt;

&lt;p&gt;The term &lt;em&gt;Data Analytics&lt;/em&gt; is very broad and can have different meanings for different people! So, I want to be clear about it.&lt;/p&gt;

&lt;p&gt;When I talk about &lt;em&gt;Data Analytics&lt;/em&gt;, I almost visualize the following diagram in my head.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fb4j17ylj9gksi9kyycva.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fb4j17ylj9gksi9kyycva.png" alt="Data Analytics"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In my head, a huge chunk of &lt;em&gt;Data Analytics&lt;/em&gt; is &lt;em&gt;Data Engineering&lt;/em&gt; that I'll be explaining shortly, regardless of the data volume, variety, or velocity (understanding those terms will become easier over time, trust me 😉).&lt;br&gt;
The remaining chunk is &lt;em&gt;Data Visualization&lt;/em&gt;. To me it's the most natural thing to do with data after that we have done stuff to it in the &lt;em&gt;Data Engineering&lt;/em&gt; phase, but it isn't the only thing we can do.&lt;br&gt;
By all means, this is not an official definition for &lt;em&gt;Data Analytics&lt;/em&gt; but this is how I'll be tackling it in my series.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is Data Engineering?
&lt;/h3&gt;

&lt;p&gt;The overly simplified version of the &lt;em&gt;Data Engineering&lt;/em&gt; definition is as follows:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Data Engineering&lt;/em&gt; is the data field concerned by moving data from source to destination systems while applying transformations to the data according to the business requirements.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;For example, imagine an enterprise with multiple applications. Due to different Development teams, each application is generating its logs with its own format different from the other applications but it still contains the same info. Now, the management wants a dashboard to show users access trends over the past 3 months aggregated for all the applications. &lt;br&gt;
The operation of collecting the logs from each application, unify their format and extract the users access data then perform aggregation to it, and finally loading the aggregated data to a  data warehouse (more on that later on in the series) that the dashboard will eventually use, is called, you've guessed it, &lt;em&gt;Data Engineering&lt;/em&gt;!&lt;/p&gt;

&lt;p&gt;Now that you have some idea on what &lt;em&gt;Data Engineering&lt;/em&gt; is, I should add to the previous definition that &lt;em&gt;Data Engineering&lt;/em&gt; isn't just the data movement and the applied transformations, it's the design, implementation, and maintenance of the whole automated system (called, pipeline) that performs the whole thing!&lt;/p&gt;

&lt;p&gt;You may now get the impression that &lt;em&gt;Data Engineering&lt;/em&gt; is an enterprise thing! You won't be wrong! But you won't be entirely correct either 😁. Think about it, if you have a bunch of Excel workbooks laying around your laptop of say, monthly sales for the past 15 years with each month is in its own workbook and you've created a Python script to open those up, get the monthly sales total, and finally load all of those in an aggregated report. According to the mentioned definition, you've kind of did &lt;em&gt;Data Engineering&lt;/em&gt;!&lt;/p&gt;

&lt;h3&gt;
  
  
  The Data Engineering Lifecycle:
&lt;/h3&gt;

&lt;p&gt;I'll start this section by showing it to you then I'll explain:&lt;br&gt;
&lt;a href="https://media.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%2Figkt4hyg00k23apf9ped.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Figkt4hyg00k23apf9ped.png" alt="de-lifecycle"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, if we talked about &lt;em&gt;Data Engineering&lt;/em&gt; leaving out the technical details and tools selection, we will get a "framework" that tries to explain how it works, i.e. The &lt;strong&gt;Data Engineering Lifecycle&lt;/strong&gt;!&lt;/p&gt;

&lt;p&gt;If you take a closer look at the previous figure, you will be able to verify the earlier &lt;em&gt;Data Engineering&lt;/em&gt; definition! You will see that we are extracting or "ingesting" data from sources that "generate" it, applying "transformations" to it, then finally load or "serve" it to destination systems that will generally be either analytics or machine learning workloads.&lt;br&gt;
You may have noticed that there is a "storage" block that spans over the "ingestion", "transformation", and "serving" blocks. This is because we can actually store data at any stage of the lifecycle. For example, a common Datalake architecture (we will know about Datalakes later in the series) is to store raw data before processing. Then, do some filtering and cleaning transformations and store the resulting data in a dedicated location. And finally, do some aggregations to the processed data and store those as well in a dedicated location.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This is called the &lt;em&gt;Medallion Architecture&lt;/em&gt; as the raw area is called the &lt;em&gt;Bronze&lt;/em&gt; stage, the processed area is called the &lt;em&gt;Silver&lt;/em&gt; stage, and the aggregated area is called the &lt;em&gt;Gold&lt;/em&gt; stage.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;One other thing to know about the &lt;em&gt;Data Engineering Lifecycle&lt;/em&gt; is that it isn't a sequential flow! Meaning that you can (and often will) do blocks out of the illustrated order and/or more than once. For example, we can serve the ingested data directly to some "data consumer" systems before any transformation then perform the transformations and serve the final output to other systems.&lt;/p&gt;

&lt;h3&gt;
  
  
  What a Data Engineer is NOT:
&lt;/h3&gt;

&lt;p&gt;There are a lot of Data related roles out there with blurry boundaries between them. In my opinion, this could be due to the rapid evolution of how we (or the systems) produce data and consume it, which adds scope to existing data roles or creates new roles entirely.&lt;br&gt;
I just want to clear the confusion about what a data engineer is not to better understand what a data engineer actually is!&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A Data Engineer should be familiar with statistics if their pipeline requires statistical transformations but it's a "Data Analyst's" role to make sense of the data and use statistical analysis to better understand it and uncover hidden insights!&lt;/li&gt;
&lt;li&gt;A Data Engineer might be familiar with statistical modelling and machine learning algorithms, but it is the job of a "Data Scientist" to create models from data for machine learning use cases.&lt;/li&gt;
&lt;li&gt;A Data Engineer should know how to handle production workloads, but it is the job of a "Machine Learning Engineer" to deploy production Machine learning systems.&lt;/li&gt;
&lt;li&gt;A Data Engineer works a lot with databases, but it's the role of a "Database Administrator" to handle the administration part of a database like users' privileges, DB maintenance, backups, ..., etc.&lt;/li&gt;
&lt;li&gt;A Data Engineer could know a bit of Software Engineering (in fact, it will be very beneficial if they do!) but it is the job of a "Software Engineer" to create apps even if data related.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Like we've seen in the &lt;em&gt;Data Engineering&lt;/em&gt; definition, A Data Engineer should focus on the design, creating, and maintaining their Data Pipeline that works within the &lt;em&gt;Data Engineering Lifecycle&lt;/em&gt; framework working with some data role like DB Admins or Software engineers to serve other Data roles like the Data Analysts and/or Scientists!&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion:
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;Data Engineering&lt;/em&gt; is a relatively new Data role and is still evolving in my opinion. I just wanted here to give you a taste of what we will be handling in the &lt;em&gt;Data Analytics Made Simpler&lt;/em&gt; series and to help you identify the scope of a Data Engineer.&lt;/p&gt;

&lt;h3&gt;
  
  
  References:
&lt;/h3&gt;

&lt;p&gt;I've used the same approach explaining Data Engineer as the "Fundamentals of Data Engineering" book by &lt;em&gt;Joe Reis and Matt Housley&lt;/em&gt; and explained the &lt;em&gt;Data Engineering Lifecycle&lt;/em&gt; as they did in their book. A highly recommended read that mostly doesn't require prior knowledge of Data Engineering.&lt;/p&gt;

</description>
      <category>dataengineering</category>
      <category>beginners</category>
      <category>learning</category>
    </item>
    <item>
      <title>دورة حياة هندسة البيانات - الأساسيات - نظري</title>
      <dc:creator>Fady GA 😎</dc:creator>
      <pubDate>Sun, 15 Oct 2023 15:34:31 +0000</pubDate>
      <link>https://dev.to/fadygrab/dwr-hy-hnds-lbynt-lssyt-nzry-2ii5</link>
      <guid>https://dev.to/fadygrab/dwr-hy-hnds-lbynt-lssyt-nzry-2ii5</guid>
      <description>&lt;p&gt;هذه هي المقالة الأولى في سلسلة تحليلات البيانات الخاصة بي.  لمعرفة المزيد عنها، يرجى زيارة &lt;a href="https://www.fadyinthecloud.click/data-analytics-made-simpler/"&gt;موقعها&lt;/a&gt; على الإنترنت حيث أقدم المزيد من التفاصيل حول ما هي بالضبط هذه السلسلة وما هو دافعي للقيام بشيء من هذا القبيل وكيف أخطط لإصدار محتواها!&lt;/p&gt;

&lt;p&gt;أردت أن أبدأ سلسلتي برسم صورة ذهنية لما ستغطيه. في هذه المقالة ، سأشرح ما أعنيه ب "تحليلات البيانات" الموجود في عنوان السلسلة وسأقدم مزيدا من التفاصيل حول ماهية هندسة البيانات بالضبط وسأشرح مكوناتها العامة من خلال إطار دورة حياة هندسة البيانات.&lt;/p&gt;

&lt;p&gt;لقراءة نفس المقالة باللغة الانجليزية، اذهب &lt;a href="https://dev.to/fadygrab/data-engineering-lifecycle-basics-theory-2e60"&gt;هنا&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  جدول المحتويات :
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;ما أعنيه بتحليلات البيانات&lt;/li&gt;
&lt;li&gt;ما هي هندسة البيانات&lt;/li&gt;
&lt;li&gt;دورة حياة هندسة البيانات&lt;/li&gt;
&lt;li&gt;ما لا يفعله مهندس البيانات&lt;/li&gt;
&lt;li&gt;الخلاصة&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  ما أعنيه بتحليلات البيانات:
&lt;/h3&gt;

&lt;p&gt;مصطلح تحليلات البيانات واسع جدا ويمكن أن يكون له معاني مختلفة لأشخاص مختلفين! لذا ، أريد أن أكون واضحا بشأن ذلك.&lt;/p&gt;

&lt;p&gt;عندما أتحدث عن تحليلات البيانات ، أتخيل تقريبا الرسم التالي في رأسي.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ixXzAts8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oaki5tiu5v7j1hllulln.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ixXzAts8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oaki5tiu5v7j1hllulln.png" alt="data analytics" width="600" height="322"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;في رأسي ، جزء كبير من تحليلات البيانات هو هندسة البيانات التي سأشرحها قريبا ، بغض النظر عن حجم البيانات أو تنوعها أو سرعتها (سيصبح فهم هذه المصطلحات أسهل بمرور الوقت ، ثق بي 😉).&lt;br&gt;
الجزء المتبقي هو تصور البيانات. بالنسبة لي ، هذا هو الشيء الطبيعي الذي يجب القيام به مع البيانات بعد أن قمنا بمعالجتها  في مرحلة هندسة البيانات ، لكنه ليس الشيء الوحيد الذي يمكننا القيام به.&lt;br&gt;
هذا ليس تعريفا رسميا لتحليلات البيانات ولكن هذه هي الطريقة التي سأتناولها بها في سلسلتي.&lt;/p&gt;

&lt;h3&gt;
  
  
  ما هي هندسة البيانات؟
&lt;/h3&gt;

&lt;p&gt;النسخة المبسطة من تعريف هندسة البيانات هي كما يلي:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;هندسة البيانات هي المجال المعني بنقل البيانات من أنظمة المصدر إلى أنظم الوجهة مع تطبيق التحولات عليها وفقا لمتطلبات العمل.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;على سبيل المثال ، تخيل مؤسسة بها عدة تطبيقات خاصة بها. نظرا لاختلاف فرق التطوير ، يقوم كل تطبيق بإنشاء سجلاته بتنسيق خاص به مختلف عن التطبيقات الأخرى ولكنه لا يزال يحتوي على نفس المعلومات. الآن ، تريد الإدارة لوحة معلومات لإظهار انماط استخدام المستخدمين على مدار الأشهر ال 3 الماضية مجمعة لجميع التطبيقات.&lt;/p&gt;

&lt;p&gt;عملية جمع السجلات من كل تطبيق ، وتوحيد تنسيقها واستخراج بيانات وصول المستخدمين ثم تجميعها ، وأخيرا تحميل البيانات المجمعة إلى مستودع بيانات (المزيد حول ذلك لاحقا في السلسلة) التي ستستخدمها لوحة المعلومات في النهاية ، تسمى ، لقد خمنت ذلك ، هندسة البيانات!&lt;/p&gt;

&lt;p&gt;الآن بعد أن أصبح لديك فكرة عن ماهية هندسة البيانات ، يجب أن أضيف إلى التعريف السابق أن هندسة البيانات ليست مجرد حركة البيانات والتحولات المطبقة عليها فقط، إنها تصميم وتنفيذ وصيانة النظام الآلي بأكمله (يسمى ، خط الأنابيب) الذي يؤدي كل شيء!&lt;/p&gt;

&lt;p&gt;قد تحصل الآن على انطباع بأن هندسة البيانات هي شيء له علاقة بالمؤسسات الكبيرة فقط! لن تكون مخطئا! لكنك لن تكون صحيحا تماما أيضا 😁. فكر في الأمر ، إذا كان لديك مجموعة من ملفات اكسل موجودة في الكمبيوتر المحمول الخاص بك ، على سبيل المثال ، تحتوي على المبيعات الشهرية على مدار ال 15 عاما الماضية مع كل شهر موجود في الملف الخاص به وقمت بإنشاء برنامج بايثون لفتح الملفات ، والحصول على إجمالي المبيعات الشهرية ، وأخيرا تحميل كل الارقام في تقرير مجمع. وفقا للتعريف المذكور ، لقد قمت نوعا ما بممارسة هندسة البيانات!&lt;/p&gt;

&lt;h3&gt;
  
  
  دورة حياة هندسة البيانات:
&lt;/h3&gt;

&lt;p&gt;سأبدأ هذا القسم بعرض الرسم الذي يوضح ماهية دورة حياة هندسة البيانات ثم سأشرح بالتفضيل لاحقاً:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--HoImMrVY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mglu15bflqp1tufxjjbv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--HoImMrVY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mglu15bflqp1tufxjjbv.png" alt="de lifecycle" width="800" height="227"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;إذا تحدثنا عن هندسة البيانات مع ترك التفاصيل الفنية واختيار الأدوات المستخدمة، فسنحصل على "إطار" يحاول شرح كيف تعمل هندسة البيانات ، أي دورة حياة هندسة البيانات!&lt;/p&gt;

&lt;p&gt;إذا ألقيت نظرة فاحصة على الشكل السابق ، فستتمكن من التحقق من تعريف هندسة البيانات السابق ذكره! سترى أننا نستخرج أو "نستوعب" البيانات من المصادر التي "تولدها" ، ونطبق "التحولات" عليها ، ثم نقوم أخيرا بتحميلها أو "تقديمها" إلى أنظمة الوجهة التي ستقوم بشكل عام إما بتحليلها أو تغذية انظمة تعلم الالة.&lt;/p&gt;

&lt;p&gt;ربما لاحظت أن هناك كلمة "تخزين" تمتد تحت "الاستخراج" و "التحويل" و "التقديم". هذا لأنه يمكننا بالفعل تخزين البيانات في أي مرحلة من مراحل دورة الحياة. على سبيل المثال ، هناك بنية شائعة عند تصميم "برك البيانات"  (سنعرف عن "برك البيانات" لاحقا في السلسلة) هي تخزين البيانات الأولية قبل المعالجة. بعد ذلك ، نقوم ببعض تحويلات التصفية والتنظيف ثم تخزين البيانات الناتجة في مكان مخصص. وأخيرا ، نقوم ببعض التجميعات للبيانات المعالجة ونقوم بتخزينها أيضا في مكان مخصص.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;وهذا ما يسمى بنية الميدالية حيث تسمى المنطقة الخام المرحلة البرونزية ، وتسمى المنطقة المعالجة المرحلة الفضية ، وتسمى المنطقة المجمعة المرحلة الذهبية.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;شيء آخر يجب معرفته عن دورة حياة هندسة البيانات هو أنها ليست تدفقا متسلسلا! بمعنى أنه يمكنك (وغالبا ما سوف) تقوم بتنفيذ المراحل المذكورة خارج الترتيب الموضح و / أو تنفيذها أكثر من مرة. على سبيل المثال ، يمكننا تقديم البيانات التي تم استخراجها مباشرة إلى بعض أنظمة "مستهلكين البيانات" قبل أي تحويل ثم إجراء التحولات وتحميل الناتج النهائي إلى أنظمة أخرى.&lt;/p&gt;

&lt;h3&gt;
  
  
  ما لا يفعله مهندس البيانات
&lt;/h3&gt;

&lt;p&gt;هناك الكثير من الأدوار المتعلقة بالبيانات ولكن الحدود ضبابية بينها. في رأيي ، قد يكون هذا بسبب التطور السريع لكيفية ننتج (أو تنتج الأنظمة) البيانات واستهلاكها ، مما قد يضيف مسؤوليات جديدة لأدوار البيانات الحالية أو يخلق أدوارا جديدة تماما.&lt;/p&gt;

&lt;p&gt;أريد فقط إزالة الالتباس حول ما لا يفترض ان يقوم به مهندس البيانات لفهم ما الذي يجب ان يقوم به مهندس البيانات!&lt;/p&gt;

&lt;p&gt;يجب أن يكون مهندس البيانات على دراية بعلوم الاحصاء حيث من الممكن ان يقوم بتحويلات احصائية على البيانات ، لكن هذا دور "محلل البيانات"! هو الذي يفهم البيانات باستخدام التحليل الإحصائي لفهمها بشكل أفضل والكشف عن الدلائل الخفية!&lt;/p&gt;

&lt;p&gt;قد يكون مهندس البيانات على دراية بالنماذج الإحصائية وخوارزميات  تعلم الآلة ، ولكنها مهمة "عالم البيانات" إنشاء نماذج من البيانات لحالات استخدام تعلم الآلة.&lt;/p&gt;

&lt;p&gt;يجب أن يعرف مهندس البيانات كيفية التعامل مع الانظمة التي تعمل في بيئة الانتاج ،  ولكنا مهمة "مهندس اتعلم الآلة" نشر أنظمة تعلم الآلة للإنتاج.&lt;/p&gt;

&lt;p&gt;يعمل مهندس البيانات كثيرا مع قواعد البيانات ، ولكنه دور "مسؤول قاعدة البيانات" هو التعامل مع الجزء الإداري من قاعدة البيانات مثل امتيازات المستخدمين ، وصيانة قاعدة البيانات ، والنسخ الاحتياطية ، ...&lt;/p&gt;

&lt;p&gt;يمكن لمهندس البيانات معرفة القليل من هندسة البرمجيات (في الواقع ، سيكون من المفيد جدا إذا فعل ذلك!) ولكنها مهمة "مهندس البرمجيات" هي إنشاء تطبيقات حتى لو كانت متعلقة بالبيانات.&lt;/p&gt;

&lt;p&gt;كما رأينا في تعريف هندسة البيانات ، يجب أن يركز مهندس البيانات على تصميم وإنشاء وصيانة "خط أنابيب" البيانات الخاص به الذي يعمل ضمن إطار دورة حياة هندسة البيانات حيث يعمل مع بعض أدوار البيانات مثل مسؤولي قواعد البيانات أو مهندسي البرمجيات لخدمة أدوار البيانات الأخرى مثل محللي البيانات و / أو علماء البيانات!&lt;/p&gt;

&lt;h3&gt;
  
  
  الخلاصة:
&lt;/h3&gt;

&lt;p&gt;هندسة البيانات هي مجال جديد نسبيا ولا تزال في طور النمو في &lt;br&gt;
رأيي. أردت فقط هنا أن أقدم لك لمحة عما سنتعامل معه في سلسلة "Data Analytics Made Simpler" &lt;br&gt;
ولمساعدتك في تحديد هوية مهندس البيانات.&lt;/p&gt;

&lt;h3&gt;
  
  
  المصادر
&lt;/h3&gt;

&lt;p&gt;لقد استخدمت كتاب&lt;br&gt;
"Fundamentals of Data Engineering" Joe Reis and Matt Housley&lt;br&gt;
في اعداد هذه المقالة حيث يعتبر هذا الكتاب من المصادر الجيدة جدا للتعرف على مجال هندسة البيانات بدون الحاجة لخبرة مسبقة&lt;/p&gt;

</description>
      <category>dataengineering</category>
      <category>beginners</category>
      <category>learning</category>
    </item>
    <item>
      <title>Learning Rust 🦀: 17 - Rust Collections: Strings - How complex can it be?!</title>
      <dc:creator>Fady GA 😎</dc:creator>
      <pubDate>Fri, 13 Oct 2023 15:26:54 +0000</pubDate>
      <link>https://dev.to/fadygrab/learning-rust-17-rust-collections-strings-how-complex-can-it-be-855</link>
      <guid>https://dev.to/fadygrab/learning-rust-17-rust-collections-strings-how-complex-can-it-be-855</guid>
      <description>&lt;p&gt;The second common Rust collection we will visit is the &lt;strong&gt;String&lt;/strong&gt;! Rust has taken a ... "different" approach when working with the &lt;strong&gt;String&lt;/strong&gt; type than other programming languages but seriously, how complex can it be?! Let's find out!&lt;br&gt;
&lt;a href="https://i.giphy.com/media/waUr4629vk5QhD558r/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/waUr4629vk5QhD558r/giphy.gif" alt="hard" width="480" height="480"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;h4&gt;
  
  
  ⚠️ Remember!
&lt;/h4&gt;

&lt;p&gt;You can find all the code snippets for this series in its &lt;a href="https://github.com/FadyGrAb/rust-learning-series"&gt;accompanying repo&lt;/a&gt;&lt;br&gt;&lt;br&gt;
If you don't want to install Rust locally, you can play with all the code of this series in the official &lt;a href="https://play.rust-lang.org/?version=stable&amp;amp;mode=debug&amp;amp;edition=2021"&gt;Rust Playground&lt;/a&gt; that can be found on its official page.&lt;/p&gt;

&lt;p&gt;⚠️⚠️ The articles in this series are loosely following the contents of "The Rust Programming Language, 2nd Edition" by Steve Klabnik and Carol Nichols in a way that reflects my understanding from a Python developer's perspective.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;⭐ I try to publish a new article every week (maybe more if the Rust gods 🙌 are generous 😁) so stay tuned 😉. I'll be posting "new articles updates" on my &lt;a href="https://www.linkedin.com/in/fady-abdelmassih"&gt;LinkedIn&lt;/a&gt; and &lt;a href="https://twitter.com/fadyGrAb"&gt;Twitter&lt;/a&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  Table of Contents:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Why the String type is considered a collection?&lt;/li&gt;
&lt;li&gt;Creating a new String&lt;/li&gt;
&lt;li&gt;Updating a String&lt;/li&gt;
&lt;li&gt;Indexing into Strings&lt;/li&gt;
&lt;li&gt;The String is not simple!&lt;/li&gt;
&lt;li&gt;Iterating over a String&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Why the String type is considered a collection?
&lt;/h3&gt;

&lt;p&gt;That's easy, because in Rust is a "collection" of bytes 😁!&lt;br&gt;
Actually, the String type in Rust is a wrapper of a &lt;code&gt;u8&lt;/code&gt; &lt;a href="https://dev.to/fadygrab/learning-rust-16-collections-vectors-43nm"&gt;Vector&lt;/a&gt; (&lt;code&gt;Vec&amp;lt;u8&amp;gt;&lt;/code&gt;) as it contains all the methods available for the Vector  type plus other methods that makes working with strings easier. It holds the bytes of a UTF-8 encoded string which might become problematic as we will see later.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You may recall that &lt;strong&gt;String&lt;/strong&gt; type is located in the Heap and it's of variable length while &lt;strong&gt;string slice&lt;/strong&gt; &lt;code&gt;str&lt;/code&gt; (usually used as a reference &lt;code&gt;&amp;amp;str&lt;/code&gt;) is located in the stack.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3&gt;
  
  
  Creating a new String:
&lt;/h3&gt;

&lt;p&gt;We can create new String as follows:&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;my_string&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;=&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;new&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will create a new and empty "immutable" &lt;strong&gt;String&lt;/strong&gt; in &lt;code&gt;my_string&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Sometimes, we would want to create a String from a seed text instead of an empty one. To do that, we can either use the &lt;code&gt;from&lt;/code&gt; method of the &lt;strong&gt;String&lt;/strong&gt; type or use the convenient &lt;code&gt;to_string&lt;/code&gt; method that is avaible to some of Rust's core types.&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;my_string&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;=&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="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"from: {my_string}"&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;word&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt; &lt;span class="o"&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;my_string&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;=&lt;/span&gt; &lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="nf"&gt;.to_string&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;"to_string: {my_string}"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The use of &lt;code&gt;from&lt;/code&gt; or &lt;code&gt;to_string&lt;/code&gt; is just a matter of style and readability!&lt;/p&gt;

&lt;h3&gt;
  
  
  Updating a String:
&lt;/h3&gt;

&lt;p&gt;There are more than one way to update a "mutable" &lt;strong&gt;String&lt;/strong&gt;, we can use the &lt;code&gt;push_str&lt;/code&gt; method that the &lt;strong&gt;String&lt;/strong&gt; type implements to append the new string passed to it to the original &lt;strong&gt;String&lt;/strong&gt;. As the &lt;strong&gt;String&lt;/strong&gt; type is a wrapper for the &lt;strong&gt;Vector&lt;/strong&gt; type, it to has a &lt;code&gt;push&lt;/code&gt; method that - similar to &lt;code&gt;push_str&lt;/code&gt; - pushes a &lt;em&gt;Char&lt;/em&gt; type to the original &lt;strong&gt;String&lt;/strong&gt;. Let's see both of those in action:&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;new_str&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="n"&gt;new_str&lt;/span&gt;&lt;span class="nf"&gt;.push_str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"There"&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;"Greeting: {new_str}"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;new_str&lt;/span&gt;&lt;span class="nf"&gt;.push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sc"&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;"New greeting: {new_str}"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Greeting: Hello, There
New greeting: Hello, There!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code does what you expect it to do. We've used &lt;code&gt;push_str&lt;/code&gt; to append "There" to the original &lt;strong&gt;String&lt;/strong&gt; ("Hello, "). Next, we've used &lt;code&gt;push&lt;/code&gt; to append "!" at the end of the original &lt;strong&gt;String&lt;/strong&gt; now containing "Hello, There".&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Notice how the &lt;code&gt;push&lt;/code&gt; takes a &lt;strong&gt;Char&lt;/strong&gt; type since the "!" is passed to it with single quotes denoting the &lt;strong&gt;Char&lt;/strong&gt; type.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Another way to update a string is the "+" operator, let's check it out:&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;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="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;"There!"&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;s3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s1&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;s2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In short, after executing this code &lt;code&gt;s3&lt;/code&gt; will hold "Hello, There!" but there is a lot that is going on here!&lt;br&gt;
You may notice that &lt;code&gt;s1&lt;/code&gt; is "moved" into &lt;code&gt;s3&lt;/code&gt; i.e. we can't use &lt;code&gt;s1&lt;/code&gt; anymore in our app after the &lt;code&gt;s3&lt;/code&gt; line and we are "borrowing" &lt;code&gt;s2&lt;/code&gt;. This is not a random thing; this has to do with the "+" operator as it uses the &lt;code&gt;add&lt;/code&gt; method with has the signature:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fn add(self, s: &amp;amp;str) -&amp;gt; String
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, the &lt;code&gt;add&lt;/code&gt; method takes ownership of &lt;code&gt;self&lt;/code&gt; (the &lt;code&gt;s1&lt;/code&gt; in our example) and takes &lt;code&gt;s&lt;/code&gt; as a &lt;strong&gt;string literal&lt;/strong&gt; which is supposed to represent &lt;code&gt;s2&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You may wonder how this code works as &lt;code&gt;s2&lt;/code&gt; is a &lt;strong&gt;String&lt;/strong&gt; not a &lt;strong&gt;&amp;amp;str&lt;/strong&gt; in our example. At this point, let's just say that the Rust compiler can coerce (convert) the &lt;strong&gt;&amp;amp;String&lt;/strong&gt; type into &lt;strong&gt;&amp;amp;str&lt;/strong&gt; and we will revisit that later.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now, consider the example below:&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;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="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;"There"&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;s3&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;"!"&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;s4&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;", "&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;s2&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;s3&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;"s4 is {s4}"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hello, There!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But as you can see, it may become unnecessarily complicated to use the "+" operator when try to modify a &lt;strong&gt;String&lt;/strong&gt; with a relatively big number of string literals. This is a perfect use case for yet another new Rust macro, &lt;code&gt;format!&lt;/code&gt;. Now, let's see how we can use it in the previous 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;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="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;"There"&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;s3&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;"!"&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;s4&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nd"&gt;format!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"{s1}, {s2}{s3}"&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;"s4 is {s4}"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will produce the exact same result as before but notice that &lt;code&gt;s1&lt;/code&gt; doesn't "move" into &lt;code&gt;s4&lt;/code&gt; which can be convenient if we were planning to use it later in the app.&lt;/p&gt;

&lt;h3&gt;
  
  
  Indexing into Strings:
&lt;/h3&gt;

&lt;p&gt;Up until now, everything more or less looks the same as other programming languages but here is where the Rust fun starts &lt;br&gt;
&lt;a href="https://i.giphy.com/media/KZkHjCgk4fH8XAe4Gs/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/KZkHjCgk4fH8XAe4Gs/giphy.gif" alt="fun" width="480" height="270"&gt;&lt;/a&gt;&lt;br&gt;
In Python, doing something like this is perfectly fine:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Hello"&lt;/span&gt;
&lt;span class="n"&gt;H&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now given that the &lt;strong&gt;String&lt;/strong&gt; type is in fact a wrap for a &lt;strong&gt;Vector&lt;/strong&gt; and the later can have its elements accessed by using zero-based indices, so the following should work, right?&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;my_string&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;"Greetings 😉 !"&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;G&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;my_string&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Wrong! This will produce the following error:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;the type `String` cannot be indexed by `{integer}`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For reasons that will become clear in just seconds, Rust only permits you to "slice" the &lt;strong&gt;String&lt;/strong&gt; type! So, something like the following code will compile and work:&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;my_string&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;"Greetings 😉 !"&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;Gree&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;my_string&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="mi"&gt;4&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;"{Gree}"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will work and will print &lt;code&gt;Gree&lt;/code&gt; in the terminal.&lt;br&gt;
Now, let's try this again and this time we will try to extract the "wink" emoji that is at position 10 with the preceding and trailing spaces:&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;my_string&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;"Greetings 😉 !"&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;wink&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;my_string&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="mi"&gt;12&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;"{wink}"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we are starting our slice at the first space at position 9 then end it at the trailing space at position 11 (the end isn't inclusive here). The app compiles but it will panic!&lt;/p&gt;

&lt;p&gt;The reason behind that is the &lt;strong&gt;String&lt;/strong&gt; stores - like I've mentioned - UTF-8 encoded "bytes" and note the visual character that we see! It turned out that "😉" is four bytes long, and although we've correctly set the slice's start at position 9 as all  the character up to this point are using only one byte, the slice's end happens to be the second byte of the "😉" which isn't a printable character and that why the app panicked! Although, &lt;strong&gt;String&lt;/strong&gt; slicing is perfectly legal in Rust, it can be dangerous at runtime!&lt;/p&gt;

&lt;p&gt;Now, why doesn't Rust allow indexing? &lt;br&gt;
It's because of the UTF-8 encoding thing to avoid cases when you could return unprintable characters and introducing bugs that might not be discoverable immediately, Rust doesn't compile that code!&lt;/p&gt;

&lt;p&gt;So why use slicing as it also can return unprintable characters? &lt;br&gt;
Slicing is Rust's way to tell you to be more specific to return a range of bytes if you know what you are doing!&lt;/p&gt;
&lt;h3&gt;
  
  
  The String is not simple!
&lt;/h3&gt;

&lt;p&gt;String in Rust isn't that complex but isn't that simple either!&lt;br&gt;
Generally, you can represent a String by either its "bytes" scalars or its "characters" (&lt;code&gt;char&lt;/code&gt; type). For example, the string used in the previous example "Greetings 😉 !" has the following scalar bytes representation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;71
114
101
101
116
105
110
103
115
32
240
159
152
137
32
33
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And the following "characters" representations:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;G
r
e
e
t
i
n
g
s

😉

!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Notice how there are 13 characters and 16 bytes representing the same string!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This distinction in strings representation can be abstracted away in some programming languages such as Python and the developer doesn't think much of how the strings are represented (unless he needs to, say when sending strings over the network). But in Rust, it by design forces the developers to think about how strings are represented, namely the UTF-8 encoding, to avoid bugs early on in the development.&lt;/p&gt;

&lt;h3&gt;
  
  
  Iterating over a String:
&lt;/h3&gt;

&lt;p&gt;Finally, the &lt;strong&gt;String&lt;/strong&gt; type has some useful methods to iterate over its elements.&lt;br&gt;
For example, if we want to iterate over the string's characters representation, we can use the following:&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;for&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;my_string&lt;/span&gt;&lt;span class="nf"&gt;.chars&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;"{c}"&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;Similarly, if we want to iterate over its bytes, we use:&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;for&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;my_string&lt;/span&gt;&lt;span class="nf"&gt;.as_bytes&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;"{b}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At the end, Rust's &lt;strong&gt;String&lt;/strong&gt; isn't that complex! It's a collection of the UTF-8 encoded bytes of a string and Rust wants you to think of that from the start. In the next one we will explore another common Rust collection, the Hash map. See you then 👋&lt;/p&gt;

</description>
      <category>learning</category>
      <category>rust</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Learning Rust 🦀: 16 - Rust Collections: Vectors</title>
      <dc:creator>Fady GA 😎</dc:creator>
      <pubDate>Thu, 05 Oct 2023 13:12:11 +0000</pubDate>
      <link>https://dev.to/fadygrab/learning-rust-16-collections-vectors-43nm</link>
      <guid>https://dev.to/fadygrab/learning-rust-16-collections-vectors-43nm</guid>
      <description>&lt;p&gt;Now we will have a look at some of Rust's most common &lt;strong&gt;Collections&lt;/strong&gt;, The &lt;strong&gt;Vector&lt;/strong&gt;, &lt;strong&gt;String&lt;/strong&gt; (yes! String type is a collection of characters 😎), and &lt;strong&gt;Hash Map&lt;/strong&gt; types. We will start first with Cyborg mmm... I mean, Vector 😁&lt;br&gt;
&lt;a href="https://i.giphy.com/media/u2sdwCFtYDXBs07t8Y/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/u2sdwCFtYDXBs07t8Y/giphy.gif" alt="cyborg" width="480" height="270"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;h4&gt;
  
  
  ⚠️ Remember!
&lt;/h4&gt;

&lt;p&gt;You can find all the code snippets for this series in its &lt;a href="https://github.com/FadyGrAb/rust-learning-series"&gt;accompanying repo&lt;/a&gt;&lt;br&gt;&lt;br&gt;
If you don't want to install Rust locally, you can play with all the code of this series in the official &lt;a href="https://play.rust-lang.org/?version=stable&amp;amp;mode=debug&amp;amp;edition=2021"&gt;Rust Playground&lt;/a&gt; that can be found on its official page.&lt;/p&gt;

&lt;p&gt;⚠️⚠️ The articles in this series are loosely following the contents of "The Rust Programming Language, 2nd Edition" by Steve Klabnik and Carol Nichols in a way that reflects my understanding from a Python developer's perspective.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;⭐ I try to publish a new article every week (maybe more if the Rust gods 🙌 are generous 😁) so stay tuned 😉. I'll be posting "new articles updates" on my &lt;a href="https://www.linkedin.com/in/fady-abdelmassih"&gt;LinkedIn&lt;/a&gt; and &lt;a href="https://twitter.com/fadyGrAb"&gt;Twitter&lt;/a&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  Table of Contents:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Creating new Vectors&lt;/li&gt;
&lt;li&gt;Updating Vectors&lt;/li&gt;
&lt;li&gt;Reading Vectors elements&lt;/li&gt;
&lt;li&gt;Using Enum to make a Vector contain multiple types&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Creating new Vectors:
&lt;/h3&gt;

&lt;p&gt;Before I show you how to create a new Vector, there is something you need to know. Unlike Arrays or Tuples that can hold a list of items themselves, Vectors are created in the &lt;strong&gt;Heap&lt;/strong&gt;. Meaning, that &lt;em&gt;they don't have to be of known length at compile time!&lt;/em&gt; But similar to Arrays and Tuples, they must contain elements of the same type.&lt;br&gt;&lt;br&gt;
We can create an empty Vector as follows:&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;v&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Vec&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;Vec&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Because the compiler doesn't know the type of the elements of the newly created Vector, there type must be explicitly stated using &lt;code&gt;Vec&amp;lt;T&amp;gt;&lt;/code&gt; (Yes, generics which we will talk about them later). So here, we are creating an empty Vector that will hold element of type &lt;code&gt;i32&lt;/code&gt;.&lt;br&gt;&lt;br&gt;
Also, we can create prefilled Vectors like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;v&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By using the &lt;code&gt;vec!&lt;/code&gt; macro, we are creating also a &lt;code&gt;Vec&amp;lt;i32&amp;gt;&lt;/code&gt; Vector as &lt;code&gt;i32&lt;/code&gt; is the default integer type.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Vectors - like any other Rust type - are immutable by default meaning that you can't add or remove items from them. To make them mutable, we use the &lt;code&gt;mut&lt;/code&gt; keyword.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Updating Vectors:
&lt;/h3&gt;

&lt;p&gt;The Vector type has a lot of useful methods that can update its elements. &lt;code&gt;push&lt;/code&gt; and &lt;code&gt;pop&lt;/code&gt; are very common in such operations. Consider the following example where we create an empty Vector and add 3 elements to it then remove one:&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="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Vec&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;Vec&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="nf"&gt;.push&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="n"&gt;v&lt;/span&gt;&lt;span class="nf"&gt;.push&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="n"&gt;v&lt;/span&gt;&lt;span class="nf"&gt;.push&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="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Length of v is: {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="nf"&gt;.len&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
    &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="nf"&gt;.pop&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;"New length of v is: {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;v&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first print will give you &lt;code&gt;Length of v is: 3&lt;/code&gt; and the second one will print &lt;code&gt;New length of v is: 2&lt;/code&gt;. &lt;code&gt;push&lt;/code&gt; adds elements to the Vector. I like to imagine the Vector as a "stack" and &lt;code&gt;push&lt;/code&gt; adds a new element each time at the top of the stack which means that the very first item that was added using &lt;code&gt;push&lt;/code&gt; will be at the bottom of the stack. &lt;code&gt;pop&lt;/code&gt; on the other hand, remove the top item from this stack.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;len&lt;/code&gt; is used to get the Vector "length" which in this case is the elements count.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Reading Vectors elements:
&lt;/h3&gt;

&lt;p&gt;You can access the Vector's elements using the square brackets "[ ]" similar to arrays (or Python's Lists and Tuples). We can also use the &lt;code&gt;get&lt;/code&gt; method as follows:&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="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;v&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;first_element&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;v&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="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"First element is {first_element}"&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;second_element&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Option&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="nf"&gt;.get&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;match&lt;/span&gt; &lt;span class="n"&gt;second_element&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="n"&gt;second&lt;/span&gt;&lt;span class="p"&gt;)&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;"Second element is {second}"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="nb"&gt;None&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;"There isn't a second element!"&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;Vectors index start at 0 so &lt;code&gt;&amp;amp;v[0]&lt;/code&gt; will return a reference to the first element in the Vector which in this case will be &lt;code&gt;1&lt;/code&gt;. &lt;code&gt;get&lt;/code&gt; returns an &lt;strong&gt;Option&lt;/strong&gt; type (remember the &lt;a href="https://dev.to/fadygrab/learning-rust-14-option-enum-an-enum-and-pattern-matching-use-case-1dgf"&gt;Option type&lt;/a&gt;?) as the index passed to &lt;code&gt;get&lt;/code&gt; can be outside of the Vector boundaries, then it will return the &lt;strong&gt;None&lt;/strong&gt; variant of the &lt;strong&gt;Option&lt;/strong&gt; type. Otherwise, it will return &lt;strong&gt;Some(T)&lt;/strong&gt;. Here, &lt;code&gt;get(1)&lt;/code&gt; should return a &lt;strong&gt;Some(i32)&lt;/strong&gt; variant as the element at index &lt;code&gt;1&lt;/code&gt; exists.&lt;br&gt;&lt;br&gt;
Now let's see what will happen if we requested an "out of bound" element:&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;v2&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;val&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;v2&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will compile just fine but will make the application "panic"! This behavior can be used if you want to crash the program if an out-of-bound value is entered. Now let's look at &lt;code&gt;get&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;v2&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;val&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;v2&lt;/span&gt;&lt;span class="nf"&gt;.get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Returns None&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nb"&gt;None&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;val&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;"None is returned!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The application won't "panic" in this case and &lt;code&gt;None is returned!&lt;/code&gt; will be printed out.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;if let&lt;/code&gt; is another way to do pattern matching that I mentioned &lt;a href="https://dev.to/fadygrab/learning-rust-14-option-enum-an-enum-and-pattern-matching-use-case-1dgf#if-let-alternate-way-to-pattern-matching"&gt;here&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;One last thing before we leave this point, do you remember that we can't have both mutable and immutable references for a type in the same scope? This applies for Vector elements as well! Let's examine the following:&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;v&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;first&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;v&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="n"&gt;v&lt;/span&gt;&lt;span class="nf"&gt;.push&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="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"{first}"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This should compile just fine, right? We are using an immutable reference for the first item in the Vector and then adding a new item at the end of the vector, those should be unrelated, right? No, they are related and the code won't be compiled!&lt;br&gt;&lt;br&gt;
Do you recall that Vectors are stored in the Heap? Which means that they can expand as they store their elements next to each other. And if the allocated memory to the Vector isn't large enough to hold all of its new elements, a new memory location will be assigned to it. Meaning that the &lt;code&gt;let first = &amp;amp;v[0];&lt;/code&gt; will be referring to an invalid location in memory if the &lt;code&gt;v.push(4);&lt;/code&gt; expression caused the Vector to be reallocated. Therefore, &lt;code&gt;prinln!("{first}");&lt;/code&gt; will cause a compilation error!  &lt;/p&gt;
&lt;h3&gt;
  
  
  Reading Vectors elements:
&lt;/h3&gt;

&lt;p&gt;Similar to Arrays, we can iterate over a Vector like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;v&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;i&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;v&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;"{i}"&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 will result in:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1
2
3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can also change the values of the Vector's elements while iterating:&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;v2&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;20&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;40&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;v2&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="mi"&gt;20&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;"Modified vector:"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;v2&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;"{i}"&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 will print:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0
20
40
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;The "*" before &lt;code&gt;i&lt;/code&gt; in the first &lt;code&gt;for&lt;/code&gt; is called "dereference" operator which we will talk about in future articles.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Using Enum to make a Vector contain multiple types:
&lt;/h3&gt;

&lt;p&gt;Vectors are lists of elements of the same type but we can encapsulate different types with &lt;strong&gt;Enum&lt;/strong&gt; in a custom type and use it with Victors! Let's see that in action:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="nd"&gt;#[derive(Debug)]&lt;/span&gt;
&lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;Item&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;Apple&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;u8&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nf"&gt;Banana&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;u8&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nf"&gt;Tape&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;f32&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nf"&gt;Book&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;shopping_cart&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="nn"&gt;Item&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;Apple&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nn"&gt;Item&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;Banana&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="nn"&gt;Item&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;Book&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;"Moby-Dick"&lt;/span&gt;&lt;span class="p"&gt;)),&lt;/span&gt;
    &lt;span class="nn"&gt;Item&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;Tape&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;5.5&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;"Items in the shopping cart:"&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="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;shopping_cart&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;"{item:?}"&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;When running this code, we will have the following output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Items in the shopping cart:
Apple(10)
Banana(5)
Book("Moby-Dick")
Tape(5.5)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we are building a &lt;code&gt;shopping_cart&lt;/code&gt; Vector. But since Vectors can only have elements of the same type, we have created the &lt;code&gt;Item&lt;/code&gt; &lt;strong&gt;Enum&lt;/strong&gt; that has only 4 variants, &lt;em&gt;Apple&lt;/em&gt; that takes their count, &lt;em&gt;Banna&lt;/em&gt; that also takes their count, &lt;em&gt;Tape&lt;/em&gt; that takes its length, and finally &lt;em&gt;Book&lt;/em&gt; that takes its title. Then, we filled our &lt;code&gt;shopping_cart&lt;/code&gt; Vector with variants of the &lt;strong&gt;Item&lt;/strong&gt; enum which its variants encapsulate other data types and are technically of the same type. This technique will work if we know all the variants that we can use. But if we don't, the &lt;strong&gt;Enum&lt;/strong&gt; won't work and we will have to use &lt;strong&gt;Traits&lt;/strong&gt; which we will discuss later.&lt;/p&gt;

&lt;p&gt;Vectors are the closest thing Rust has to Python's List's. They are relatively straight forward to create and use. Next, we will revisit another Rust Collection, The &lt;em&gt;String&lt;/em&gt;! See you then 👋 &lt;/p&gt;

</description>
      <category>learning</category>
      <category>rust</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Learning Rust 🦀: 15 - How you can organize your Rust code with "Modules"</title>
      <dc:creator>Fady GA 😎</dc:creator>
      <pubDate>Sun, 01 Oct 2023 13:59:08 +0000</pubDate>
      <link>https://dev.to/fadygrab/learning-rust-15-how-you-can-organize-you-rust-code-with-modules-2c28</link>
      <guid>https://dev.to/fadygrab/learning-rust-15-how-you-can-organize-you-rust-code-with-modules-2c28</guid>
      <description>&lt;p&gt;In this article we will see how the Rust's Module system works and how we can group related code together to enhance our project's maintainability. Let's jump in!&lt;/p&gt;

&lt;blockquote&gt;
&lt;h4&gt;
  
  
  ⚠️ Remember!
&lt;/h4&gt;

&lt;p&gt;You can find all the code snippets for this series in its &lt;a href="https://github.com/FadyGrAb/rust-learning-series"&gt;accompanying repo&lt;/a&gt;&lt;br&gt;&lt;br&gt;
If you don't want to install Rust locally, you can play with all the code of this series in the official &lt;a href="https://play.rust-lang.org/?version=stable&amp;amp;mode=debug&amp;amp;edition=2021"&gt;Rust Playground&lt;/a&gt; that can be found on its official page. That said, the playground may not be suitable for this topic.&lt;/p&gt;

&lt;p&gt;⚠️⚠️ The articles in this series are loosely following the contents of "The Rust Programming Language, 2nd Edition" by Steve Klabnik and Carol Nichols in a way that reflects my understanding from a Python developer's perspective.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;⭐ I try to publish a new article every week (maybe more if the Rust gods 🙌 are generous 😁) so stay tuned 😉. I'll be posting "new articles updates" on my &lt;a href="https://www.linkedin.com/in/fady-abdelmassih"&gt;LinkedIn&lt;/a&gt; and &lt;a href="https://twitter.com/fadyGrAb"&gt;Twitter&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Table of Contents:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Defining Modules&lt;/li&gt;
&lt;li&gt;Using Paths&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;pub&lt;/code&gt; keyword&lt;/li&gt;
&lt;li&gt;Structs and Enums inside a module&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;super&lt;/code&gt; and &lt;code&gt;self&lt;/code&gt; keywords&lt;/li&gt;
&lt;li&gt;Scope management with the &lt;code&gt;use&lt;/code&gt; keyword&lt;/li&gt;
&lt;li&gt;Using external packages&lt;/li&gt;
&lt;li&gt;Nested paths and globing&lt;/li&gt;
&lt;li&gt;Separate your modules in different files&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Defining Modules:
&lt;/h3&gt;

&lt;p&gt;What is a module in Rust? A module in Rust is just a grouping of related code together. We can define all of our modules in a single file or separate them into different files, which proves very handy if our project code base is large.  &lt;/p&gt;

&lt;p&gt;But before I show you how to define a module in Rust, I just want to demonstrate how a Rust project is structured using &lt;code&gt;cargo&lt;/code&gt;. When we type &lt;code&gt;cargo new &amp;lt;package name&amp;gt;&lt;/code&gt; into our terminal, cargo makes a "package" structure. A package contains "crates" which are in the &lt;code&gt;src&lt;/code&gt; directory. Crates can be either a library crate or a binary crate. The difference between the two is that a library crate doesnt contain a &lt;code&gt;main&lt;/code&gt; function and it just provides some functionality to the binary crate(s) in the same package or when used as an external crate.&lt;br&gt;&lt;br&gt;
A package can contain only one library crate and one or more binary crates.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;So far, all the packages we have created contain only one binary crate.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In the &lt;code&gt;src&lt;/code&gt; directory, the binary crate root is the &lt;code&gt;main.rs&lt;/code&gt; and the library crate is the &lt;code&gt;lib.rs&lt;/code&gt;. The root crate will have the same name as the package's. If our package contains binary crates other than the one in &lt;code&gt;main.rs&lt;/code&gt;, we put them in &lt;code&gt;src/bin/&lt;/code&gt; directory and they will be compiled into executables alongside our default binary crate.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If we typed &lt;code&gt;cargo new building&lt;/code&gt;, it will create a "building" package and the root binary crate (written in &lt;code&gt;scr/main.rs&lt;/code&gt;) will be named "building" too. If we wrote a library crate (in &lt;code&gt;src/lib.rs&lt;/code&gt;), it will also have the same name - building - as the package.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now that we've got this introduction out of our way, let's see how we can define a module:&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;mod&lt;/span&gt; &lt;span class="n"&gt;my_module&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// module contents&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's it! 🙂  &lt;/p&gt;

&lt;p&gt;To better demonstrate Rust's module system, let's assume that we are building a digital "hotel" in Rust and we will use the modules system to organize similar code together.&lt;br&gt;&lt;br&gt;
Type &lt;code&gt;cargo new hotel&lt;/code&gt; and create a &lt;code&gt;lib.rs&lt;/code&gt; in the &lt;code&gt;src&lt;/code&gt; directory.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;We can type &lt;code&gt;cargo new hotel --lib&lt;/code&gt; and this will create a package &lt;code&gt;hotel&lt;/code&gt; with only one library crate.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Next, put the following module in our recently created &lt;code&gt;lib.rs&lt;/code&gt; file.&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;mod&lt;/span&gt; &lt;span class="n"&gt;reception&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;mod&lt;/span&gt; &lt;span class="n"&gt;booking&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;book_room&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;"Booking room!"&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;cancel_booking&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;mod&lt;/span&gt; &lt;span class="n"&gt;guests_management&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;guest_checkin&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;"Checking in!"&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;guest_checkout&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;receive_guest_request&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;mod&lt;/span&gt; &lt;span class="n"&gt;facilities&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;mod&lt;/span&gt; &lt;span class="n"&gt;house_keeping&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;clean_room&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;deliver_meal&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;mod&lt;/span&gt; &lt;span class="n"&gt;maintenance&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;pool_maintenance&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;electrical_maintenance&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;building_maintenance&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;mod&lt;/span&gt; &lt;span class="n"&gt;restaurants&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;prepare_tables&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;"Preparing table"&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;make_meal&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;"Making meal"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A module in Rust can contain other items such as functions, structs, enums, constants, traits, and other modules. Here we have two main modules, &lt;code&gt;reception&lt;/code&gt; and &lt;code&gt;facilities&lt;/code&gt; both containing "child" modules that contain function.&lt;/p&gt;

&lt;h3&gt;
  
  
  Using Paths:
&lt;/h3&gt;

&lt;p&gt;To access an item inside a module we use a "file system-like" path which can be either absolute or relative. To demonstrate that, write the following function in &lt;code&gt;src/lib.rs&lt;/code&gt; outside of any module.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;go_on_vacation&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;crate&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;reception&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;booking&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;book_room&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;         &lt;span class="c1"&gt;// Absolute path&lt;/span&gt;
    &lt;span class="nn"&gt;reception&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;guests_management&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;guest_checkin&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;  &lt;span class="c1"&gt;// Relative path&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This function calls two other function from the &lt;code&gt;reception&lt;/code&gt; module.  It will be a lot easier to understand the module paths if we imagined our &lt;code&gt;lib.rs&lt;/code&gt; as the following file system:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;crate
    ├── reception
    |       ├── booking
    |       |       ├── book_room
    |       |       └── cancel_booking
    |       └── guests_management
    |               ├── guest_checkin
    |               └── guest_checkout
    └── facilities
            ├── house_keeping
            |       ├── clean_room
            |       └── deliver_meal
            ├── maintenance
            |       ├── pool_maintenance
            |       ├── electrical_maintenance
            |       └── building_maintenance
            └── restaurants
                    ├── prepare_tables
                    └── make_meal
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Where the &lt;code&gt;crate&lt;/code&gt; keyword represents the root crate or the "/" (root) in a Linux file system.&lt;br&gt;&lt;br&gt;
In the first call, we used an absolute path where we started with the root crate (&lt;code&gt;crate&lt;/code&gt; keyword) then all the way to the &lt;code&gt;book_room&lt;/code&gt; function. In the second call, we use a relative path as both the &lt;code&gt;go_on_vacation&lt;/code&gt; function and the &lt;code&gt;reception&lt;/code&gt; module are on the same level (children of the root crate), we can refer to the &lt;code&gt;reception&lt;/code&gt; module from the &lt;code&gt;go_on_vacation&lt;/code&gt; function like we did.&lt;br&gt;&lt;br&gt;
Unfortunately, if we tried to run this code, it wouldn't compile! The compilier will complain about &lt;code&gt;booking&lt;/code&gt; and &lt;code&gt;guests_management&lt;/code&gt; modules being private!&lt;/p&gt;
&lt;h3&gt;
  
  
  The &lt;code&gt;pub&lt;/code&gt; keyword:
&lt;/h3&gt;

&lt;p&gt;In order to solve the problem we saw in the previous section, we can use the &lt;code&gt;pub&lt;/code&gt; (for public) keyword. Using &lt;code&gt;pub&lt;/code&gt; before &lt;code&gt;mod&lt;/code&gt; will make the module public. Armed with our new found knowledge, let's add the &lt;code&gt;pub&lt;/code&gt; keyword as follows:&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;mod&lt;/span&gt; &lt;span class="n"&gt;reception&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;mod&lt;/span&gt; &lt;span class="n"&gt;booking&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// snip&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;mod&lt;/span&gt; &lt;span class="n"&gt;guests_management&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// snip&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;If we run the code now, the compiler will complain, again!&lt;br&gt;&lt;br&gt;
This time, it will complain about &lt;code&gt;book_rook&lt;/code&gt; and &lt;code&gt;guest_checkin&lt;/code&gt; functions being private. This highlights an important rule about the &lt;code&gt;pub&lt;/code&gt; keyword, &lt;em&gt;making a module public won't make its children public!&lt;/em&gt; and you will have to explicitly choose which components for public access as they are private by default. Let's do the same for those functions as we did with the modules:&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;mod&lt;/span&gt; &lt;span class="n"&gt;reception&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;mod&lt;/span&gt; &lt;span class="n"&gt;booking&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;book_room&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;"Booking room!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="c1"&gt;// snip&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;mod&lt;/span&gt; &lt;span class="n"&gt;guests_management&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;guest_checkin&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;"Checking in!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="c1"&gt;// snip&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, it will be compiled! But wait! we didn't put &lt;code&gt;pub&lt;/code&gt; before &lt;code&gt;reception&lt;/code&gt; module, how we could access it?&lt;br&gt;&lt;br&gt;
As the &lt;code&gt;reception&lt;/code&gt; module and the &lt;code&gt;go_on_vacation&lt;/code&gt; function are both on the same level in the "modules system" (the root crate), they can see each other.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Items that are on the same level are called "siblings" while items that are one level down are called "children" and the items that are one level up are called "parents". &lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3&gt;
  
  
  Structs and Enums inside a module:
&lt;/h3&gt;

&lt;p&gt;As I've mentioned before, modules can contain &lt;em&gt;Structs&lt;/em&gt; and &lt;em&gt;Enums&lt;/em&gt; and both of them (like functions) can accept the &lt;code&gt;pub&lt;/code&gt; keyword but with a subtle difference! First, let's add a &lt;code&gt;Room&lt;/code&gt; struct to the &lt;code&gt;reception&lt;/code&gt; module.&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;mod&lt;/span&gt; &lt;span class="n"&gt;reception&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// snip&lt;/span&gt;
    &lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;mod&lt;/span&gt; &lt;span class="n"&gt;guests_management&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// snip&lt;/span&gt;
        &lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;get_room_number&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;i8&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="mi"&gt;10&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nd"&gt;#[derive(Debug)]&lt;/span&gt;
    &lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;Room&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="n"&gt;view&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="n"&gt;beds_count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;impl&lt;/span&gt; &lt;span class="n"&gt;Room&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;view&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;beds&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i8&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Room&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;Room&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;view&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="n"&gt;view&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
                &lt;span class="n"&gt;beds_count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;beds&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nn"&gt;guests_management&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;get_room_number&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And add the following in &lt;code&gt;src/main.rs&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;hotel&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;guests&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="nn"&gt;hotel&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;go_on_vacation&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;Okay! There is a lot going on here, so let's break it down! 😉&lt;br&gt;
Let's start first with the Struct definition. One thing to notice is that both the struct and some of its elements have the &lt;code&gt;pub&lt;/code&gt; keyword. For structs if you are going to make the public, in addition to the &lt;code&gt;pub&lt;/code&gt; before the struct definition, you have to explicitly set which elements are public as the default behavior for them is to be private. Here we have created a &lt;code&gt;Room&lt;/code&gt; struct that has two public elements, &lt;code&gt;view&lt;/code&gt; and &lt;code&gt;beds_count&lt;/code&gt;, and one private element, &lt;code&gt;number&lt;/code&gt;. But this presents a problem! As the &lt;code&gt;number&lt;/code&gt; property is private, how can it be set??&lt;br&gt;&lt;br&gt;
We solve that by creating an implementation for this struct including a &lt;code&gt;new&lt;/code&gt; function that acts as a factory function for new Rooms. This function sets the &lt;code&gt;view&lt;/code&gt; and &lt;code&gt;beds_number&lt;/code&gt; according to the guest's request and sets the &lt;code&gt;number&lt;/code&gt; using a public function, &lt;code&gt;get_room_number&lt;/code&gt;, inside the sibling &lt;code&gt;guests_management&lt;/code&gt; module.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The implementation of the &lt;code&gt;get_room_number&lt;/code&gt; function is really silly! It just returns the number &lt;code&gt;10&lt;/code&gt; for every request! A better function would check the rooms in a database for example and return a vacant one. But this one here will do the trick.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Add the following code to the &lt;code&gt;go_on_vacation&lt;/code&gt; function and run it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;go_on_vacation&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// snip&lt;/span&gt;

    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;my_room&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;reception&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;Room&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="s"&gt;"Sea view"&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="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="s"&gt;"My room's vew is {} and my room number is {}. My room: {:?}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;my_room&lt;/span&gt;&lt;span class="py"&gt;.view&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;my_room&lt;/span&gt;&lt;span class="py"&gt;.number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;my_room&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;As expected, it will produce a compilation error as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;error[E0616]: field `number` of struct `Room` is private
   --&amp;gt; src/lib.rs:100:31
    |
100 |         my_room.view, my_room.number, my_room
    |                               ^^^^^^ private field
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We are not allowed to access "private" fields of a struct even if the struct itself is "public". If we ommited the erroneous code and replaced it with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;    &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"My room's vew is {}. My room: {:?}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;my_room&lt;/span&gt;&lt;span class="py"&gt;.view&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;my_room&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It will be compiled and we will be able to see that the &lt;code&gt;number&lt;/code&gt; field of the &lt;code&gt;Room&lt;/code&gt; struct is set to &lt;code&gt;10&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;My room's vew is Sea view. My room: Room { view: "Sea view", beds_count: 2, number: 10 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For Enums, however, if we set it to public, all of its variants will be public too without the need to explicitly set their access to public which makes sense as all the variants of an enum are checked exhaustively so we can't choose a subset to make them public. Add the following enumb to the &lt;code&gt;restaurant&lt;/code&gt; module inside the &lt;code&gt;facilities&lt;/code&gt; module:&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;mod&lt;/span&gt; &lt;span class="n"&gt;facilities&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// snip&lt;/span&gt;
    &lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;mod&lt;/span&gt; &lt;span class="n"&gt;restaurants&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="c1"&gt;// snip&lt;/span&gt;
        &lt;span class="nd"&gt;#[derive(Debug)]&lt;/span&gt;
        &lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;Meal&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;Breakfast&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;Brunch&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;Lunch&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;Diner&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To test it, let's add the following new module to &lt;code&gt;src/lib.rs&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;mod&lt;/span&gt; &lt;span class="n"&gt;guests&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;book_a_room&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;go_to_beach&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;go_to_pool&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
    &lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;eat_meal&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;my_meal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;crate&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;facilities&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;restaurants&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;Meal&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Diner&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;"Eating {my_meal:?}"&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;end_vacation&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;"Bye bye!"&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;And add the following to the main function in &lt;code&gt;src/main.rs&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;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="c1"&gt;// snip&lt;/span&gt;
    &lt;span class="nn"&gt;hotel&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;guests&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;eat_meal&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;If we try to run it, we will get the following result as expected&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Eating Diner
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  The &lt;code&gt;super&lt;/code&gt; and &lt;code&gt;self&lt;/code&gt; keywords:
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;super&lt;/code&gt; and &lt;code&gt;self&lt;/code&gt; are very useful keywords when used in Rust's module system paths. &lt;code&gt;super&lt;/code&gt; refers to the direct parent of the module that is calling it and &lt;code&gt;self&lt;/code&gt; refers to the same module that is calling it. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The use of &lt;code&gt;self&lt;/code&gt; will become evidence in the "nested paths" section.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To demonstrate their use, add this code to the &lt;code&gt;book_a_room&lt;/code&gt; function in the &lt;code&gt;guest&lt;/code&gt; module:&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;book_a_room&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;reception&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;booking&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;book_room&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;go_to_beach&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;Here, the &lt;code&gt;super&lt;/code&gt; expression is referring to the root crate as its the &lt;code&gt;guest&lt;/code&gt; module's direct parent. Then it accesses the &lt;code&gt;reception&lt;/code&gt; which is accessible from the root level all the way down to the &lt;code&gt;book_room&lt;/code&gt; function.&lt;br&gt;&lt;br&gt;
In the expression with the &lt;code&gt;self&lt;/code&gt;, it calls the &lt;code&gt;go_to_beach&lt;/code&gt; function from the same &lt;code&gt;guest&lt;/code&gt; module.&lt;/p&gt;
&lt;h3&gt;
  
  
  Scope management with the &lt;code&gt;use&lt;/code&gt; keyword:
&lt;/h3&gt;

&lt;p&gt;As we saw, we can use the module system paths to call functions and use elements inside modules provided that they are public. But sometimes the path can be very long! One way to overcome this is by using the &lt;code&gt;use&lt;/code&gt; keyword as it brings to scope the element at its end and we can use it directly afterwards. Let's try it by making the following changes to the &lt;code&gt;guests&lt;/code&gt; module:&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;mod&lt;/span&gt; &lt;span class="n"&gt;guests&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;reception&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;guests_management&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;guest_checkout&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="k"&gt;crate&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;facilities&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;restaurants&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

   &lt;span class="c1"&gt;// snip&lt;/span&gt;

    &lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;eat_meal&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nn"&gt;restaurants&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;prepare_tables&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="nn"&gt;restaurants&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;make_meal&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="c1"&gt;// snip&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;end_vacation&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;guest_checkout&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;"Bye bye!"&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;Here we are bringing both the &lt;code&gt;restaurants&lt;/code&gt; module and the &lt;code&gt;guest_checkout&lt;/code&gt; function to scope using the &lt;code&gt;use&lt;/code&gt; keyword. Notice that in the &lt;code&gt;eat_meal&lt;/code&gt; function, we are writing &lt;code&gt;restaurants&lt;/code&gt; directly without its parents as we already brought it into scope. And in the &lt;code&gt;end_vacation&lt;/code&gt; function, we are also using the &lt;code&gt;guest_checkout&lt;/code&gt; directly as -similar to the &lt;code&gt;restaurants&lt;/code&gt; module- we brought it into scope. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;There is a convention to use the path of the parent module if we are bringing in functions. That way, our code will be more readable as we know the source module of the function used. On the other hand, if we are bringing in structs or enums, we should use the full path.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Using external packages:
&lt;/h3&gt;

&lt;p&gt;Up until this moment, we've used our own crates. To use external crates, we use the &lt;code&gt;cargo.toml&lt;/code&gt; file in our project's root like we did &lt;a href="https://dev.to/fadygrab/learning-rust-05-your-first-rust-game-guess-the-number-25dn#using-external-crates"&gt;here&lt;/a&gt; in our guessing game. We have to specify the crate name and version under the &lt;code&gt;dependencies&lt;/code&gt; in the toml file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rand = "0.8.5"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then we can use the &lt;code&gt;use&lt;/code&gt; keyword of the path to use the functionality provided by this crate.&lt;/p&gt;

&lt;h3&gt;
  
  
  Nested paths and globing:
&lt;/h3&gt;

&lt;p&gt;Nested paths is a way to reduce the clutter when importing several elements from the same module. Consider this example when we try to import the &lt;code&gt;make_meal&lt;/code&gt; and &lt;code&gt;prepare_tables&lt;/code&gt; functions from the &lt;code&gt;restaurants&lt;/code&gt; module:&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;hotel&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;facilities&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;restaurants&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;make_meal&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;hotel&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;facilities&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;restaurants&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;prepare_tables&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The parents for both functions are the same, so we can use nested paths to reduce the preceding into this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;hotel&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;facilities&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;restaurants&lt;/span&gt;&lt;span class="p"&gt;::{&lt;/span&gt;&lt;span class="n"&gt;make_meal&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;prepare_tables&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And what about the following situation where we want to import the module and one of its elements?:&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;hotel&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;facilities&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;restaurants&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;hotel&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;facilities&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;restaurants&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;prepare_tables&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We simply use the &lt;code&gt;self&lt;/code&gt; keyword which we talked about earlier:&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;hotel&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;facilities&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;restaurants&lt;/span&gt;&lt;span class="p"&gt;::{&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;prepare_tables&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And we can use "globing" using "*" to bring "everything" (that is public) into scope from a module:&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;hotel&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;facilities&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;restaurants&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will bring the &lt;code&gt;prepare_tables&lt;/code&gt; and &lt;code&gt;make_meal&lt;/code&gt; functions alongside with the &lt;code&gt;Meal&lt;/code&gt; enum into scope.&lt;/p&gt;

&lt;h3&gt;
  
  
  Separate your modules in different files:
&lt;/h3&gt;

&lt;p&gt;Up to this moment, our &lt;code&gt;src/lib.rs&lt;/code&gt; contains all of our modules which is acceptable with a small code base. But what happens when our project grows bigger? One file containing all of our modules sounds like a bad idea and will decrease the code maintainability!&lt;br&gt;&lt;br&gt;
Luckily, we can split our modules into separate files. The rules are simple:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Write the module contents in a separate file inside &lt;code&gt;src&lt;/code&gt; directory.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;mod &amp;lt;module name&amp;gt;&lt;/code&gt; in its parent module (or binary crate) where  is the same as its file name.&lt;/li&gt;
&lt;li&gt;If the module contains other submodules, create a directory at the same module file level and follow point 1 and 2.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's demonstrate that with an example. Create a new package typing &lt;code&gt;cargo new hotel_different_files&lt;/code&gt;. Next, create &lt;code&gt;src/lib.rs&lt;/code&gt; and add &lt;code&gt;pub mod reception;&lt;/code&gt; to it. After that, create a new file &lt;code&gt;scr/reception.rs&lt;/code&gt; and the following to it:&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;mod&lt;/span&gt; &lt;span class="n"&gt;booking&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;mod&lt;/span&gt; &lt;span class="n"&gt;guests_management&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nd"&gt;#[derive(Debug)]&lt;/span&gt;
&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;Room&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;view&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;beds_count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;impl&lt;/span&gt; &lt;span class="n"&gt;Room&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;get_room&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;view&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;beds&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i8&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Room&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Room&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;view&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="n"&gt;view&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
            &lt;span class="n"&gt;beds_count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;beds&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nn"&gt;guests_management&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;get_room_number&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we have referred to the &lt;code&gt;booking&lt;/code&gt; and &lt;code&gt;guests_management&lt;/code&gt; submodules and kept the Room struct and its implementation the same as before.&lt;br&gt;&lt;br&gt;
As the &lt;code&gt;reception&lt;/code&gt; module contains submodules, we will create the &lt;code&gt;src/reception/&lt;/code&gt; directory which contains both submodules as &lt;code&gt;src/reception/booking.rs&lt;/code&gt; and &lt;code&gt;src/reception/guests_management.rs&lt;/code&gt; which will contain the following respectively:&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="c1"&gt;// booking.rs&lt;/span&gt;
&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;book_room&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;"Booking room!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;cancel_booking&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;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="c1"&gt;// guests_management.rs&lt;/span&gt;
&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;guest_checkin&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;"Checking in!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;guest_checkout&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;receive_guest_request&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;get_room_number&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;i8&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="mi"&gt;10&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And finally, to test everything out, add the following to &lt;code&gt;src/main.rs&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;hotel_different_files&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;reception&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;my_room&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;reception&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;Room&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;get_room&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"pool view"&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="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"My room is {my_room:?}"&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;And it will print the &lt;code&gt;Room&lt;/code&gt; struct exactly as our "one-file" implementation!&lt;/p&gt;

&lt;p&gt;Few! 😮‍💨, this was a long but interesting one! Buy now, we should have a basic understanding of Rust's Module system. In the next article, We will start exploring Rust's common collections! See you then 👋&lt;/p&gt;

</description>
      <category>learning</category>
      <category>rust</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Learning Rust 🦀: 14 - Option Enum: An Enum and Pattern matching use case</title>
      <dc:creator>Fady GA 😎</dc:creator>
      <pubDate>Fri, 15 Sep 2023 10:46:23 +0000</pubDate>
      <link>https://dev.to/fadygrab/learning-rust-14-option-enum-an-enum-and-pattern-matching-use-case-1dgf</link>
      <guid>https://dev.to/fadygrab/learning-rust-14-option-enum-an-enum-and-pattern-matching-use-case-1dgf</guid>
      <description>&lt;p&gt;We will finish off our Enum and pattern matching discussion that was started &lt;a href="https://dev.to/fadygrab/learning-rust-12-basic-enum-2mn3"&gt;here&lt;/a&gt; by a case study on the &lt;strong&gt;Option&lt;/strong&gt; standard enum. Let's begin.&lt;/p&gt;

&lt;blockquote&gt;
&lt;h4&gt;
  
  
  ⚠️ Remember!
&lt;/h4&gt;

&lt;p&gt;You can find all the code snippets for this series in its &lt;a href="https://github.com/FadyGrAb/rust-learning-series"&gt;accompanying repo&lt;/a&gt;&lt;br&gt;&lt;br&gt;
If you don't want to install Rust locally, you can play with all the code of this series in the official &lt;a href="https://play.rust-lang.org/?version=stable&amp;amp;mode=debug&amp;amp;edition=2021"&gt;Rust Playground&lt;/a&gt; that can be found on its official page.&lt;/p&gt;

&lt;p&gt;⚠️⚠️ The articles in this series are loosely following the contents of "The Rust Programming Language, 2nd Edition" by Steve Klabnik and Carol Nichols in a way that reflects my understanding from a Python developer's perspective.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;⭐ I try to publish a new article every week (maybe more if the Rust gods 🙌 are generous 😁) so stay tuned 😉. I'll be posting "new articles updates" on my &lt;a href="https://www.linkedin.com/in/fady-abdelmassih"&gt;LinkedIn&lt;/a&gt; and &lt;a href="https://twitter.com/fadyGrAb"&gt;Twitter&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Table of Contents:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Problem of &lt;strong&gt;Null&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;The Option&amp;lt;T&amp;gt; Enum&lt;/li&gt;
&lt;li&gt;Using the Option Enum&lt;/li&gt;
&lt;li&gt;If Let: alternate way to pattern matching&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Problem of &lt;strong&gt;Null&lt;/strong&gt;:
&lt;/h3&gt;

&lt;p&gt;How many times have you faced bugs in your code due to an "unexpected" null value? I'll answer that, plenty 😁&lt;br&gt;
The fix is easy, in python for example you can write something like that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"A none value!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;None&lt;/em&gt; and &lt;em&gt;Null&lt;/em&gt; are the same in Python&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;But you have to remember to do that for every value (or data structure) that may become - for some reason 🤷‍♂️ - absent or invalid (i.e. Null) which might not be very scalable in a large code base.&lt;br&gt;&lt;br&gt;
Rust fixes that! by ... removing the Null 😁! Yes, you have read it write, there is no intrinsic Null type in Rust.&lt;br&gt;&lt;br&gt;
But ...&lt;br&gt;&lt;br&gt;
There is a standard Enum called "&lt;strong&gt;Option&lt;/strong&gt;" (Option) that can be used in this use case.&lt;/p&gt;
&lt;h3&gt;
  
  
  The Option&amp;lt;T&amp;gt; Enum:
&lt;/h3&gt;

&lt;p&gt;As I've mentioned, Rust has a standard Enum called "Option" which is loaded in the "prelude" (you don't have to load it with the &lt;code&gt;use&lt;/code&gt; keyword) to deal with the &lt;strong&gt;Null&lt;/strong&gt; situation that we have here. It has two variants:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Some(T)&lt;/li&gt;
&lt;li&gt;None&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;And it is defined like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;enum&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="n"&gt;T&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;Some&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="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;The first thing to notice is that weird "&amp;lt;T&amp;gt;" notation. This is a generic type notation (T for Type) and we will encounter it later on. What's important now is that you imagine "Option" as a "&lt;strong&gt;wrapper&lt;/strong&gt;" for &lt;em&gt;Any Type&lt;/em&gt; that you use in your program that might be either "Some" value or "None" (Null) and as we have learned in the &lt;a href="https://dev.to/fadygrab/learning-rust-13-pattern-matching-basics-40pj"&gt;last article&lt;/a&gt;, that pattern matching for Enums must be "Exhaustive", when using the Option Enum the compiler will force the Null value handling sparing you from remembering to do that yourself.  &lt;/p&gt;

&lt;h3&gt;
  
  
  Using the Option Enum:
&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;Option&lt;/strong&gt; Enum is load into the Rust program prelude by default so as its variants &lt;strong&gt;Some(T)&lt;/strong&gt; and &lt;strong&gt;None&lt;/strong&gt; and you don't have to use namespacing to use them.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Something like &lt;code&gt;Option&amp;lt;i32&amp;gt;::Some(5)&lt;/code&gt; isn't necessary. Just use &lt;code&gt;Some(5)&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&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="c1"&gt;// Using the Option Enum without importing it.&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;some_number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;Some&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;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;Some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"My Name"&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;none&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Option&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="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;Here, we are directly using &lt;code&gt;Some&lt;/code&gt; and &lt;code&gt;None&lt;/code&gt;. Note that for &lt;code&gt;Some&lt;/code&gt;, it can take any Type and the compiler can infer that type. But for &lt;code&gt;None&lt;/code&gt;, we have to set its type.&lt;br&gt;&lt;br&gt;
The &lt;strong&gt;Option/&lt;/strong&gt; Enum has a set of useful methods like &lt;code&gt;is_some()&lt;/code&gt; or &lt;code&gt;is_none()&lt;/code&gt;. See the docs for a complete listing:&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="c1"&gt;// Using the Option Enum without importing it.&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;some_number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;Some&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;none&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Option&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;None&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// Some Option Enum methods&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;some_number&lt;/span&gt;&lt;span class="nf"&gt;.is_some&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="kc"&gt;true&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;none&lt;/span&gt;&lt;span class="nf"&gt;.is_none&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// Will cause assertion error&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One important thing to know, is that &lt;code&gt;Some(5)&lt;/code&gt; isn't &lt;code&gt;5&lt;/code&gt;! Let's try it out:&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="c1"&gt;// Using the Option Enum without importing it.&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;some_number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;Some&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;number&lt;/span&gt; &lt;span class="o"&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;addition&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;some_number&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;number&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;If we run this code, we will get the following compilation error:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;error[E0369]: cannot add `{integer}` to `Option&amp;lt;{integer}&amp;gt;`
  --&amp;gt; src/main.rs:13:32
   |
13 |     let addition = some_number + number;
   |                    ----------- ^ ------ {integer}
   |                    |
   |                    Option&amp;lt;{integer}&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Basically, what this error message is telling us is that Rust don't know how to add a &lt;code&gt;5&lt;/code&gt; that is wrapped in Option Enum with a normal &lt;code&gt;i32&lt;/code&gt; 5 as they are now two different types.&lt;br&gt;&lt;br&gt;
So how could we do that??🤔&lt;br&gt;&lt;br&gt;
This is when &lt;strong&gt;Pattern Matching&lt;/strong&gt; comes in and the purpose of the &lt;strong&gt;Option Enum&lt;/strong&gt; becomes clear.&lt;br&gt;&lt;br&gt;
We will demo the use of bound data by using a function that doubles what's inside &lt;code&gt;Some&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;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="c1"&gt;// Using the data bound to Some.&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;five&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;Some&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="k"&gt;mut&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;double&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;five&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;"Double of 5 is: {result:?}"&lt;/span&gt;&lt;span class="p"&gt;);&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;double&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;none&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;"Double of None is: {result:?}"&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;double&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="nb"&gt;Option&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;Option&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;match&lt;/span&gt; &lt;span class="n"&gt;num&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="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;Some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="nb"&gt;None&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;None&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The function is called &lt;code&gt;double&lt;/code&gt; and it takes an &lt;strong&gt;Option&lt;/strong&gt; Enum with &lt;code&gt;i32&lt;/code&gt; data type and outputs the same type. Inside the function, there is a &lt;code&gt;match&lt;/code&gt; expression that has to matching arms for the two &lt;strong&gt;Option&lt;/strong&gt; variants, &lt;strong&gt;Some&lt;/strong&gt; and &lt;strong&gt;None&lt;/strong&gt;. And hence pattern matching must be exhaustive, the compiler checks if all variants are mentioned and therefore enforces a "Null" handling arm.&lt;br&gt;&lt;br&gt;
If we run this code we will get:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Double of 5 is: Some(10)
Double of None is: None
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, when should we use the &lt;strong&gt;Option&lt;/strong&gt; Enum you say?&lt;br&gt;&lt;br&gt;
We should use it whenever our values could be a Null because as long as the value isn't wrapped in it, Rust assumes that it will be always valid through the program execution.&lt;br&gt;&lt;br&gt;
The &lt;strong&gt;Option&lt;/strong&gt; Enum is yet another way where Rust enforces its safety guard rails and we should use that for our advantage.&lt;br&gt;
&lt;a href="https://i.giphy.com/media/3oz8xUJsD8AsihJrtC/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/3oz8xUJsD8AsihJrtC/giphy.gif" alt="safety first" width="320" height="180"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  If Let: alternate way to pattern matching:
&lt;/h3&gt;

&lt;p&gt;One more thing before we leave pattern matching (for now 😉), is the &lt;code&gt;if let&lt;/code&gt; expression. Image that you are receiving a sensor readings that may be null due to network problems, corrupt data, or for whatever reason and you are interested in printing the value when it's &lt;code&gt;5&lt;/code&gt; only. One way to do it is as follows:&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="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;reading&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Option&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;u8&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;Some&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;match&lt;/span&gt; &lt;span class="n"&gt;reading&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="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&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;"Reading of 5 is received [match]"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="n"&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="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;Remember the "_" placeholder from &lt;a href="https://dev.to/fadygrab/learning-rust-13-pattern-matching-basics-40pj/#using-the-placeholder"&gt;here&lt;/a&gt;? Here, we are only interested when we receive &lt;code&gt;Some(5)&lt;/code&gt;. Any other value including Null, will produce the Unit tuple () (basically, do nothing). But this is a bit verbose.&lt;br&gt;&lt;br&gt;
Another way to write the same code is by using &lt;code&gt;if let&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;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;let&lt;/span&gt; &lt;span class="n"&gt;reading&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Option&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;u8&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;Some&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;if&lt;/span&gt; &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nf"&gt;Some&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="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;reading&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;"Reading of 5 is received [if let]"&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;If we run this code, you will get the same result as before but less verbose. The &lt;code&gt;if let&lt;/code&gt; syntax is a bit confusing but once you use it often, it will make sense:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if let &amp;lt;pattern&amp;gt; = &amp;lt;value&amp;gt; {
   &amp;lt;expression(s) to be executed if matched&amp;gt;
} else {
   &amp;lt;expression(s) to be executed if not matched&amp;gt;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, when to use &lt;code&gt;if let&lt;/code&gt;? There is no advantage of using &lt;code&gt;if let&lt;/code&gt; over &lt;code&gt;match&lt;/code&gt; for "one" pattern matching except it's less verbose. So, using one over the other depends on what you are trying to do...&lt;br&gt;&lt;br&gt;
If you want to take advantage of the exhaustiveness of the &lt;code&gt;match&lt;/code&gt;, use it. Otherwise, if you are only interested in one match and you can safely ignore all the others, use &lt;code&gt;if let&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;As show, &lt;code&gt;if let&lt;/code&gt; has and &lt;code&gt;else&lt;/code&gt; clause that that be used as a "catch-all" for all other not matched patterns&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Next, we will explore more about how we can use Packages, Crates and Modules in Rust. I may break it into several articles too. See you then 👋.&lt;/p&gt;

</description>
      <category>learning</category>
      <category>rust</category>
      <category>programming</category>
      <category>development</category>
    </item>
    <item>
      <title>Learning Rust 🦀: 13 - Pattern matching basics</title>
      <dc:creator>Fady GA 😎</dc:creator>
      <pubDate>Fri, 08 Sep 2023 14:20:16 +0000</pubDate>
      <link>https://dev.to/fadygrab/learning-rust-13-pattern-matching-basics-40pj</link>
      <guid>https://dev.to/fadygrab/learning-rust-13-pattern-matching-basics-40pj</guid>
      <description>&lt;p&gt;We will continue our Enum mini-series. This time we will discuss the pattern matching basics using Enums. Let's begin.&lt;/p&gt;

&lt;blockquote&gt;
&lt;h4&gt;
  
  
  ⚠️ Remember!
&lt;/h4&gt;

&lt;p&gt;You can find all the code snippets for this series in its &lt;a href="https://github.com/FadyGrAb/rust-learning-series" rel="noopener noreferrer"&gt;accompanying repo&lt;/a&gt;&lt;br&gt;&lt;br&gt;
If you don't want to install Rust locally, you can play with all the code of this series in the official &lt;a href="https://play.rust-lang.org/?version=stable&amp;amp;mode=debug&amp;amp;edition=2021" rel="noopener noreferrer"&gt;Rust Playground&lt;/a&gt; that can be found on its official page.&lt;/p&gt;

&lt;p&gt;⚠️⚠️ The articles in this series are loosely following the contents of "The Rust Programming Language, 2nd Edition" by Steve Klabnik and Carol Nichols in a way that reflects my understanding from a Python developer's perspective.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;⭐ I try to publish a new article every week (maybe more if the Rust gods 🙌 are generous 😁) so stay tuned 😉. I'll be posting "new articles updates" on my &lt;a href="https://www.linkedin.com/in/fady-abdelmassih" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; and &lt;a href="https://twitter.com/fadyGrAb" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Table of Contents:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;strong&gt;match&lt;/strong&gt; control flow&lt;/li&gt;
&lt;li&gt;Matching with patterns bind to values&lt;/li&gt;
&lt;li&gt;Using the &lt;strong&gt;"_"&lt;/strong&gt; placeholder&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  The &lt;strong&gt;match&lt;/strong&gt; control flow:
&lt;/h3&gt;

&lt;p&gt;Another feature that I couldn't find in Python is the &lt;em&gt;Pattern Matching&lt;/em&gt;. It's that control flow construct that is based on ... you've guessed it, &lt;em&gt;patterns&lt;/em&gt; 😁! Let's inspect that with an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;die_roll&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;4&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;die_roll&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="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"one"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;die_roll&lt;/span&gt; &lt;span class="o"&gt;==&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;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"two"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;die_roll&lt;/span&gt; &lt;span class="o"&gt;==&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;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"three"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;die_roll&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;4&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;"four"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;die_roll&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;5&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;"five"&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="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"six"&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 example, we are throwing a die and print the number that we got in letters. Here, we are controlling the program flow based on the &lt;strong&gt;value&lt;/strong&gt; of &lt;code&gt;die_roll&lt;/code&gt; and the &lt;code&gt;if&lt;/code&gt; expression should evaluate to either &lt;code&gt;true&lt;/code&gt; or &lt;code&gt;false&lt;/code&gt;. This is as far as we can go with &lt;code&gt;if&lt;/code&gt;. We can re-write the previous example using &lt;code&gt;match&lt;/code&gt; as follows:&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="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;die_roll&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;4&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;die_roll&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="mi"&gt;1&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;"one"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="mi"&gt;2&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;"two"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="mi"&gt;3&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;"three"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="mi"&gt;4&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;"four"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="mi"&gt;5&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;"five"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="mi"&gt;6&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;"six"&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 code snippet will work exactly the same as the previous one but with less typing 😉. But &lt;code&gt;match&lt;/code&gt; doesn't only provide &lt;em&gt;less typing&lt;/em&gt; benefits over &lt;code&gt;if&lt;/code&gt;, it can match based on types either and not only values. Take a look at the following example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="nd"&gt;#[derive(Debug)]&lt;/span&gt;
&lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;Superhero&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Batman&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;Superman&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;WonderWoman&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;Flash&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;GreenLantern&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;CatWoman&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="c1"&gt;// Basic matching.&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;hero&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;Superhero&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Batman&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;is_batman&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;false&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;super_power&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="n"&gt;hero&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nn"&gt;Superhero&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Batman&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;is_batman&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="s"&gt;"Rich"&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="nn"&gt;Superhero&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Superman&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;"Flying, Bulletproof, Heat vision, Cold breath"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nn"&gt;Superhero&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;WonderWoman&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;"Flying, Strength, Divine weapons"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nn"&gt;Superhero&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;CatWoman&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;"Stealth, Agility"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nn"&gt;Superhero&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Flash&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;"Speed, Connection with the speed-force"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nn"&gt;Superhero&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;GreenLantern&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;"Flying, Instant light constructs, Lantern Corps"&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;"{hero:?}: {super_power} -- is batman {is_batman}"&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;If you run this code, you will get:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Batman: Rich -- is batman true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's break it down. As you can see, we've created our &lt;code&gt;Superhero&lt;/code&gt; Enum containing my favorite superheroes 😁.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note the &lt;code&gt;#[derive(Debug)]&lt;/code&gt;, remember &lt;a href="https://dev.to/fadygrab/learning-rust-11-debugging-custom-typesstructs-3dnp"&gt;this one&lt;/a&gt; 😉? &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Then, we have created &lt;code&gt;hero&lt;/code&gt; variable that holds the &lt;code&gt;Superhero&lt;/code&gt; variant &lt;em&gt;BATMAAAAN&lt;/em&gt; 🦇 and an &lt;code&gt;is_batman&lt;/code&gt; boolean flag that is initially set to &lt;code&gt;false&lt;/code&gt;. Then, we've created &lt;code&gt;super_power&lt;/code&gt; variable that gets its value form a &lt;code&gt;match&lt;/code&gt; expression depending on the passed &lt;code&gt;Superhero&lt;/code&gt; variant in &lt;code&gt;hero&lt;/code&gt;. And Finally, we print what we get.&lt;br&gt;&lt;br&gt;
Now let's inspect the &lt;code&gt;match&lt;/code&gt; block. It is constructed by using the &lt;code&gt;match&lt;/code&gt; keyword then the "Scrutinee" (as described by the docs) or in simpler terms, the variable that we want to match against. Following that in the curly brackets, we have the &lt;strong&gt;"match arm"&lt;/strong&gt; which is the value - or more generally the pattern - we are matching followed by the &lt;code&gt;=&amp;gt;&lt;/code&gt; operation then the &lt;strong&gt;"expression"&lt;/strong&gt; we want to execute (or return). Usually, you don't want to use curly bracket in the "expression" block if the expression is short but if your expression block contains more than one expression/statement then their use is mandatory.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Check out the "BATMAAAN" expression where we have changed the value of &lt;code&gt;is_batman&lt;/code&gt; to &lt;code&gt;true&lt;/code&gt; in addition to returning its superpower as &lt;code&gt;&amp;amp;str&lt;/code&gt; (string literal).  &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;There are two things to consider when using &lt;code&gt;match&lt;/code&gt; for pattern matching:&lt;br&gt;
    1. The match patterns must be exhaustive i.e. using all the variants of the checked variable.&lt;br&gt;
    2. The match arms must return the same type i.e. one match arm can't return &lt;code&gt;i32&lt;/code&gt; and another one returns &lt;code&gt;&amp;amp;str&lt;/code&gt;.&lt;br&gt;&lt;br&gt;
Notice that in this example we have listed &lt;strong&gt;all&lt;/strong&gt; the superheroes in our &lt;code&gt;Superhero&lt;/code&gt; Enum and all the match arms are returning string literals.&lt;br&gt;&lt;br&gt;
Think of the &lt;code&gt;match&lt;/code&gt; flow as a "Coins sorting machine" that executes the first match arm that matches the tested variable.  &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fp56gp81m5rz8xj7lz7oa.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fp56gp81m5rz8xj7lz7oa.gif" alt="Coin sorting machine"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Matching with patterns bind to values:
&lt;/h3&gt;

&lt;p&gt;In the &lt;a href="https://dev.to/fadygrab/learning-rust-12-basic-enum-2mn3"&gt;last article&lt;/a&gt;, we saw that we can bind data to Enums and using &lt;code&gt;match&lt;/code&gt; is a way to use this bound data. Have a look at this example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="nd"&gt;#[derive(Debug)]&lt;/span&gt;
&lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;SuperheroWithWeapon&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;Batman&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;FavoriteWeapon&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="n"&gt;Superman&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nf"&gt;WonderWoman&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;FavoriteWeapon&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="n"&gt;Flash&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nf"&gt;GreenLantern&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;FavoriteWeapon&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="n"&gt;CatWoman&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(Debug)]&lt;/span&gt;
&lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;FavoriteWeapon&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;LassoOfTruth&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;GreenLanternRing&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;Batarang&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="c1"&gt;// Matching with patterns that bind to values with a catch-all.&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;hero_with_weapon_1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;SuperheroWithWeapon&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;WonderWoman&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nn"&gt;FavoriteWeapon&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;LassoOfTruth&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;hero_with_weapon_1&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nn"&gt;SuperheroWithWeapon&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;Batman&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;weapon&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;"Batman: {weapon:?}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="nn"&gt;SuperheroWithWeapon&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;WonderWoman&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;weapon&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;"Wonder Woman: {weapon:?}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="nn"&gt;SuperheroWithWeapon&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;GreenLantern&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;weapon&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;"Green Lantern: {weapon:?}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;other&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;"{other:?} doesn't usually use weapons."&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;If you run this code, you will get:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Wonder Woman: LassoOfTruth
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we've created two Enums, the &lt;code&gt;SuperheroWithWeapon&lt;/code&gt; that accepts Superheroes with favorite weapons or without, and the &lt;code&gt;FavoriteWeapon&lt;/code&gt; Enums that is passed to the previous one if the superhero uses a favorite weapon.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Notice the #[derive(Debug)] again.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This time, we've created a &lt;code&gt;hero_with_weapon_1&lt;/code&gt; variable that hold a &lt;code&gt;WonderWoman&lt;/code&gt; variant of the &lt;code&gt;SuperheroWithWeapon&lt;/code&gt; Enum and she uses the &lt;code&gt;LassoOfTruth&lt;/code&gt; as her favorite weapon 🪢. In order to use the data of the favorite weapon that is bound to Wonder Woman, we have "named" it &lt;code&gt;weapon&lt;/code&gt; in the match arm when we were defining the pattern to match against then we can use it normally in the match arm expression as in&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="nn"&gt;SuperheroWithWeapon&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;WonderWoman&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;weapon&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;"Wonder Woman: {weapon:?}"&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;You have noticed that we didn't list all the Enum's variants this time and we only listed &lt;code&gt;Batman&lt;/code&gt;, &lt;code&gt;WonderWoman&lt;/code&gt;, &lt;code&gt;GreenLantern&lt;/code&gt;, and a mysterious &lt;code&gt;other&lt;/code&gt; placeholder?? Actually, this placeholder serves as a &lt;strong&gt;"catch all"&lt;/strong&gt; match arm (or the &lt;code&gt;else&lt;/code&gt; in the &lt;code&gt;if&lt;/code&gt; expression). Therefore, our patterns in this example fulfil the match requirement of being exhaustive. Also, notice that all the match arms return the same unit tuple "()" (println! return value).&lt;/p&gt;

&lt;h3&gt;
  
  
  Using the &lt;strong&gt;"_"&lt;/strong&gt; placeholder:
&lt;/h3&gt;

&lt;p&gt;Another way to use a "catch all" match arm, is using the "_" place holder. But this time, we won't use any data in the match arm (similar to Python's "_" in for loops or tuples expansion).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="nd"&gt;#[derive(Debug)]&lt;/span&gt;
&lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;SuperheroWithWeapon&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;Batman&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;FavoriteWeapon&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="n"&gt;Superman&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nf"&gt;WonderWoman&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;FavoriteWeapon&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="n"&gt;Flash&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nf"&gt;GreenLantern&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;FavoriteWeapon&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="n"&gt;CatWoman&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(Debug)]&lt;/span&gt;
&lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;FavoriteWeapon&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;LassoOfTruth&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;GreenLanternRing&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;Batarang&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="c1"&gt;// Matching with patterns that bind to values with _ placeholder.&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;hero_with_weapon_2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;SuperheroWithWeapon&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Superman&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;hero_with_weapon_2&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nn"&gt;SuperheroWithWeapon&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;Batman&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;weapon&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;"Batman: {weapon:?}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="nn"&gt;SuperheroWithWeapon&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;WonderWoman&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;weapon&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;"Wonder Woman: {weapon:?}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="nn"&gt;SuperheroWithWeapon&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;GreenLantern&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;weapon&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;"Green Lantern: {weapon:?}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;_&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;"Doesn't usually use weapons."&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;If you run this code, you will get:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Doesn't usually use weapons.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This time we have created &lt;code&gt;hero_with_weapon_2&lt;/code&gt; as a &lt;code&gt;Superman&lt;/code&gt; variant which doesn't use any weapons. The &lt;code&gt;match&lt;/code&gt; in this case will go to the "catch all" arm represented by the "_" placeholder which prints a generic message and doesn't use any data.  &lt;/p&gt;

&lt;p&gt;We will revisit "pattern matching" again later. In the next one, we will check an important Enum, the &lt;strong&gt;"Option"&lt;/strong&gt; Enum which protects us from &lt;strong&gt;Null&lt;/strong&gt; values bugs 🫢. See you then 👋.&lt;/p&gt;

</description>
      <category>learning</category>
      <category>rust</category>
      <category>programming</category>
      <category>development</category>
    </item>
    <item>
      <title>Learning Rust 🦀: 12 - Basic enum</title>
      <dc:creator>Fady GA 😎</dc:creator>
      <pubDate>Sat, 02 Sep 2023 15:58:04 +0000</pubDate>
      <link>https://dev.to/fadygrab/learning-rust-12-basic-enum-2mn3</link>
      <guid>https://dev.to/fadygrab/learning-rust-12-basic-enum-2mn3</guid>
      <description>&lt;p&gt;This is the first article in about 3 or 4 articles that will discuss &lt;strong&gt;enum&lt;/strong&gt; and &lt;strong&gt;pattern matching&lt;/strong&gt;. In this one, I'll show you how to define and use a basic enum.&lt;/p&gt;

&lt;blockquote&gt;
&lt;h4&gt;
  
  
  ⚠️ Remember!
&lt;/h4&gt;

&lt;p&gt;You can find all the code snippets for this series in its &lt;a href="https://github.com/FadyGrAb/rust-learning-series"&gt;accompanying repo&lt;/a&gt;&lt;br&gt;&lt;br&gt;
If you don't want to install Rust locally, you can play with all the code of this series in the official &lt;a href="https://play.rust-lang.org/?version=stable&amp;amp;mode=debug&amp;amp;edition=2021"&gt;Rust Playground&lt;/a&gt; that can be found on its official page.&lt;/p&gt;

&lt;p&gt;⚠️⚠️ The articles in this series are loosely following the contents of "The Rust Programming Language, 2nd Edition" by Steve Klabnik and Carol Nichols in a way that reflects my understanding from a Python developer's perspective.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;⭐ I try to publish a new article every week (maybe more if the Rust gods 🙌 are generous 😁) so stay tuned 😉. I'll be posting "new articles updates" on my &lt;a href="https://www.linkedin.com/in/fady-abdelmassih"&gt;LinkedIn&lt;/a&gt; and &lt;a href="https://twitter.com/fadyGrAb"&gt;Twitter&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Table of Contents:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;What's an enum&lt;/li&gt;
&lt;li&gt;Defining enum&lt;/li&gt;
&lt;li&gt;Structs and enums&lt;/li&gt;
&lt;li&gt;Implementing enum methods&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  What's an enum:
&lt;/h3&gt;

&lt;p&gt;In the &lt;a href="https://dev.to/fadygrab/learning-rust-10-the-struct-1efd"&gt;Struct article&lt;/a&gt;, we saw how a &lt;strong&gt;Struct&lt;/strong&gt; can be used to group relevant data together as in the &lt;em&gt;width&lt;/em&gt; and &lt;em&gt;height&lt;/em&gt; of a &lt;strong&gt;Rectangle&lt;/strong&gt;. The &lt;strong&gt;enum&lt;/strong&gt; is similar to the &lt;strong&gt;struct&lt;/strong&gt; in a way that it "enumerates" all the "variants" of the custom type we are creating in one place. Think of our earlier &lt;strong&gt;Rectangle&lt;/strong&gt; struct, with &lt;strong&gt;enum&lt;/strong&gt;, we can make a &lt;strong&gt;Shape&lt;/strong&gt; type that contains a &lt;strong&gt;Rectangle&lt;/strong&gt;, a &lt;strong&gt;Circle&lt;/strong&gt;, and a &lt;strong&gt;Square&lt;/strong&gt;. Each with its own "properties" and "functionality". To me, I couldn't find a similar concept in Python so this is a new one for me.&lt;/p&gt;

&lt;h3&gt;
  
  
  Defining enum:
&lt;/h3&gt;

&lt;p&gt;We use the &lt;code&gt;enum&lt;/code&gt; keyword to define enums. The definition can be as simple as the following:&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;enum&lt;/span&gt; &lt;span class="n"&gt;SimpleAnswer&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;False&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;Here we are defining a &lt;strong&gt;SimpleAnswer&lt;/strong&gt; custom type and its variants could be either &lt;strong&gt;True&lt;/strong&gt; or &lt;strong&gt;False&lt;/strong&gt; (needed for an automatic exam grading system for example).&lt;br&gt;&lt;br&gt;
We can very easily use them and pass them to functions as follows:&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;enum&lt;/span&gt; &lt;span class="n"&gt;SimpleAnswer&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;False&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="c1"&gt;// Enum values&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;correct_answer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;SimpleAnswer&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;True&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;wrong_answer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;SimpleAnswer&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;False&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// Enums can be passed to functions&lt;/span&gt;
    &lt;span class="nf"&gt;evaluate_answer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;correct_answer&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nf"&gt;evaluate_answer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;wrong_answer&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;evaluate_answer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user_answer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;SimpleAnswer&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;Here we are creating two answers that use our &lt;strong&gt;SimpleAnswer&lt;/strong&gt; type, one is correct and the other is wrong. And we can pass any of them to the &lt;code&gt;evaluate_answer&lt;/code&gt; function for evaluation (that does nothing for now😁).&lt;/p&gt;

&lt;h3&gt;
  
  
  Struct and enums:
&lt;/h3&gt;

&lt;p&gt;Right now, our enum doesn't contain any data! Just the variants names. One could be tempted to use his &lt;strong&gt;Struct&lt;/strong&gt; knowledge and write something like this to include data, say the value of the answer:&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;enum&lt;/span&gt; &lt;span class="n"&gt;SimpleAnswer&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;False&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;UserAnswer&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;value&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;evaluation&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;SimpleAnswer&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;true_with_value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;UserAnswer&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;value&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;"The sky is blue"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="n"&gt;evaluation&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nn"&gt;SimpleAnswer&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;True&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;false_with_value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;UserAnswer&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;value&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;"The sky is brown"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="n"&gt;evaluation&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nn"&gt;SimpleAnswer&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;False&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 could work but it's too much typing 😁 also there is a much simpler way to include data in enums. We can define an &lt;strong&gt;enum&lt;/strong&gt; with as many variants as we want and with as many data bound to those variants as we want, take the follow as an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;Superhero&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Batman&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// No data, he is ... Batman&lt;/span&gt;
    &lt;span class="nf"&gt;Superman&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;u32&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="c1"&gt;// Current altitude as u32&lt;/span&gt;
    &lt;span class="n"&gt;WonderWoman&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;lasso_of_truth&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;bool&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sword&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;bool&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="c1"&gt;// Struct-like data&lt;/span&gt;
    &lt;span class="nf"&gt;Flash&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;u32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;u32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;u32&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="c1"&gt;// Speedforce alarm levels as u32&lt;/span&gt;
    &lt;span class="nf"&gt;GreenLantern&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;LanternCorps&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="c1"&gt;// LanternCorps is a Struct!&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each variant can take its data in a different way, this is cool 😎! So, getting back to our "Answers" enum example, we could integrate the answer value as follows:&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;enum&lt;/span&gt; &lt;span class="n"&gt;Answer&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;True&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="nf"&gt;False&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="c1"&gt;// Enums with values&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;true_with_value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;Answer&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;True&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;"The sky is blue"&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;false_with_value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;Answer&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;False&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;"The sky is brown"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Implementing enum methods:
&lt;/h3&gt;

&lt;p&gt;Similar to structs, we can bind enums with methods with the &lt;code&gt;impl&lt;/code&gt; keyword and use the &lt;code&gt;&amp;amp;self&lt;/code&gt; shorthand to pass the enum to the methods. For demonstrating this one, take a look at the following example implementing a &lt;strong&gt;Shape&lt;/strong&gt; type similar to what I've mentioned at the beginning of this article:&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;enum&lt;/span&gt; &lt;span class="n"&gt;Shape&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Rectangle&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;f32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;f32&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="nf"&gt;Circle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;f32&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nf"&gt;Square&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;f32&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;impl&lt;/span&gt; &lt;span class="n"&gt;Shape&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;area&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="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;f32&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;match&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;self&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;Shape&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Rectangle&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="n"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;h&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="n"&gt;w&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;Shape&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;Circle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;radius&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;radius&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;radius&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mf"&gt;3.14&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;Shape&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;Square&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;side&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;side&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;side&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;get_type&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="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;match&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;self&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;Shape&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Rectangle&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;width&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="n"&gt;height&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="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;"Rectangle"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;Shape&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;Circle&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="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;"Circle"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;Shape&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;Square&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="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;"Square"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="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;r&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;Shape&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Rectangle&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;10.0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;5.0&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;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;Shape&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;Circle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;5.0&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;Shape&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;Square&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;4.0&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;shapes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;c&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="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;shape&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;shapes&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;shape&lt;/span&gt;&lt;span class="nf"&gt;.get_type&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;shape&lt;/span&gt;&lt;span class="nf"&gt;.area&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;There is a lot going on in this example so let's break it down 😉. First, we defined our &lt;strong&gt;Shape&lt;/strong&gt; enum that has 3 variants, &lt;strong&gt;Rectangle&lt;/strong&gt;, &lt;strong&gt;Circle&lt;/strong&gt;, and &lt;strong&gt;Square&lt;/strong&gt;. For the &lt;strong&gt;Rectangle&lt;/strong&gt;, it has a struct-like data structure that accepts the &lt;strong&gt;width&lt;/strong&gt; and &lt;strong&gt;height&lt;/strong&gt; properties as floats (f32). For the &lt;strong&gt;Circle&lt;/strong&gt; and &lt;strong&gt;Square&lt;/strong&gt;, they only take one float value which is the &lt;strong&gt;radius&lt;/strong&gt; and the &lt;strong&gt;side&lt;/strong&gt; for each shape respectively.&lt;br&gt;&lt;br&gt;
Then we have implemented two methond bound to this &lt;strong&gt;Shape&lt;/strong&gt; enum, the &lt;code&gt;area&lt;/code&gt; and &lt;code&gt;get_type&lt;/code&gt;. They use something called &lt;strong&gt;Pattern Matching&lt;/strong&gt; with the &lt;code&gt;match&lt;/code&gt; keyword that we will discuss in detail in the next article. For now, just know that &lt;code&gt;area&lt;/code&gt; calculates the area of the shape and &lt;code&gt;get_type&lt;/code&gt; returns the shape type as a string literal.&lt;br&gt;&lt;br&gt;
Lastly, we have created 3 variables, &lt;code&gt;r&lt;/code&gt;, &lt;code&gt;c&lt;/code&gt;, and &lt;code&gt;s&lt;/code&gt; which are &lt;strong&gt;Rectangle&lt;/strong&gt;, &lt;strong&gt;Circle&lt;/strong&gt;, and &lt;strong&gt;Square&lt;/strong&gt; variants of the &lt;strong&gt;Shape&lt;/strong&gt; type respectively. Then we crammed all of them in an array called &lt;code&gt;shapes&lt;/code&gt; and we looped over this array printing the type and the area of each shape as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;Finished dev &lt;span class="o"&gt;[&lt;/span&gt;unoptimized + debuginfo] target&lt;span class="o"&gt;(&lt;/span&gt;s&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;in &lt;/span&gt;14.11s
     Running &lt;span class="sb"&gt;`&lt;/span&gt;target/debug/structs_basics&lt;span class="sb"&gt;`&lt;/span&gt;
Rectangle: 50
Circle: 78.5
Square: 16
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's it for now for &lt;strong&gt;enums&lt;/strong&gt;. In the next one, we will dig deeper into the &lt;strong&gt;Pattern Matching&lt;/strong&gt; that we glimpsed in this article. See you then 👋.&lt;/p&gt;

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