<?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: Khair Alanam</title>
    <description>The latest articles on DEV Community by Khair Alanam (@khair_al_anam).</description>
    <link>https://dev.to/khair_al_anam</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%2F843376%2F8dddc2ff-8154-4ccf-829f-84f1a091e5c6.jpg</url>
      <title>DEV Community: Khair Alanam</title>
      <link>https://dev.to/khair_al_anam</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/khair_al_anam"/>
    <language>en</language>
    <item>
      <title>Rust Tutorial 5: Let's Build a Simple Calculator! (Part 2)</title>
      <dc:creator>Khair Alanam</dc:creator>
      <pubDate>Thu, 28 Dec 2023 10:15:56 +0000</pubDate>
      <link>https://dev.to/khair_al_anam/rust-tutorial-5-lets-build-a-simple-calculator-part-2-332o</link>
      <guid>https://dev.to/khair_al_anam/rust-tutorial-5-lets-build-a-simple-calculator-part-2-332o</guid>
      <description>&lt;p&gt;&lt;em&gt;Reading Time: 15 minutes&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Welcome back to the Rust Tutorial Series!&lt;/p&gt;

&lt;p&gt;We will be continuing the simple calculator project, and on the way learn new concepts like tuples, arrays, etc.&lt;/p&gt;

&lt;p&gt;So, let's get started!&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%2Fuknxbp1g16w6prb4gw6o.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%2Fuknxbp1g16w6prb4gw6o.png" alt="Semicolon and Code meme"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  More functions!
&lt;/h2&gt;

&lt;p&gt;Going back to the code, notice that for the operations we do, we are doing them in our main code and not refactored into separate functions.&lt;/p&gt;

&lt;p&gt;Now for this project, our functions are all one-liners. But it's a good practice to keep operations as separate functions so that we can maintain the readability of our main program code.&lt;/p&gt;

&lt;p&gt;So, let's write functions for each of the operations:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 rust
fn add(x: f64, y: f64) -&amp;gt; f64 {
    return x + y;
}

fn subtract(x: f64, y: f64) -&amp;gt; f64 {
    return x - y;
}

fn multiply(x: f64, y: f64) -&amp;gt; f64 {
    return x * y;
}

fn divide(x: f64, y: f64) -&amp;gt; f64 {
    return x / y;
}


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

&lt;/div&gt;

&lt;p&gt;Now, let's replace each of the operations in the &lt;code&gt;match&lt;/code&gt; statement &lt;code&gt;op&lt;/code&gt; with these functions:&lt;/p&gt;

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

use std::io;

fn main() {

    // rest of the code

    match op {
        1 =&amp;gt; result = add(x, y),
        2 =&amp;gt; result = subtract(x, y),
        3 =&amp;gt; result = multiply(x, y),
        4 =&amp;gt; result = divide(x, y),
        _ =&amp;gt; {
            println!("Invalid selection");
            return;
        }
    }

    println!("The result is: {}", result);
}


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

&lt;/div&gt;

&lt;p&gt;Now that looks good. Let's get back to our four functions again.&lt;/p&gt;

&lt;p&gt;Remember this block of code from a previous tutorial?&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 rust
fn main() {
    let some_num: i32 = {
        let x: i32 = 45;
        let y: i32 = 34;
        x + y
    };

    println!("{}", some_num);
}



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

&lt;/div&gt;

&lt;p&gt;And remember that odd-looking &lt;code&gt;x + y&lt;/code&gt; line of code that returns itself to the &lt;code&gt;some_num&lt;/code&gt; variable? Well, we can do that in functions too! &lt;/p&gt;

&lt;p&gt;In our four functions, we can remove the &lt;code&gt;return&lt;/code&gt; keyword and the semi-colon to just do the same thing:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 rust
fn add(x: f64, y: f64) -&amp;gt; f64 {
    x + y
}

fn subtract(x: f64, y: f64) -&amp;gt; f64 {
    x - y
}

fn multiply(x: f64, y: f64) -&amp;gt; f64 {
    x * y
}

fn divide(x: f64, y: f64) -&amp;gt; f64 {
    x / y
}


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

&lt;/div&gt;

&lt;p&gt;These are still valid functions! These "naked returns" can be tricky to someone who isn't used to Rust. But just know that Rust can do this :D&lt;/p&gt;




&lt;p&gt;Let's take a look at this part of the code we wrote:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 rust
match op {
        1 =&amp;gt; result = add(x, y),
        2 =&amp;gt; result = subtract(x, y),
        3 =&amp;gt; result = multiply(x, y),
        4 =&amp;gt; result = divide(x, y),
        _ =&amp;gt; {
            println!("Invalid selection");
            return;
        }
    }


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

&lt;/div&gt;

&lt;p&gt;Notice that the numbers are not random and they are ordered?&lt;/p&gt;

&lt;p&gt;Wouldn't it be nice to keep them in an ordered sequence in such a way that I can just access them via "indices", and get rid of the entire &lt;code&gt;match&lt;/code&gt; statement completely?&lt;/p&gt;

&lt;p&gt;That's where tuples and arrays come in!&lt;/p&gt;




&lt;h2&gt;
  
  
  Tuples
&lt;/h2&gt;

&lt;p&gt;Tuples are compound data structures that can hold different types of data. These data are enclosed in parentheses &lt;code&gt;()&lt;/code&gt;. They are fixed in length.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 rust
    let my_tup = (1, true, "Hello");


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

&lt;/div&gt;

&lt;p&gt;To add types for a tuple, you have to specify the type for each data inside the tuple in the exact order, enclosed in parentheses like this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 rust
    let my_tup: (i32, bool, &amp;amp;str) = (1, true, "Hello");


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

&lt;/div&gt;

&lt;p&gt;We will get to know more about tuples in the later sections.&lt;/p&gt;




&lt;h2&gt;
  
  
  Arrays
&lt;/h2&gt;

&lt;p&gt;Arrays are just like tuples in many ways, except unlike tuples, they can only hold values of the same data type. These data are enclosed in square brackets &lt;code&gt;[]&lt;/code&gt;. They are also fixed in length.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 rust
    let my_arr = [1, 2, 3, 4, 5];


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

&lt;/div&gt;

&lt;p&gt;To add types for an array, you have to specify two parameters enclosed in square brackets; the first is the data type for each value in the array, and the second is the length of the array. These parameters are separated by a semi-colon. Here's what it looks like:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 rust
    let my_arr: [i32; 5] = [1, 2, 3, 4, 5];


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

&lt;/div&gt;

&lt;p&gt;We will get to know more about arrays in the later sections.&lt;/p&gt;




&lt;h2&gt;
  
  
  Accessing values in tuples and arrays
&lt;/h2&gt;

&lt;p&gt;If you ever come across accessing values in arrays or lists in other programming languages, then it is almost identical here in Rust. Rust follows zero-indexing, that is the first element is in the 0th index, the second element is in the 1st index, and so on.&lt;/p&gt;

&lt;p&gt;However, The syntax to access values in arrays is very different from that of tuples. Let me show you:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 rust
fn main() {
    let my_arr: [i32; 5] = [1, 2, 3, 4, 5];
    let my_tup: (i32, f64, bool) = (23, 16.4, false);

    println!("{}", my_arr[0]);  // prints 1
    println!("{}", my_tup.1);   // prints 16.4
}


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

&lt;/div&gt;

&lt;p&gt;If you notice, we use square brackets to access any element in an array based on the given index. However, in tuples, we use dot notation.&lt;/p&gt;

&lt;p&gt;Also, just a quick note, if you want to print the entire tuple or array in one print statement. We have to use the &lt;code&gt;{:?}&lt;/code&gt; formatting instead of &lt;code&gt;{}&lt;/code&gt; to do the same:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 rust
    let my_arr: [i32; 5] = [1, 2, 3, 4, 5];
    let my_tup: (i32, f64, bool) = (23, 16.4, false);

    println!("{:?}", my_arr);
    println!("{:?}", my_tup);


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

&lt;/div&gt;

&lt;p&gt;Now let's get back to our project.&lt;/p&gt;




&lt;p&gt;Let's create an array of length four to enclose all the four operations:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 rust
let ops = [add, subtract, multiply, divide];


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

&lt;/div&gt;

&lt;p&gt;Remember when adding the function names in the array or tuple, you should not use parentheses along with the function name.&lt;/p&gt;

&lt;p&gt;Now you might be thinking, what will be the data type of a function?&lt;/p&gt;

&lt;p&gt;It's not necessary to mention types during declaration as Rust infers the type from the given value. However, here's what it looks like:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 rust
let ops: [fn(f64, f64) -&amp;gt; f64; 4] = [add, subtract, multiply, divide];


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

&lt;/div&gt;

&lt;p&gt;Let's break down the function type:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Firstly, we have the &lt;code&gt;fn&lt;/code&gt; keyword to signify that the values given inside the &lt;code&gt;ops&lt;/code&gt; array are functions.&lt;/li&gt;
&lt;li&gt;Then we have two &lt;code&gt;f64&lt;/code&gt;s enclosed inside the &lt;code&gt;fn&lt;/code&gt; keyword. This is to signify the data types of parameters of these four functions. If you remember, all these four functions take two decimals as arguments which are of &lt;code&gt;f64&lt;/code&gt; type.&lt;/li&gt;
&lt;li&gt;Lastly, we have the &lt;code&gt;f64&lt;/code&gt; after an arrow &lt;code&gt;-&amp;gt;&lt;/code&gt;. This is the return type of the function. As you can notice, every function in the given array returns a decimal of type &lt;code&gt;f64&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That's pretty much the breakdown. To summarise this bit, if you are including the functions in your array, make sure they have these checked:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;All the values are of the &lt;code&gt;fn&lt;/code&gt; type&lt;/li&gt;
&lt;li&gt;The number of parameters AND the datatypes of each of the parameters IN the exact order are the same for every function.&lt;/li&gt;
&lt;li&gt;Finally, the return type for each function has to be the same.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;Now that we have our operations array, we can completely remove the &lt;code&gt;match&lt;/code&gt; case statement and just use the desired operation based on the selection given:&lt;/p&gt;

&lt;p&gt;So let's write this one line of code by replacing the &lt;code&gt;match&lt;/code&gt; case statement:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 rust
result = ops[op - 1];


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

&lt;/div&gt;

&lt;p&gt;You will get an error saying that you cannot use an &lt;code&gt;i32&lt;/code&gt; as an index. So for indexing, you have to use the type casting to convert &lt;code&gt;op - 1&lt;/code&gt;'s type of &lt;code&gt;i32&lt;/code&gt; to something called &lt;code&gt;usize&lt;/code&gt; which is the type defined for those variables that act as pointers to the array elements.&lt;/p&gt;

&lt;p&gt;It will look like this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 rust
result = ops[(op - 1) as usize];


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

&lt;/div&gt;

&lt;p&gt;Later, you will get a mismatched type error. This is because the &lt;code&gt;result&lt;/code&gt; variable is of type &lt;code&gt;f64&lt;/code&gt; but you are assigning an array element of type &lt;code&gt;fn&lt;/code&gt;, hence the mismatched type.&lt;/p&gt;

&lt;p&gt;So to fix this, you have to "call" the operation along with arguments &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt; using parentheses.&lt;/p&gt;

&lt;p&gt;If the above sounds confusing, here's what it looks like:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 rust
result = ops[(op - 1) as usize](x, y);


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

&lt;/div&gt;

&lt;p&gt;Finally, we have to put an &lt;code&gt;if&lt;/code&gt; check to see if the &lt;code&gt;op&lt;/code&gt; lies in the range of 1 to 4 before assigning the result to the &lt;code&gt;result&lt;/code&gt; variable, or else we will get the "out of range" error.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 rust
if op &amp;gt; 4 || op &amp;lt; 1 {
    println!("Invalid Selection!");
    return;
}

result = ops[(op - 1) as usize](x, y);


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

&lt;/div&gt;

&lt;p&gt;Now, the final code will look like this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 rust
use std::io;

fn main() {
    let result: f64;
    let ops: [fn(f64, f64) -&amp;gt; f64; 4] = [add, subtract, multiply, divide];

    println!("Enter the first number: ");
    let x: f64 = input_parser();

    if f64::is_nan(x) {
        println!("Invalid input!");
        return;
    }

    println!("Enter the second number: ");
    let y: f64 = input_parser();


    if f64::is_nan(y) {
        println!("Invalid input!");
        return;
    }

    println!("List of operators:");
    println!("(1) Add");
    println!("(2) Subtract");
    println!("(3) Multiply");
    println!("(4) Divide");
    println!("Select the number associated with the desired operation: ");

    let op: f64 = input_parser();

    if f64::is_nan(op) {
        println!("Invalid input!");
        return;
    }

    let op: i32 = op as i32;

    if op &amp;gt; 4 || op &amp;lt; 1 {
        println!("Invalid Selection!");
        return;
    }

    result = ops[(op - 1) as usize](x, y);

    println!("The result is: {}", result);
}

fn input_parser() -&amp;gt; f64 {
    let mut x: String = String::new();
    io::stdin().read_line(&amp;amp;mut x).expect("Invalid Input");
    let x: f64 = match x.trim().parse() {
        Ok(num) =&amp;gt; num,
        Err(_) =&amp;gt; {
            return f64::NAN;
        }
    };

    return x;
}

fn add(x: f64, y: f64) -&amp;gt; f64 {
    x + y
}

fn subtract(x: f64, y: f64) -&amp;gt; f64 {
    x - y
}

fn multiply(x: f64, y: f64) -&amp;gt; f64 {
    x * y
}

fn divide(x: f64, y: f64) -&amp;gt; f64 {
    x / y
}


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

&lt;/div&gt;




&lt;p&gt;Now let's make this calculator work as much as we want to by wrapping the logic inside a &lt;code&gt;loop&lt;/code&gt; statement.&lt;/p&gt;

&lt;p&gt;Here's the final code:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 rust
use std::io;

fn main() {
    let mut result: f64;
    let mut y_or_n: String = String::new();
    let ops: [fn(f64, f64) -&amp;gt; f64; 4] = [add, subtract, multiply, divide];

    loop {
        println!("Enter the first number: ");
        let x: f64 = input_parser();
        if f64::is_nan(x) {
            println!("Invalid input!");
            return;
        }

        println!("Enter the second number: ");
        let y: f64 = input_parser();


        if f64::is_nan(y) {
            println!("Invalid input!");
            return;
        }

        println!("List of operators:");
        println!("(1) Add");
        println!("(2) Subtract");
        println!("(3) Multiply");
        println!("(4) Divide");
        println!("Select the number associated with the desired operation: ");

        let op: f64 = input_parser();

        if f64::is_nan(op) {
            println!("Invalid input!");
            return;
        }

        let op: i32 = op as i32;

        if op &amp;gt; 4 || op &amp;lt; 1 {
            println!("Invalid Selection!");
            return;
        }

        result = ops[(op - 1) as usize](x, y);

        println!("The result is: {}", result);

        println!("Continue? (y/n)");
        io::stdin().read_line(&amp;amp;mut y_or_n).expect("Invalid Input");

        if y_or_n.trim() == "n" {
            break;
        }
    }
}


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

&lt;/div&gt;

&lt;p&gt;Here's what I did:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;I declared &lt;code&gt;result&lt;/code&gt; as mutable using &lt;code&gt;mut&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;I declared a new variable &lt;code&gt;y_or_n&lt;/code&gt; to check for loop condition whether to continue or not.&lt;/li&gt;
&lt;li&gt;Then I wrapped the entire calculation logic inside a &lt;code&gt;loop&lt;/code&gt; statement, except the first three declarations.&lt;/li&gt;
&lt;li&gt;After the &lt;code&gt;result&lt;/code&gt; printing, I take the user input for &lt;code&gt;y_or_n&lt;/code&gt;. Since we want a string, we don't have to parse it for integer input.&lt;/li&gt;
&lt;li&gt;Finally I check for the condition comparing the input with &lt;code&gt;"n"&lt;/code&gt;. I used the &lt;code&gt;trim()&lt;/code&gt; function because when you press Enter key to finish the input, that key press is also recorded in the input (as &lt;code&gt;\n&lt;/code&gt;). So to remove that &lt;code&gt;\n&lt;/code&gt;, we use the &lt;code&gt;trim()&lt;/code&gt; function.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;That's pretty much it! Now you should probably be good with the Rust basics with just this simple calculator project!&lt;/p&gt;

&lt;p&gt;In the next tutorial, we will explore one of the most important concepts of Rust and that is the concept of ownership and how memory management works in Rust.&lt;/p&gt;

&lt;p&gt;Until then, have a great day ahead!&lt;/p&gt;

&lt;p&gt;GitHub Repo: &lt;a href="https://github.com/khairalanam/rust-calculator" rel="noopener noreferrer"&gt;https://github.com/khairalanam/rust-calculator&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you like whatever I write here, follow me on Devto and check out my socials:&lt;/p&gt;

&lt;p&gt;LinkedIn: &lt;a href="https://www.linkedin.com/in/khair-alanam-b27b69221/" rel="noopener noreferrer"&gt;https://www.linkedin.com/in/khair-alanam-b27b69221/&lt;/a&gt;&lt;br&gt;
Twitter: &lt;a href="https://www.twitter.com/khair_alanam" rel="noopener noreferrer"&gt;https://www.twitter.com/khair_alanam&lt;/a&gt;&lt;br&gt;
GitHub: &lt;a href="https://github.com/khairalanam" rel="noopener noreferrer"&gt;https://github.com/khairalanam&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I also have a new portfolio! I am a web developer as well as a web designer with 3+ years of experience designing creative web experiences for everyone.&lt;/p&gt;

&lt;p&gt;Check it out: &lt;a href="https://khairalanam.carrd.co/" rel="noopener noreferrer"&gt;https://khairalanam.carrd.co/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>rust</category>
      <category>tutorial</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Rust Tutorial 4: Let's build a Simple Calculator! (Part 1)</title>
      <dc:creator>Khair Alanam</dc:creator>
      <pubDate>Sat, 28 Oct 2023 07:53:02 +0000</pubDate>
      <link>https://dev.to/khair_al_anam/rust-tutorial-4-lets-build-a-simple-calculator-part-1-59fe</link>
      <guid>https://dev.to/khair_al_anam/rust-tutorial-4-lets-build-a-simple-calculator-part-1-59fe</guid>
      <description>&lt;p&gt;&lt;em&gt;Reading time: 20 minutes&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Welcome back to the Rust Tutorial Series!&lt;/p&gt;

&lt;p&gt;In this tutorial, we will be building a simple calculator! On the way, we will learn some more concepts like functions, generics, tuples, arrays, and more!&lt;/p&gt;

&lt;p&gt;This tutorial will be a 2-parter since we will be covering a heck load of concepts with just this simple project. The Rust Tutorial 5 will just be the second part of this tutorial.&lt;/p&gt;

&lt;p&gt;So let's get started!&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%2F1r9a6gczu44pfg2v60g7.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%2F1r9a6gczu44pfg2v60g7.jpg" alt="No Compile Error Meme"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Setup
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Run the &lt;code&gt;cargo new rust_calculator&lt;/code&gt; command to create a new Rust project.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Open the &lt;code&gt;rust_calculator&lt;/code&gt; project in your code editor.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's start!&lt;/p&gt;




&lt;h2&gt;
  
  
  Getting the user inputs
&lt;/h2&gt;

&lt;p&gt;For our calculator, we want two numbers as the user input. So, let's set them up.&lt;/p&gt;

&lt;p&gt;Go to the &lt;code&gt;main.rs&lt;/code&gt; file and code this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 rust
use std::io;

fn main() {
    let mut x: String = String::new();
    let mut y: String = String::new();

    println!("Enter the first number: ");
    io::stdin().read_line(&amp;amp;mut x).expect("Invalid Input");

    println!("Enter the second number: ");
    io::stdin().read_line(&amp;amp;mut y).expect("Invalid Input");

}


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

&lt;/div&gt;

&lt;p&gt;Funnily enough, I had to refer to the previous tutorial to see how to get the user input because I forgot lol. So yeah 😅.&lt;/p&gt;

&lt;p&gt;We also need the input to get the type of operation to be done between the given two numbers.&lt;/p&gt;

&lt;p&gt;There are many ways to do this. Do remember that for all the cases, we will handle the errors in case of invalid input (Please refer to the previous tutorial for handling errors).&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We can ask the user for any of the four operations (&lt;code&gt;+&lt;/code&gt;, &lt;code&gt;-&lt;/code&gt;, &lt;code&gt;*&lt;/code&gt;, &lt;code&gt;/&lt;/code&gt;) directly.&lt;/li&gt;
&lt;li&gt;We can ask the user to enter the commands like &lt;code&gt;ADD&lt;/code&gt;, &lt;code&gt;SUBTRACT&lt;/code&gt;, &lt;code&gt;MULTIPLY&lt;/code&gt;, and &lt;code&gt;DIVIDE&lt;/code&gt;. These commands can be used to trigger the corresponding operation in our code.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Or here's my way:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We show the set of operations the user can select in the terminal and then the user has to input the selection number. Then we can map that selection number to the operation we need.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So let's do that!&lt;/p&gt;

&lt;p&gt;Here's the current code after asking for the operator input:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 rust
use std::io;

fn main() {
    let mut x: String = String::new();
    let mut y: String = String::new();
    let mut op: String = String::new();

    println!("Enter the first number: ");
    io::stdin().read_line(&amp;amp;mut x).expect("Invalid Input");

    println!("Enter the second number: ");
    io::stdin().read_line(&amp;amp;mut y).expect("Invalid Input");

    println!("List of operators:");
    println!("(1) Add");
    println!("(2) Subtract");
    println!("(3) Multiply");
    println!("(4) Divide");
    println!("Select the number associated with the desired operation: ");

    io::stdin().read_line(&amp;amp;mut op).expect("Invalid Input");
}


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

&lt;/div&gt;

&lt;p&gt;Let's not forget to parse the number inputs for &lt;code&gt;x&lt;/code&gt;, &lt;code&gt;y&lt;/code&gt;, and &lt;code&gt;op&lt;/code&gt;. Along with parsing the input, I will also handle the errors here using the &lt;code&gt;match&lt;/code&gt; statement (Please refer to the previous tutorial to learn more about the &lt;code&gt;match&lt;/code&gt; statement and handling errors)&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 rust
use std::io;

fn main() {
    let mut x: String = String::new();
    let mut y: String = String::new();
    let mut op: String = String::new();

    println!("Enter the first number: ");
    io::stdin().read_line(&amp;amp;mut x).expect("Invalid Input");
    let x: i32 = match x.trim().parse() {
        Ok(num) =&amp;gt; num,
        Err(_) =&amp;gt; {
            println!("Invalid input!");
            return;
        }
    };

    println!("Enter the second number: ");
    io::stdin().read_line(&amp;amp;mut y).expect("Invalid Input");
    let y: i32 = match y.trim().parse() {
        Ok(num) =&amp;gt; num,
        Err(_) =&amp;gt; {
            println!("Invalid input!");
            return;
        }
    };

    println!("List of operators:");
    println!("(1) Add");
    println!("(2) Subtract");
    println!("(3) Multiply");
    println!("(4) Divide");
    println!("Select the number associated with the desired operation: ");

    io::stdin().read_line(&amp;amp;mut op).expect("Invalid Input");
}


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

&lt;/div&gt;

&lt;p&gt;Notice that you will get warnings about unused variables. We'll ignore that for now.&lt;/p&gt;




&lt;p&gt;Now that we have the operator input, let's use a &lt;code&gt;match&lt;/code&gt; statement to see which number the operator corresponds to, do the operation, and store the result in some variable &lt;code&gt;result&lt;/code&gt; (which we have to declare). If the number is not valid, we return some default case.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 rust
use std::io;

fn main() {
    let mut x: String = String::new();
    let mut y: String = String::new();
    let result: i32;
    let mut op: String = String::new();

    println!("Enter the first number: ");
    io::stdin().read_line(&amp;amp;mut x).expect("Invalid Input");
    let x: i32 = match x.trim().parse() {
        Ok(num) =&amp;gt; num,
        Err(_) =&amp;gt; {
            println!("Invalid input!");
            return;
        }
    };

    println!("Enter the second number: ");
    io::stdin().read_line(&amp;amp;mut y).expect("Invalid Input");
    let y: i32 = match y.trim().parse() {
        Ok(num) =&amp;gt; num,
        Err(_) =&amp;gt; {
            println!("Invalid input!");
            return;
        }
    };

    println!("List of operators:");
    println!("(1) Add");
    println!("(2) Subtract");
    println!("(3) Multiply");
    println!("(4) Divide");
    println!("Select the number associated with the desired operation: ");

    io::stdin().read_line(&amp;amp;mut op).expect("Invalid Input");
    let op: i32 = match op.trim().parse() {
        Ok(num) =&amp;gt; num,
        Err(_) =&amp;gt; {
            println!("Invalid input!");
            return;
        }
    };

    match op {
        1 =&amp;gt; result = x + y,
        2 =&amp;gt; result = x - y,
        3 =&amp;gt; result = x * y,
        4 =&amp;gt; result = x / y,
        _ =&amp;gt; {
            println!("Invalid selection");
            return;
        }
    }

    println!("The result is: {}", result);
}


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

&lt;/div&gt;

&lt;p&gt;Now, let's run the code using the &lt;code&gt;cargo run&lt;/code&gt; command.&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%2Fuztsbat5i9isdp2i0uvl.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%2Fuztsbat5i9isdp2i0uvl.png" alt="Rust calculator output"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Our calculator is working!&lt;/p&gt;




&lt;h2&gt;
  
  
  Making a better calculator
&lt;/h2&gt;

&lt;p&gt;Just like our previous project, we can make this calculator even better with much better readability and other options.&lt;/p&gt;

&lt;p&gt;Firstly, if you notice, we are only doing calculations on integers and not on decimal numbers. We can change this by changing the parsing type of our inputs to &lt;code&gt;f64&lt;/code&gt;.&lt;/p&gt;

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

&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;io&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;// existing code&lt;/span&gt;

    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;f64&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;x&lt;/span&gt;&lt;span class="nf"&gt;.trim&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.parse&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&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;num&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nf"&gt;Err&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="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="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;"Invalid input!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="k"&gt;return&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="c1"&gt;// existing code&lt;/span&gt;

    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;f64&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;y&lt;/span&gt;&lt;span class="nf"&gt;.trim&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.parse&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&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;num&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nf"&gt;Err&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="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="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;"Invalid input!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="k"&gt;return&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="c1"&gt;// existing code&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;All we did was change the parsing type to &lt;code&gt;f64&lt;/code&gt; instead of &lt;code&gt;i32&lt;/code&gt; for the variables &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now you can run the program and input decimals like 23.4 and 12.3.&lt;/p&gt;




&lt;p&gt;Next, notice that the code for asking &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt; inputs from the user is repetitive. Wouldn't it be nice to keep this code as some sort of a function so that we can reuse it?&lt;/p&gt;

&lt;h2&gt;
  
  
  Functions
&lt;/h2&gt;

&lt;p&gt;Functions are basically reusable blocks of code. They help in maintaining readability.&lt;/p&gt;

&lt;p&gt;In Rust, since we have the &lt;code&gt;main&lt;/code&gt; function, we will have to write our functions outside the &lt;code&gt;main&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;A Rust function looks like this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 rust
fn add_two(x: i32, y: i32) -&amp;gt; i32 {
    return x + y;
}


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

&lt;/div&gt;

&lt;p&gt;Some points to note:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Functions are declared using the &lt;code&gt;fn&lt;/code&gt; keyword.&lt;/li&gt;
&lt;li&gt;When including parameters, each parameter has to be declared with its corresponding type like &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt; here in the example function.&lt;/li&gt;
&lt;li&gt;Notice that arrow &lt;code&gt;-&amp;gt;&lt;/code&gt; after the parameters? That basically means the return type of the function. Here, the result is the addition of two &lt;code&gt;i32&lt;/code&gt; numbers &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt; and we are returning an &lt;code&gt;i32&lt;/code&gt; result. Hence, we define the return type as &lt;code&gt;i32&lt;/code&gt; after the arrow &lt;code&gt;-&amp;gt;&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's get back to the project!&lt;/p&gt;




&lt;p&gt;Let's make a function &lt;code&gt;input_parser&lt;/code&gt; that takes &lt;code&gt;x&lt;/code&gt; of type &lt;code&gt;String&lt;/code&gt; as argument, assigns the user input, and parses the input to &lt;code&gt;f64&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Here's the code for our function:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 rust
fn input_parser() -&amp;gt; f64 {
    let mut x: String = String::new();
    io::stdin().read_line(&amp;amp;mut x).expect("Invalid Input");
    let x: f64 = match x.trim().parse() {
        Ok(num) =&amp;gt; num,
        Err(_) =&amp;gt; {
            println!("Invalid input!");
            return f64::NAN;
        }
    };

    return x;
}


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

&lt;/div&gt;

&lt;p&gt;What I basically did was copy the entire code for parsing the variable &lt;code&gt;x&lt;/code&gt; and then return that &lt;code&gt;x&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Also notice that for errors in parsing the input, we return something called a &lt;code&gt;NAN&lt;/code&gt; (Not A Number). If you know JavaScript, then this should be familiar. We are using &lt;code&gt;f64::NAN&lt;/code&gt; to return &lt;code&gt;NAN&lt;/code&gt; as f64.&lt;/p&gt;

&lt;p&gt;Now let's change the main code by using the &lt;code&gt;input_parser&lt;/code&gt; function:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 rust
use std::io;

fn main() {
    let result: f64;
    let mut op: String = String::new();

    println!("Enter the first number: ");
    let x: f64 = input_parser();

    println!("Enter the second number: ");
    let y: f64 = input_parser();

    println!("List of operators:");
    println!("(1) Add");
    println!("(2) Subtract");
    println!("(3) Multiply");
    println!("(4) Divide");
    println!("Select the number associated with the desired operation: ");

    io::stdin().read_line(&amp;amp;mut op).expect("Invalid Input");
    let op: i32 = match op.trim().parse() {
        Ok(num) =&amp;gt; num,
        Err(_) =&amp;gt; {
            println!("Invalid input!");
            return;
        }
    };

    match op {
        1 =&amp;gt; result = x + y,
        2 =&amp;gt; result = x - y,
        3 =&amp;gt; result = x * y,
        4 =&amp;gt; result = x / y,
        _ =&amp;gt; {
            println!("Invalid selection");
            return;
        }
    }

    println!("The result is: {}", result);
}

fn input_parser() -&amp;gt; f64 {
    let mut x: String = String::new();
    io::stdin().read_line(&amp;amp;mut x).expect("Invalid Input");
    let x: f64 = match x.trim().parse() {
        Ok(num) =&amp;gt; num,
        Err(_) =&amp;gt; {
            println!("Invalid input!");
            return f64::NAN;
        }
    };

    return x;
}


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

&lt;/div&gt;

&lt;p&gt;Notice how simple our main code is after we refactored the input and the parsing code into a function. Also, notice that we removed the declarations for &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt; in the main code since &lt;code&gt;input_parser()&lt;/code&gt; does that for us.&lt;/p&gt;




&lt;p&gt;We forgot to handle the error in case we get one of our numbers as &lt;code&gt;f64::NAN&lt;/code&gt;. So let's handle that in our main code:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 rust
use std::io;

fn main() {
    // existing code

    if f64::is_nan(x) {
        println!("Invalid input!");
        return;
    }

    // existing code


    if f64::is_nan(y) {
        println!("Invalid input!");
        return;
    }

    // existing code
}


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

&lt;/div&gt;

&lt;p&gt;We are using a built-in function called &lt;code&gt;f64::is_nan()&lt;/code&gt; which accepts an &lt;code&gt;f64&lt;/code&gt; parameter to check whether the given parameter is &lt;code&gt;NAN&lt;/code&gt; or not.&lt;/p&gt;




&lt;p&gt;Also, notice that &lt;code&gt;op&lt;/code&gt; is just accepting any number in the given selection (1 to 4) and can be considered as &lt;code&gt;f64&lt;/code&gt;. So let's change the types from &lt;code&gt;i32&lt;/code&gt; to &lt;code&gt;f64&lt;/code&gt;. The reason we are doing this is just so that we can use our &lt;code&gt;input_parser()&lt;/code&gt; function for the &lt;code&gt;op&lt;/code&gt; variable to not repeat ourselves.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 rust
    println!("List of operators:");
    println!("(1) Add");
    println!("(2) Subtract");
    println!("(3) Multiply");
    println!("(4) Divide");
    println!("Select the number associated with the desired operation: ");

    let op: f64 = input_parser();

    if f64::is_nan(op) {
        println!("Invalid input!");
        return;
    }


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

&lt;/div&gt;

&lt;p&gt;Just like the &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt; variables, we use &lt;code&gt;input_parser()&lt;/code&gt; for the &lt;code&gt;op&lt;/code&gt; variable too. Also, you can remove the &lt;code&gt;op&lt;/code&gt; variable declaration in the main code since &lt;code&gt;input_parser()&lt;/code&gt; takes care of that.&lt;/p&gt;




&lt;p&gt;After doing this, you will notice that there are errors related to mismatching types for &lt;code&gt;f64&lt;/code&gt; and integer in the &lt;code&gt;match&lt;/code&gt; statement for &lt;code&gt;op&lt;/code&gt;. We can fix this by converting the &lt;code&gt;op&lt;/code&gt; variable's type from &lt;code&gt;f64&lt;/code&gt; to &lt;code&gt;i32&lt;/code&gt; like this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 rust
    let op: f64 = input_parser();

    if f64::is_nan(op) {
        println!("Invalid input!");
        return;
    }

    let op: i32 = op as i32;


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

&lt;/div&gt;

&lt;p&gt;Here, by the concept of shadowing, we re-declare &lt;code&gt;op&lt;/code&gt; as an &lt;code&gt;i32&lt;/code&gt; variable. Then we assign the value of the previous &lt;code&gt;op&lt;/code&gt; variable. But since the new &lt;code&gt;op&lt;/code&gt; and old &lt;code&gt;op&lt;/code&gt; have different types, we explicitly convert the old &lt;code&gt;op&lt;/code&gt; variable type to &lt;code&gt;i32&lt;/code&gt;, which is what &lt;code&gt;as i32&lt;/code&gt; basically means. This is called type casting and is pretty essential when it comes to explicit conversions of data types.&lt;/p&gt;




&lt;p&gt;Now that everything's done, let's see the final code:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 rust
use std::io;

fn main() {
    let result: f64;

    println!("Enter the first number: ");
    let x: f64 = input_parser();

    if f64::is_nan(x) {
        println!("Invalid input!");
        return;
    }

    println!("Enter the second number: ");
    let y: f64 = input_parser();


    if f64::is_nan(y) {
        println!("Invalid input!");
        return;
    }

    println!("List of operators:");
    println!("(1) Add");
    println!("(2) Subtract");
    println!("(3) Multiply");
    println!("(4) Divide");
    println!("Select the number associated with the desired operation: ");

    let op: f64 = input_parser();

    if f64::is_nan(op) {
        println!("Invalid input!");
        return;
    }

    let op: i32 = op as i32;

    match op {
        1 =&amp;gt; result = x + y,
        2 =&amp;gt; result = x - y,
        3 =&amp;gt; result = x * y,
        4 =&amp;gt; result = x / y,
        _ =&amp;gt; {
            println!("Invalid selection");
            return;
        }
    }

    println!("The result is: {}", result);
}

fn input_parser() -&amp;gt; f64 {
    let mut x: String = String::new();
    io::stdin().read_line(&amp;amp;mut x).expect("Invalid Input");
    let x: f64 = match x.trim().parse() {
        Ok(num) =&amp;gt; num,
        Err(_) =&amp;gt; {
            return f64::NAN;
        }
    };

    return x;
}


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

&lt;/div&gt;

&lt;p&gt;Now you can run the code using the &lt;code&gt;cargo run&lt;/code&gt; command and test for some inputs.&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%2Fwf4zjjcc2paehladktft.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%2Fwf4zjjcc2paehladktft.png" alt="Rust calculator output"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;Great job! you have made a simple calculator that can do basic math operations!&lt;/p&gt;

&lt;p&gt;In the next tutorial which is just the second part of this tutorial, we will be making a better calculator and on the way, learn some more new concepts!&lt;/p&gt;

&lt;p&gt;Until then, have a great day!&lt;/p&gt;

&lt;p&gt;GitHub Repo: &lt;a href="https://github.com/khairalanam/rust-calculator" rel="noopener noreferrer"&gt;https://github.com/khairalanam/rust-calculator&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you like whatever I write here, follow me on Devto and check out my socials:&lt;/p&gt;

&lt;p&gt;LinkedIn: &lt;a href="https://www.linkedin.com/in/khair-alanam-b27b69221/" rel="noopener noreferrer"&gt;https://www.linkedin.com/in/khair-alanam-b27b69221/&lt;/a&gt;&lt;br&gt;
Twitter: &lt;a href="https://www.twitter.com/khair_alanam" rel="noopener noreferrer"&gt;https://www.twitter.com/khair_alanam&lt;/a&gt;&lt;br&gt;
GitHub: &lt;a href="https://github.com/khairalanam" rel="noopener noreferrer"&gt;https://github.com/khairalanam&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I also have a new portfolio!&lt;br&gt;
Check it out: &lt;a href="https://khairalanam.carrd.co/" rel="noopener noreferrer"&gt;https://khairalanam.carrd.co/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>rust</category>
      <category>tutorial</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Rust Tutorial 3: Handling Errors and Other Concepts</title>
      <dc:creator>Khair Alanam</dc:creator>
      <pubDate>Fri, 29 Sep 2023 17:01:57 +0000</pubDate>
      <link>https://dev.to/khair_al_anam/rust-tutorial-3-handling-errors-and-other-concepts-44g6</link>
      <guid>https://dev.to/khair_al_anam/rust-tutorial-3-handling-errors-and-other-concepts-44g6</guid>
      <description>&lt;p&gt;&lt;em&gt;Reading time: 11 minutes&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Welcome to the Rust Tutorial 3!&lt;/p&gt;

&lt;p&gt;This will be a short tutorial and is a continuation of the previous tutorial.&lt;/p&gt;

&lt;p&gt;In this tutorial, we will handle errors in our guessing game and also learn some more concepts on the way.&lt;/p&gt;

&lt;p&gt;So let's get started!&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;code&gt;const&lt;/code&gt; keyword
&lt;/h3&gt;

&lt;p&gt;Just like the &lt;code&gt;let&lt;/code&gt; keyword, &lt;code&gt;const&lt;/code&gt; is used to declare variables in Rust that will never be changed through out the program.&lt;/p&gt;

&lt;p&gt;One key thing you have to know about &lt;code&gt;const&lt;/code&gt; variables is that, we cannot mutate these variables, even if you use the &lt;code&gt;mut&lt;/code&gt; keyword. Using the &lt;code&gt;mut&lt;/code&gt; keyword in a &lt;code&gt;const&lt;/code&gt; variable will throw an error.&lt;/p&gt;

&lt;p&gt;Also, it's a convention in Rust to use upper snake case names for &lt;code&gt;const&lt;/code&gt; variables.&lt;/p&gt;

&lt;p&gt;Let's just use an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;SOME_NUMBER&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;100_000_000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Fun fact: you can use underscores in an integer if the given number is huge for better readability.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;99% of the time, you will be using the &lt;code&gt;let&lt;/code&gt; keyword. So just use that. If you are really sure that your variable doesn't change at all in the main program execution, then you may use &lt;code&gt;const&lt;/code&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;code&gt;match&lt;/code&gt; case
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;match&lt;/code&gt; case statement in Rust is very similar to the &lt;code&gt;switch&lt;/code&gt; case statements you see in other programming languages. A &lt;code&gt;match&lt;/code&gt; case is a clean way of writing multiple &lt;code&gt;if&lt;/code&gt; statement that use the same variable to check for some value. An example looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;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;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;30&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;age&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="p"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;11&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;"This is a prime"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="mi"&gt;13&lt;/span&gt;&lt;span class="o"&gt;..=&lt;/span&gt;&lt;span class="mi"&gt;19&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;"A teen"&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;"Just some 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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the &lt;code&gt;match&lt;/code&gt; statement, each value in the LHS of the &lt;code&gt;=&amp;gt;&lt;/code&gt; expression represents the cases of the &lt;code&gt;match&lt;/code&gt; statement, separated by commas. For instance, If age is 1, then we print "One!".&lt;/p&gt;

&lt;p&gt;We can chain multiple cases using the pipe &lt;code&gt;|&lt;/code&gt; expression if many values give the same output. You can also use a range of numbers as a case.&lt;/p&gt;

&lt;p&gt;In case none of the numbers match, we use underscore &lt;code&gt;_&lt;/code&gt; to define a default case.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;code&gt;match&lt;/code&gt; case with variable assignments
&lt;/h3&gt;

&lt;p&gt;Let's take a look at this program:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="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;res&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="k"&gt;match&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;..=&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Child"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="mi"&gt;11&lt;/span&gt;&lt;span class="o"&gt;..=&lt;/span&gt;&lt;span class="mi"&gt;18&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Teen"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="o"&gt;..=&lt;/span&gt;&lt;span class="mi"&gt;30&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Young Adult"&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="n"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Adult"&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;res&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 program prints out the group type based on age using &lt;code&gt;match&lt;/code&gt; case. But notice that in every case, I am assigning a string to the variable &lt;code&gt;res&lt;/code&gt;. You can see that it seems repetitive.&lt;/p&gt;

&lt;p&gt;Well there's a way to make it DRY. Checkout this 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="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="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;res&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="k"&gt;match&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;..=&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;"Child"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="mi"&gt;11&lt;/span&gt;&lt;span class="o"&gt;..=&lt;/span&gt;&lt;span class="mi"&gt;18&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;"Teen"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="o"&gt;..=&lt;/span&gt;&lt;span class="mi"&gt;30&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;"Young Adult"&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="s"&gt;"Adult"&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;res&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// prints "Young Adult"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All I did was declare the &lt;code&gt;res&lt;/code&gt; variable and assign it to the &lt;code&gt;match&lt;/code&gt; case statement based on the &lt;code&gt;age&lt;/code&gt; value.&lt;/p&gt;

&lt;p&gt;Seems pretty intuitive. Right?&lt;/p&gt;




&lt;h3&gt;
  
  
  Block statements and Block scope
&lt;/h3&gt;

&lt;p&gt;There are lot of syntactic sugar in Rust that you wouldn't see in other programming languages. Though all the programming languages has some kind of syntactic sugar, Rust has a unique way of handling them. Block statements are one of them.&lt;/p&gt;

&lt;p&gt;Block statements are pretty useful when you want to assign a value to some variable "once" (thus not needing a separate function) but requires some complex calculations to get that value.&lt;/p&gt;

&lt;p&gt;A major advantage of block statements is that any variable declared inside the block statement are scoped to that block only. This helps in keeping the program consistent and prevent the program from any kind of scope errors.&lt;/p&gt;

&lt;p&gt;Let's take an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;30&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;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;24&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;"Block Age: {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;age&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;"Global Age: {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;age&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;Running the code will give this output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;Block Age: 24
Global Age: 30
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You would expect that the &lt;code&gt;age&lt;/code&gt; would change to 24 outside the block due to shadowing, but that's not what happened.&lt;/p&gt;

&lt;p&gt;Due to block scope, the &lt;code&gt;age&lt;/code&gt; variable declared inside the block will not affect anything outside the block.&lt;/p&gt;

&lt;p&gt;Except, if you are mutating the &lt;code&gt;age&lt;/code&gt; variable. 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;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;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;24&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;"Block Age: {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;age&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;"Global Age: {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;age&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 shell"&gt;&lt;code&gt;Block Age: 54
Global Age: 54
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, yea. If the variable is being declared as mutable, then block statements can change the variables outside the block scope.&lt;/p&gt;

&lt;p&gt;Now let's get to an interesting part.&lt;/p&gt;




&lt;h3&gt;
  
  
  Assigning block statements to the variables.
&lt;/h3&gt;

&lt;p&gt;Let's write this 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;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;some_num&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;45&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;y&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;34&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;y&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;some_num&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this code:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;I declared a variable called &lt;code&gt;some_num&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Then I declared a block statement in which I declared two local variables &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt; and assigned some values.&lt;/li&gt;
&lt;li&gt;Then I add them &lt;code&gt;x + y&lt;/code&gt; and assign the sum to &lt;code&gt;some_num&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This will print out "79" in the console.&lt;/p&gt;

&lt;p&gt;Now, you might be confused with the &lt;code&gt;x + y&lt;/code&gt; term that seems out of place compared to the rest of code. Basically what's happening is that after I add &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt;, since there's no other code remaining, The block will return this value, i.e. &lt;code&gt;x + y&lt;/code&gt;, and assign it to &lt;code&gt;some_num&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Also, notice that I didn't put a semi-colon after &lt;code&gt;x + y&lt;/code&gt;. This is because if you used a semi-colon after &lt;code&gt;x + y&lt;/code&gt; then the value cannot get assigned to the variable and will return some empty block.&lt;/p&gt;




&lt;h3&gt;
  
  
  Some more loops!
&lt;/h3&gt;

&lt;p&gt;Let's get back to the loops! In the previous tutorial, I talked about the &lt;code&gt;loop&lt;/code&gt; expression, the simplest of the loops. Now let's learn the &lt;code&gt;for&lt;/code&gt; and &lt;code&gt;while&lt;/code&gt; loop.&lt;/p&gt;

&lt;p&gt;A &lt;code&gt;while&lt;/code&gt; loop in Rust looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;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;i&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="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;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A &lt;code&gt;for&lt;/code&gt; loop in Rust looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;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;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="mi"&gt;1&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;"{}"&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="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;Notice we have used a range in a &lt;code&gt;for&lt;/code&gt; loop.&lt;/p&gt;




&lt;h2&gt;
  
  
  Making a Better Guessing Game
&lt;/h2&gt;

&lt;p&gt;In the previous tutorial, we have made our guessing game. But you might have noticed that we can guess as many times as you want and the game wouldn't end.&lt;/p&gt;

&lt;p&gt;So let's implement a guess limiter to our game.&lt;/p&gt;

&lt;p&gt;To do that, we can simply replace the &lt;code&gt;loop&lt;/code&gt; expression with a &lt;code&gt;while&lt;/code&gt; loop expression. For our game, we can let the player have at maximum 5 guesses. &lt;/p&gt;

&lt;p&gt;Here's how the code will look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;io&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;rand&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Rng&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;secret_number&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;rand&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;thread_rng&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.gen_range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="mi"&gt;101&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;i&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Guess {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;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;"Guess the number between 1 and 100: "&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;user_input&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;span class="nn"&gt;io&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;stdin&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.read_line&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;user_input&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Some Input Error."&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;user_input&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;user_input&lt;/span&gt;&lt;span class="nf"&gt;.trim&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.parse&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Not an integer"&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;user_input&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;secret_number&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Too small!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;user_input&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;secret_number&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Too big!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;i&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;"You have lost! The number was {}!"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;secret_number&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"You guessed it! It's {}!"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;secret_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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The changes that I made here are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;I declared a mutable variable &lt;code&gt;i&lt;/code&gt; with initial value as 0.&lt;/li&gt;
&lt;li&gt;I replaced the &lt;code&gt;loop&lt;/code&gt; with a &lt;code&gt;while&lt;/code&gt; loop when i &amp;lt; 5 (5 guesses).&lt;/li&gt;
&lt;li&gt;I added a print statement to keep track of the guesses.&lt;/li&gt;
&lt;li&gt;Once the &lt;code&gt;while&lt;/code&gt; loop is over, whether normally or using &lt;code&gt;break&lt;/code&gt; statement, it will go through an &lt;code&gt;if&lt;/code&gt; block (outside the &lt;code&gt;while&lt;/code&gt; loop) to see if &lt;code&gt;i&lt;/code&gt; is equal to 5 or not and then set the print statements for each condition.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can now play the game and you will notice you will only have five guesses to guess the number.&lt;/p&gt;




&lt;p&gt;Now let's play the game, but this time let's input some string instead of an integer as our guess.&lt;/p&gt;

&lt;p&gt;Let's run the &lt;code&gt;cargo run&lt;/code&gt; command and see the output:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkj221kwavpqn6zmjzl0v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkj221kwavpqn6zmjzl0v.png" alt="Rust error output" width="800" height="232"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We get an error since we input a string and not an integer. We have to handle these errors in our code.&lt;/p&gt;

&lt;p&gt;Let's take a look at our code:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fconyy6r7b13xpdktf5yy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fconyy6r7b13xpdktf5yy.png" alt="Rust code analysis" width="800" height="507"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you observe, the line of code pointed by the arrow is where we are getting the error. We need to modify this line to handle our errors.&lt;/p&gt;

&lt;p&gt;What we can do is, if the user inputs a valid input, then it's all okay. But if the user inputs an invalid input, we can notify the user that it's an invalid input and then restart the loop.&lt;/p&gt;

&lt;p&gt;We can do this using match case statement for the &lt;code&gt;user_input&lt;/code&gt; variable.&lt;/p&gt;

&lt;p&gt;But what will be the variable which we will match the values to?&lt;/p&gt;

&lt;p&gt;That will be the &lt;code&gt;user_input.trim().parse()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We know that this function will either return a valid result or an error.&lt;/p&gt;

&lt;p&gt;We can verify the return value of the function by using these two enums:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Ok()&lt;/code&gt; to see if it's a valid result, and&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Err()&lt;/code&gt; to see if it's an error.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These two enums will be our cases for matching &lt;code&gt;user_input.trim().parse()&lt;/code&gt; and then assign that to the &lt;code&gt;user_input&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Let's modify that one line of code to 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;user_input&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="n"&gt;user_input&lt;/span&gt;&lt;span class="nf"&gt;.trim&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.parse&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nf"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&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;num&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="nf"&gt;Err&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="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="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;"Invalid Input! Try again!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
                &lt;span class="k"&gt;continue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this code, The first case is when the result is valid. We first check whether the &lt;code&gt;user_input.trim().parse()&lt;/code&gt; returns an integer, say &lt;code&gt;num&lt;/code&gt;. The &lt;code&gt;Ok(num)&lt;/code&gt; will verify if it's a valid result.&lt;/p&gt;

&lt;p&gt;The second case is when &lt;code&gt;user_input.trim().parse()&lt;/code&gt; returns an Error object. Since, we will check for any kind of errors, we use an underscore &lt;code&gt;_&lt;/code&gt; inside &lt;code&gt;Err()&lt;/code&gt; to verify for any errors.&lt;/p&gt;

&lt;p&gt;Also, notice that I removed &lt;code&gt;expect()&lt;/code&gt; function since we are already handling the errors in a better way.&lt;/p&gt;




&lt;p&gt;Now let's run the code and see the output:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fynktqmnq710m8vk81t41.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fynktqmnq710m8vk81t41.png" alt="Final Rust output" width="800" height="426"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can notice, I have input some invalid inputs to test the game and it works!&lt;/p&gt;

&lt;p&gt;Great job! You have made a better guessing game!&lt;/p&gt;




&lt;p&gt;I hope you have learnt a lot in this short tutorial. As I have said, I would have included this in the previous tutorial but that one was getting too long and hence, I decided to write a new one just for this.&lt;/p&gt;

&lt;p&gt;In the next tutorial, we will make a simple calculator using Rust.&lt;/p&gt;

&lt;p&gt;Until next time, have an awesome day ahead!&lt;/p&gt;

&lt;p&gt;GitHub Repo: &lt;a href="https://github.com/khairalanam/better-rust-guessing-game" rel="noopener noreferrer"&gt;https://github.com/khairalanam/better-rust-guessing-game&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you like whatever I write here, follow me on Devto and check out my socials:&lt;/p&gt;

&lt;p&gt;LinkedIn: &lt;a href="https://www.linkedin.com/in/khair-alanam-b27b69221/" rel="noopener noreferrer"&gt;https://www.linkedin.com/in/khair-alanam-b27b69221/&lt;/a&gt;&lt;br&gt;
Twitter: &lt;a href="https://www.twitter.com/khair_alanam" rel="noopener noreferrer"&gt;https://www.twitter.com/khair_alanam&lt;/a&gt;&lt;br&gt;
GitHub: &lt;a href="https://github.com/khairalanam" rel="noopener noreferrer"&gt;https://github.com/khairalanam&lt;/a&gt;&lt;/p&gt;

</description>
      <category>rust</category>
      <category>programming</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Rust Tutorial 2: Let's make a Guessing Game!</title>
      <dc:creator>Khair Alanam</dc:creator>
      <pubDate>Thu, 21 Sep 2023 07:07:36 +0000</pubDate>
      <link>https://dev.to/khair_al_anam/rust-tutorial-2-lets-make-a-guessing-game-2lm0</link>
      <guid>https://dev.to/khair_al_anam/rust-tutorial-2-lets-make-a-guessing-game-2lm0</guid>
      <description>&lt;p&gt;&lt;em&gt;Reading time: 20 minutes&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Welcome back to the Rust Tutorial series!&lt;/p&gt;

&lt;p&gt;In this tutorial, I will be programming a simple guessing game! On the way, we will learn basic concepts like variables, loops, control statements, etc.&lt;/p&gt;

&lt;p&gt;As promised in the previous tutorial, we will use Rust's built-in tool called Cargo to make our game.&lt;/p&gt;

&lt;p&gt;Also, I will be referring to some chapters from the official Rust documentation to get the basics done. However, I will be building projects in each tutorial based on the concepts learned.&lt;/p&gt;

&lt;p&gt;So let's get started!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2jrmz2qd5o6rclno5vvy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2jrmz2qd5o6rclno5vvy.png" alt="Rust programmer humor" width="800" height="553"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Building with Cargo
&lt;/h2&gt;

&lt;p&gt;To the web developers out there, Cargo is basically npm in Rust. It's a package manager as well as a build tool.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;To create our guessing game project, we have to run the below command:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;cargo new &amp;lt;Name of the project&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Let's name our project "guessing_game" and run:
&lt;/li&gt;
&lt;/ul&gt;

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Now open the &lt;code&gt;guessing_game&lt;/code&gt; folder in VS Code.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;On opening the folder, you will notice that the folder structure is dead simple. It's just a &lt;code&gt;src&lt;/code&gt; folder with a &lt;code&gt;main.rs&lt;/code&gt; file and a &lt;code&gt;Cargo.toml&lt;/code&gt; file.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;src
-&amp;gt; main.rs

Cargo.toml
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;main.rs&lt;/code&gt; file just contains a simple "Hello World" program. Cargo.toml is basically package.json in web development projects. It just lists out any dependencies and packages used in the project.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frshr2yovl62tfzyvzq80.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frshr2yovl62tfzyvzq80.png" alt="Content of Cargo.toml file" width="800" height="251"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Variables in Rust
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Like JavaScript, we use the &lt;code&gt;let&lt;/code&gt; keyword to declare a Rust variable. There is also a &lt;code&gt;const&lt;/code&gt; keyword, like JavaScript, but you will be using &lt;code&gt;let&lt;/code&gt; keyword for 99.9% of the time.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;a&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's declare some more variables and print them in our terminal. But how do you print them?&lt;/p&gt;

&lt;p&gt;Rust uses formatting using &lt;code&gt;{}&lt;/code&gt; to print the variables. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;a&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="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;b&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="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="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;"a is {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"b is {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"c is {}"&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="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"a, b, c are {}, {} and {} respectively"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;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;To execute the above program, run the command:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;You will get the output:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fue852w1gyuhrrrrkjl5s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fue852w1gyuhrrrrkjl5s.png" alt="Rust output" width="800" height="195"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You will also notice that after running the &lt;code&gt;cargo run&lt;/code&gt; command, we got extra files in our directory:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Cargo.lock&lt;/code&gt;: It's more or less similar to package-lock.json in web development projects.&lt;/li&gt;
&lt;li&gt;Then, we have the &lt;code&gt;target&lt;/code&gt; folder where the executable file is stored. If you open the &lt;code&gt;debug&lt;/code&gt; folder in the &lt;code&gt;target&lt;/code&gt; folder, you will see a lot of files related to the project build files plus the executable file.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the end, you don't have to worry about anything inside the &lt;code&gt;target&lt;/code&gt; folder. All you need to focus on is everything inside the &lt;code&gt;src&lt;/code&gt; folder.&lt;/p&gt;




&lt;h3&gt;
  
  
  Using different data and datatypes
&lt;/h3&gt;

&lt;p&gt;Rust is a strongly typed static programming language. It means that once the variable has data of some datatype like numbers, strings, etc., that variable can only store data of that datatype.&lt;/p&gt;

&lt;p&gt;In Rust, it's essential to declare the variable along with its datatype. However, Rust can infer the datatype from the data you've given for the variable.&lt;/p&gt;

&lt;p&gt;Let's get to know the different datatypes:&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;// integers&lt;/span&gt;

    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;u8&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="c1"&gt;// numbers from 0 - 255&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i8&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;34&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                              &lt;span class="c1"&gt;// numbers from -128 - 127&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;u16&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                            &lt;span class="c1"&gt;// numbers from 0 - 65535&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i16&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;20000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                          &lt;span class="c1"&gt;// numbers from -32768 - 32767&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;u32&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1000000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                         &lt;span class="c1"&gt;// numbers from 0 - 4294967295&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;5000000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                        &lt;span class="c1"&gt;// numbers from -2147483648 - 2147483647&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;u64&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10000000000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                     &lt;span class="c1"&gt;// numbers from 0 - 18446744073709551615&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i64&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;80000000000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                    &lt;span class="c1"&gt;// numbers from -9223372036854775808 - 9223372036854775807&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;u128&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1000000000000000000000000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;      &lt;span class="c1"&gt;// numbers from 0 - 340282366920938463463374607431768211455&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i128&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;9000000000000000000000000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;     &lt;span class="c1"&gt;// numbers from -170141183460469231731687303715884105728 - 170141183460469231731687303715884105727&lt;/span&gt;

    &lt;span class="c1"&gt;// floating-points&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;f32&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="c1"&gt;// 32-bit floating-point&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;f64&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;2.71828182845&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                   &lt;span class="c1"&gt;// 64-bit floating-point&lt;/span&gt;

    &lt;span class="c1"&gt;// boolean types&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;is_true&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;bool&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="c1"&gt;// Boolean type (true)&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;is_false&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;bool&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="c1"&gt;// Boolean type (false)&lt;/span&gt;

    &lt;span class="c1"&gt;// single character type&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;ch&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;char&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sc"&gt;'A'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                           &lt;span class="c1"&gt;// Character type&lt;/span&gt;

    &lt;span class="c1"&gt;// strings&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;str1&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, world!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;             &lt;span class="c1"&gt;// String slice&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;str2&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="c1"&gt;// String object&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;"u8: {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"i8: {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"u16: {}"&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="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"i16: {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;d&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;"u32: {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"i32: {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;f&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;"u64: {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"i64: {}"&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="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"u128: {}"&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="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"i128: {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;j&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;"f32: {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"f64: {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"bool (true): {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;is_true&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;"bool (false): {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;is_false&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;"char: {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ch&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;"&amp;amp;str: {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;str1&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;"String: {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;str2&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;h4&gt;
  
  
  Integers
&lt;/h4&gt;

&lt;p&gt;There are mainly two types of integer data types.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Unsigned Integers (Can represent only positive integers including 0)&lt;/li&gt;
&lt;li&gt;Signed Integers (Can represent both negative and positive integers)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Unsigned integer datatypes include: &lt;code&gt;u8&lt;/code&gt;, &lt;code&gt;u16&lt;/code&gt;, &lt;code&gt;u32&lt;/code&gt;, &lt;code&gt;u64&lt;/code&gt;, and &lt;code&gt;u128&lt;/code&gt;.&lt;br&gt;
Signed integer datatypes include: &lt;code&gt;i8&lt;/code&gt;, &lt;code&gt;i16&lt;/code&gt;, &lt;code&gt;i32&lt;/code&gt;, &lt;code&gt;i64&lt;/code&gt;, and &lt;code&gt;i128&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;I am not going to explain each data type from integers. But I will explain &lt;code&gt;u8&lt;/code&gt; and &lt;code&gt;i8&lt;/code&gt;, while the rest can be inferred from these two.&lt;/p&gt;

&lt;p&gt;Variables with the &lt;code&gt;u8&lt;/code&gt; type imply that such variables can store 2^8 = 256 unique positive integers from 0 to 255. Whereas for &lt;code&gt;i8&lt;/code&gt;, you can represent 256 unique integers like &lt;code&gt;u8&lt;/code&gt;, but we have negative integers too. Variables of the &lt;code&gt;i8&lt;/code&gt; type can store values from -128 to 127.&lt;/p&gt;

&lt;p&gt;Similarly, &lt;code&gt;u16&lt;/code&gt; and &lt;code&gt;i16&lt;/code&gt; can represent 2^16 = 65536 unique integers, 2^32 = 4294967296 unique integers for &lt;code&gt;u32&lt;/code&gt; and &lt;code&gt;i32&lt;/code&gt;, and so on.&lt;/p&gt;

&lt;p&gt;So, you may be wondering, which datatype should I choose for integers?&lt;/p&gt;

&lt;p&gt;The answer is &lt;code&gt;i32&lt;/code&gt;. Most of the time, when dealing with integers, they would be in the range of &lt;code&gt;i32&lt;/code&gt;. Even Rust considers the default integer type as &lt;code&gt;i32&lt;/code&gt; when inferring from integer data.&lt;/p&gt;


&lt;h4&gt;
  
  
  Floating-points
&lt;/h4&gt;

&lt;p&gt;There are only two datatypes; &lt;code&gt;f32&lt;/code&gt; and &lt;code&gt;f64&lt;/code&gt;. You can represent any decimal number using floating points.&lt;/p&gt;

&lt;p&gt;As for which datatype to use, use &lt;code&gt;f64&lt;/code&gt;. It's also the default floating-point datatype.&lt;/p&gt;


&lt;h4&gt;
  
  
  Booleans
&lt;/h4&gt;

&lt;p&gt;Booleans represent the values &lt;code&gt;true&lt;/code&gt; and &lt;code&gt;false&lt;/code&gt; and are represented by the &lt;code&gt;bool&lt;/code&gt; keyword.&lt;/p&gt;


&lt;h4&gt;
  
  
  Character types
&lt;/h4&gt;

&lt;p&gt;The &lt;code&gt;char&lt;/code&gt; datatype represents any single character enclosed in single or double quotation marks. For example, &lt;code&gt;'a'&lt;/code&gt;, &lt;code&gt;'w'&lt;/code&gt;, &lt;code&gt;"1"&lt;/code&gt;, &lt;code&gt;"?"&lt;/code&gt;, etc. Variables with a &lt;code&gt;char&lt;/code&gt; type can store exactly one character and not more or less than that. Storing more or less than one character for the &lt;code&gt;char&lt;/code&gt; type will throw an error.&lt;/p&gt;


&lt;h4&gt;
  
  
  Strings
&lt;/h4&gt;

&lt;p&gt;This can get a bit tricky because both &lt;code&gt;&amp;amp;str&lt;/code&gt; and &lt;code&gt;String&lt;/code&gt; are related to each other and very similar. I will differentiate them here:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&amp;amp;str&lt;/th&gt;
&lt;th&gt;String&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Cannot change size&lt;/td&gt;
&lt;td&gt;Can change size&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Used for read-only operations&lt;/td&gt;
&lt;td&gt;Used for both read and write operations&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Variables of this type are string slices of some existing string.&lt;/td&gt;
&lt;td&gt;Variables of this type are actually string objects and can be manipulated by any string operations.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Anyways, which one to use? Just use &lt;code&gt;String&lt;/code&gt;. But remember, unlike other datatypes, it's important to declare a string using &lt;code&gt;String::new()&lt;/code&gt; like the code snippet below along with the &lt;code&gt;String&lt;/code&gt; datatype:&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;hello&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;span class="s"&gt;"Hello World!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;code&gt;String&lt;/code&gt; in &lt;code&gt;String::new()&lt;/code&gt; is a library, and the &lt;code&gt;new()&lt;/code&gt; is the function to declare a string object. As for the double colons &lt;code&gt;::&lt;/code&gt;, it's similar to double-colon notation in C++.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh48s0cvzs7b5f2cfdo0d.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh48s0cvzs7b5f2cfdo0d.jpg" alt="Rust meme" width="512" height="512"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  Control flow statements
&lt;/h3&gt;

&lt;p&gt;The if-else condition statements in Rust are just as identical as those of other programming languages.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;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;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;15&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;a&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"It's 10"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;a&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="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"It's 20"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;15&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;"It's 15"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Some 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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice that when mentioning conditions, you don't need to enclose the condition in brackets &lt;code&gt;()&lt;/code&gt; similar to Python.&lt;/p&gt;




&lt;h3&gt;
  
  
  Loops
&lt;/h3&gt;

&lt;p&gt;Surprisingly, there are five types of loops in Rust!&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;loop&lt;/code&gt; expression&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;while&lt;/code&gt; expression&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;while let&lt;/code&gt; expression&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;for&lt;/code&gt; expression&lt;/li&gt;
&lt;li&gt;Labeled block expression&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We will know these loops in the coming tutorials while building some projects. But for this guessing game, let's learn the &lt;code&gt;loop&lt;/code&gt; expression.&lt;/p&gt;

&lt;p&gt;As the name suggests, a &lt;code&gt;loop&lt;/code&gt; expression is a block that loops the execution of the statements inside the &lt;code&gt;loop&lt;/code&gt; indefinitely.&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;loop&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello world"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's the syntax. Look how simple it is! This program will print "Hello world" for eternity :)&lt;/p&gt;

&lt;p&gt;You can break out of the loop by using a &lt;code&gt;break&lt;/code&gt; statement.&lt;/p&gt;

&lt;p&gt;Now for practice, let's write the code given 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;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;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;loop&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;a&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;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello world"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;a&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="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, try to compile without executing the code by running the below command:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;When you run the command, you will get the below error:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs5xl9l7fopd2jjg4siec.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs5xl9l7fopd2jjg4siec.png" alt="Rust immutable variable error" width="800" height="287"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is because Rust variables are immutable by default, despite using the &lt;code&gt;let&lt;/code&gt; keyword. To make it mutable, use the &lt;code&gt;mut&lt;/code&gt; keyword before the variable declaration 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="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you can run the code using &lt;code&gt;cargo run&lt;/code&gt;, and you'll see "Hello world" printed five times.&lt;/p&gt;

&lt;p&gt;A quick tip: You can change the value of the variable without the &lt;code&gt;mut&lt;/code&gt; keyword just by re-declaring the same variable with the &lt;code&gt;let&lt;/code&gt; keyword and then assigning the new value. This concept is called "Shadowing" in Rust.&lt;/p&gt;

&lt;p&gt;You can also use the same variable to assign a new value with a different datatype by shadowing. We will see this concept when we are making our guessing game.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// 'a' is of integer type&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;a&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// prints 4&lt;/span&gt;

    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;f64&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;4.5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// 'a' becomes floating point here&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;a&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// prints 4.5&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Let's make the Guessing Game!
&lt;/h2&gt;

&lt;p&gt;So, let's get started!&lt;/p&gt;

&lt;p&gt;First off, we need a secret number for our guess. Let's declare 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="n"&gt;secret_number&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;34&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, we need user input to guess. But how?&lt;/p&gt;

&lt;p&gt;For that, we need to use a library called &lt;code&gt;std::io&lt;/code&gt;. We can import any library in Rust using the &lt;code&gt;use&lt;/code&gt; keyword.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;io&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;secret_number&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;34&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 get the user input, we need a variable to store it first. So let's declare a variable called &lt;code&gt;user_input&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;io&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;secret_number&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;34&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;user_input&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;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Wait, why is the user input a string and not a number?&lt;/p&gt;

&lt;p&gt;Well, it's because, by default, all the inputs coming from the user are stored as strings, including numbers. Hence, we declare a string variable. But there's a way to convert a string input to an input of type integer or float as long as the string is a number.&lt;/p&gt;

&lt;p&gt;Now, here's the confusing part to get the input from the user:&lt;/p&gt;

&lt;p&gt;After you have declared a string variable &lt;code&gt;user_input&lt;/code&gt;, go to the next line.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Then, type &lt;code&gt;io&lt;/code&gt; to use the &lt;code&gt;std::io&lt;/code&gt; library.&lt;/li&gt;
&lt;li&gt;Then, attach the &lt;code&gt;stdin()&lt;/code&gt; function using double colon notation so that Rust can access the input device.&lt;/li&gt;
&lt;li&gt;Then, attach the &lt;code&gt;read_line()&lt;/code&gt; function using simple dot notation to read the input from the user.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;read_line()&lt;/code&gt; accepts the "reference" of the variable in which you are going to store the user input, i.e., &lt;code&gt;user_input&lt;/code&gt;. The reference of the variable can be taken using the &lt;code&gt;&amp;amp;&lt;/code&gt; notation. As for why we are using &lt;code&gt;&amp;amp;&lt;/code&gt;, it's part of the memory management in Rust which I will cover in a later tutorial. For now, just follow the tutorial.&lt;/li&gt;
&lt;li&gt;And finally, it could be possible that some error could come due to some erroneous input. Therefore, we have to finally attach one more function called the &lt;code&gt;expect()&lt;/code&gt; function to handle such errors.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;expect()&lt;/code&gt; function accepts a string parameter to print some error message.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;After all this confusing stuff, your final code should look 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;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;secret_number&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;34&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;user_input&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;span class="nn"&gt;io&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;stdin&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.read_line&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;user_input&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Some Input Error."&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;It's confusing but will become easier once it's used frequently.&lt;/p&gt;

&lt;p&gt;Also, if you are using VS Code, you may get an error in the third line stating that it has to use the &lt;code&gt;mut&lt;/code&gt; keyword. That's because when it comes to inputs, they have to be mutable. So you must use the &lt;code&gt;mut&lt;/code&gt; keyword in both the declaration and the read_line function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;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;secret_number&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;34&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;user_input&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;span class="nn"&gt;io&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;stdin&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.read_line&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;user_input&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Some Input Error."&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;
  
  
  Let's use randomness!
&lt;/h3&gt;

&lt;p&gt;Notice that our secret number is set to 34. But, we want a random number every time we start playing a new game. To generate random numbers, we have to import a new library called &lt;code&gt;rand&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;However, this library is not built into Rust and has to be installed in our project.&lt;/p&gt;

&lt;p&gt;To install &lt;code&gt;rand&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Open &lt;code&gt;Cargo.toml&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Under the &lt;code&gt;[dependencies]&lt;/code&gt; tab, type this:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight toml"&gt;&lt;code&gt;&lt;span class="py"&gt;rand&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"0.8.5"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Basically, we are adding a new dependency called &lt;code&gt;rand&lt;/code&gt; which as of writing now, is currently in version 0.8.5.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F38tt1syvpvyntbj6y8hp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F38tt1syvpvyntbj6y8hp.png" alt="Cargo.toml file" width="800" height="258"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Then finally, run the &lt;code&gt;cargo build&lt;/code&gt; command.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;After this, you will have &lt;code&gt;rand&lt;/code&gt; installed.&lt;/p&gt;

&lt;p&gt;Next, let's import &lt;code&gt;rand&lt;/code&gt; using the &lt;code&gt;use&lt;/code&gt; keyword. Plus, we will be specifically importing the &lt;code&gt;Rng&lt;/code&gt; trait to generate random numbers.&lt;/p&gt;

&lt;p&gt;What are traits? They are basically abstract classes in Rust. I will discuss these in a later tutorial.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;io&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;rand&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Rng&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;secret_number&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;34&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;"Guess the number between 1 and 100: "&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;user_input&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;span class="nn"&gt;io&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;stdin&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.read_line&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;user_input&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Some Input Error."&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, let's remove the &lt;code&gt;secret_number&lt;/code&gt; value and replace it with a random number. To do that, we will have to type a long line just like we did to get the user input:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Type &lt;code&gt;rand&lt;/code&gt; in the &lt;code&gt;secret_number&lt;/code&gt; expression.&lt;/li&gt;
&lt;li&gt;Then using double colon notation, attach the &lt;code&gt;thread_rng()&lt;/code&gt; function.&lt;/li&gt;
&lt;li&gt;Then using dot notation, attach the &lt;code&gt;gen_range()&lt;/code&gt; function.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;gen_range&lt;/code&gt; accepts a range of values in which a random number can be selected.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Before I finish adding the code for random number generation, it's important to know that Rust has a weird but unique way of declaring ranges of numbers.&lt;/p&gt;




&lt;p&gt;In Python, you may have used the &lt;code&gt;range()&lt;/code&gt; function to generate a range of numbers. Something like &lt;code&gt;range(1, 101)&lt;/code&gt; to generate all the numbers between 1 and 101 (101 is not included).&lt;/p&gt;

&lt;p&gt;But in Rust, you use this unique "double dot" notation to generate a range of numbers.&lt;br&gt;
For example, to generate numbers between 1 to 10. You have to write like this: &lt;code&gt;1..11&lt;/code&gt;.&lt;br&gt;
The number at the right end is exclusive, hence you will have numbers from 1 upto and including 10.&lt;/p&gt;

&lt;p&gt;For 1 to 100, it would be &lt;code&gt;1..101&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;But let's say you want to include the right end as well. For example, you want a range of numbers between 1 to 6 with 6 included. Then you can write the range as &lt;code&gt;1..=6&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For 1 to 100, you can write it as &lt;code&gt;1..=100&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now, let's pick up where we have left off.&lt;/p&gt;



&lt;p&gt;So remember that our &lt;code&gt;gen_range()&lt;/code&gt; function needs a range of numbers as a parameter. And we need the user to guess a number between 1 to 100. So, we will have &lt;code&gt;1..101&lt;/code&gt; or &lt;code&gt;1..=100&lt;/code&gt; as our range.&lt;/p&gt;

&lt;p&gt;So the final code for our random number generation looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;io&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;rand&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Rng&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;secret_number&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;rand&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;thread_rng&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.gen_range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="mi"&gt;101&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;"Guess the number between 1 and 100: "&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;user_input&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;span class="nn"&gt;io&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;stdin&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.read_line&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;user_input&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Some Input Error."&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;
  
  
  Comparing the input guess and the secret number
&lt;/h3&gt;

&lt;p&gt;Now it's time to see if the user input and the secret number are the same. To do that, we can set some "if" conditions to see if the given input is greater than, less than, or equal to the secret number.&lt;/p&gt;

&lt;p&gt;Here's the code for the conditional block:&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;if&lt;/span&gt; &lt;span class="n"&gt;user_input&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;secret_number&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Too small!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;user_input&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;secret_number&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Too big!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"You guessed it!"&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 our current code will look 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;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;io&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;rand&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Rng&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;secret_number&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;rand&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;thread_rng&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.gen_range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="mi"&gt;101&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;"Guess the number between 1 and 100: "&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;user_input&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;span class="nn"&gt;io&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;stdin&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.read_line&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;user_input&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Some Input Error."&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;user_input&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;secret_number&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Too small!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;user_input&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;secret_number&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Too big!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"You guessed it!"&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;Now, you will probably get the error regarding the mismatched types in the conditional block. That's because the &lt;code&gt;user_input&lt;/code&gt; is a string while the &lt;code&gt;secret_number&lt;/code&gt; is an integer.&lt;/p&gt;

&lt;p&gt;To fix that, we will have to parse our &lt;code&gt;user_input&lt;/code&gt; into an integer. Here's how:&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;user_input&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;user_input&lt;/span&gt;&lt;span class="nf"&gt;.trim&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.parse&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Not an integer"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let me explain:&lt;/p&gt;

&lt;p&gt;Here, we re-declare the &lt;code&gt;user_input&lt;/code&gt; variable to be of integer type by the concept of shadowing. Then to get rid of any trailing spaces, we use the &lt;code&gt;trim()&lt;/code&gt; function. Then we use the &lt;code&gt;parse()&lt;/code&gt; function to parse the input (which is of string type) to any datatype we want to convert to, which is &lt;code&gt;i32&lt;/code&gt; in this case. Then in case of any error, we use the &lt;code&gt;expect()&lt;/code&gt; function with a custom error message.&lt;/p&gt;

&lt;p&gt;Our code will now look 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;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;io&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;rand&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Rng&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;secret_number&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;rand&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;thread_rng&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.gen_range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="mi"&gt;101&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;"Guess the number between 1 and 100: "&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;user_input&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;span class="nn"&gt;io&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;stdin&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.read_line&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;user_input&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Some Input Error."&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;user_input&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;user_input&lt;/span&gt;&lt;span class="nf"&gt;.trim&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.parse&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Not an integer"&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;user_input&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;secret_number&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Too small!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;user_input&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;secret_number&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Too big!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"You guessed it!"&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;You can now run this program and play the guessing game!&lt;/p&gt;

&lt;p&gt;However, you will notice that the game only plays once and then stops.&lt;/p&gt;

&lt;p&gt;So, we need to run this program until we guess the secret number.&lt;/p&gt;




&lt;h3&gt;
  
  
  Loop the program
&lt;/h3&gt;

&lt;p&gt;All we need to do here is to add a &lt;code&gt;loop&lt;/code&gt; expression with a condition to break out of the loop. The loop has to break once we guess the number right.&lt;/p&gt;

&lt;p&gt;Just wrap the guessing logic under a &lt;code&gt;loop&lt;/code&gt; expression and then modify the &lt;code&gt;else&lt;/code&gt; clause to include a &lt;code&gt;break&lt;/code&gt; statement.&lt;/p&gt;

&lt;p&gt;Here's the final code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;io&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;rand&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Rng&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;secret_number&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;rand&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;thread_rng&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.gen_range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="mi"&gt;101&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;loop&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;"Guess the number between 1 and 100: "&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;user_input&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;span class="nn"&gt;io&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;stdin&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.read_line&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;user_input&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Some Input Error."&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;user_input&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;user_input&lt;/span&gt;&lt;span class="nf"&gt;.trim&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.parse&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Not an integer"&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;user_input&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;secret_number&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Too small!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;user_input&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;secret_number&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Too big!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"You guessed it! It's {}!"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;secret_number&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="k"&gt;break&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;Now run the code using &lt;code&gt;cargo run&lt;/code&gt; and you are good to go!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpcsi2prn2nwg60p5gita.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpcsi2prn2nwg60p5gita.png" alt="Guessing Game output" width="800" height="328"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;Congratulations! You have made a really solid program that covers the major Rust basics including variables, datatypes, user input, loops, etc.&lt;/p&gt;

&lt;p&gt;For the next tutorial, we will be improving this guessing game to handle errors as well as learn some new concepts on the way. I could cover this here but since this tutorial is getting long, I think it's better to keep it as a separate short tutorial.&lt;/p&gt;

&lt;p&gt;Anyways, I hope you have learned a lot in this tutorial!&lt;/p&gt;

&lt;p&gt;Have an awesome day ahead!&lt;/p&gt;

&lt;p&gt;GitHub Repo: &lt;a href="https://github.com/khairalanam/rust-guessing-game" rel="noopener noreferrer"&gt;https://github.com/khairalanam/rust-guessing-game&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you like whatever I write here, follow me on Devto and check out my socials:&lt;/p&gt;

&lt;p&gt;LinkedIn: &lt;a href="https://www.linkedin.com/in/khair-alanam-b27b69221/" rel="noopener noreferrer"&gt;https://www.linkedin.com/in/khair-alanam-b27b69221/&lt;/a&gt;&lt;br&gt;
Twitter: &lt;a href="https://www.twitter.com/khair_alanam" rel="noopener noreferrer"&gt;https://www.twitter.com/khair_alanam&lt;/a&gt;&lt;br&gt;
GitHub: &lt;a href="https://github.com/khairalanam" rel="noopener noreferrer"&gt;https://github.com/khairalanam&lt;/a&gt;&lt;/p&gt;

</description>
      <category>programming</category>
      <category>rust</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Rust Tutorial 1: Writing Hello World</title>
      <dc:creator>Khair Alanam</dc:creator>
      <pubDate>Thu, 14 Sep 2023 15:05:34 +0000</pubDate>
      <link>https://dev.to/khair_al_anam/rust-tutorial-1-writing-hello-world-4gd9</link>
      <guid>https://dev.to/khair_al_anam/rust-tutorial-1-writing-hello-world-4gd9</guid>
      <description>&lt;p&gt;&lt;em&gt;Reading time: 3 minutes&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Hey everyone! Recently I started learning the Rust programming language for a personal side project of mine. But there aren't many resources available apart from the official documentation. So I decided I might as well share the stuff I have learned in Rust.&lt;/p&gt;

&lt;p&gt;So without further ado, let's learn Rust!&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%2Fweu1kcc006j47db2oyxn.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%2Fweu1kcc006j47db2oyxn.PNG" alt="Rust in Google Images"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  How is Rust different?
&lt;/h2&gt;

&lt;p&gt;Rust is a low-level programming language like C. It's a compiled language like C. But unlike C, Rust is memory-safe (No segfaults nightmares). And unlike many languages, Rust doesn't use a garbage collector. This "best of both worlds"-feature makes Rust insanely fast and efficient.&lt;/p&gt;

&lt;p&gt;The main factor that makes it different is its memory management. I will cover this in a later tutorial. But to give you a brief idea, Rust language works on ownership and borrowing concepts of memory. In other words, when data are being passed on to different variables, the data don't get copied to all these variables. Instead, the one that currently has the data will have ownership of it while the other variables "lose" ownership of it.&lt;/p&gt;

&lt;p&gt;Now let's get to the interesting part.&lt;/p&gt;




&lt;h2&gt;
  
  
  Writing your first program in Rust
&lt;/h2&gt;

&lt;p&gt;Before writing your first program in Rust, let's install Rust!&lt;/p&gt;




&lt;h3&gt;
  
  
  Installing Rust
&lt;/h3&gt;

&lt;p&gt;The installation differs for each platform. But I will be installing Rust for Windows.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Go to &lt;a href="https://www.rust-lang.org/tools/install" rel="noopener noreferrer"&gt;this link&lt;/a&gt; to install Rust for Windows.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click on the button that downloads the rustup installer. Mine is Windows 10 64-bit. So I will download a 64-bit installer.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Go to the folder where you downloaded the installer and open it.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You will get the below screen:&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%2Fhsr0wxutn399w7h0lmjc.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%2Fhsr0wxutn399w7h0lmjc.png" alt="rustup installer screen"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Type "1" and then press Enter. Wait until everything is installed. Also, Rust requires Visual Studio C++ Build Tools, which will be prompted by the installer for you to install these tools.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Once everything is done, you will get the below image which implies Rust is finally installed. Press Enter.&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%2F8qb0rmxgh2wny38uwgzo.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%2F8qb0rmxgh2wny38uwgzo.PNG" alt="Rust is installed"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  Setup Rust in VS Code
&lt;/h3&gt;

&lt;p&gt;After the installation, create a new folder and open it in VS Code.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Go to Extensions. Search for "rust-analyzer" and install it.&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%2Fhoni8kdk7gq2ife7cyc2.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%2Fhoni8kdk7gq2ife7cyc2.png" alt="Rust analyzer"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Also, search for "CodeLLDB" and install it.&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%2Ffzfrivi9sxr5hf51hvwr.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%2Ffzfrivi9sxr5hf51hvwr.png" alt="CodeLLDB"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  Writing Rust code
&lt;/h3&gt;

&lt;p&gt;Every program in Rust ends with the file extension &lt;code&gt;.rs&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Create a file and name it &lt;code&gt;main.rs&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Like C, C++, and Java, Rust runs your main program inside a main function.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Now, write your program.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&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="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello World!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


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

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;Now, run the below command on your terminal to compile the Rust code.&lt;/li&gt;
&lt;/ul&gt;

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

rustc main.rs


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

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;After running this command, you will see some files including an executable file.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Now finally, run the below command to see the output.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

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


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

&lt;/div&gt;

&lt;p&gt;This will run the &lt;code&gt;main.exe&lt;/code&gt; file and print "Hello World" in your terminal.&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%2Fgy4eelmue9xxrb594u58.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%2Fgy4eelmue9xxrb594u58.png" alt="Hello World in Rust"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;Great job! You have written in your first program in Rust!&lt;/p&gt;

&lt;p&gt;Plus, you have also learned how to print texts in the terminal using &lt;code&gt;println!()&lt;/code&gt; as well as initializing a function using the &lt;code&gt;fn&lt;/code&gt; keyword.&lt;/p&gt;

&lt;p&gt;In the next tutorial, we will be using Rust's in-built package manager called Cargo to make simple Rust programs and build Rust projects.&lt;/p&gt;

&lt;p&gt;Have a great day!&lt;/p&gt;

&lt;p&gt;GitHub Repo: &lt;a href="https://github.com/khairalanam/rust-tutorial" rel="noopener noreferrer"&gt;https://github.com/khairalanam/rust-tutorial&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you like whatever I write here, follow me on Devto and check out my socials:&lt;/p&gt;

&lt;p&gt;LinkedIn: &lt;a href="https://www.linkedin.com/in/khair-alanam-b27b69221/" rel="noopener noreferrer"&gt;https://www.linkedin.com/in/khair-alanam-b27b69221/&lt;/a&gt;&lt;br&gt;
Twitter: &lt;a href="https://www.twitter.com/khair_alanam" rel="noopener noreferrer"&gt;https://www.twitter.com/khair_alanam&lt;/a&gt;&lt;br&gt;
GitHub: &lt;a href="https://github.com/khairalanam" rel="noopener noreferrer"&gt;https://github.com/khairalanam&lt;/a&gt;&lt;/p&gt;

</description>
      <category>programming</category>
      <category>rust</category>
    </item>
    <item>
      <title>What are some things that one has to be ready with before starting a side project?</title>
      <dc:creator>Khair Alanam</dc:creator>
      <pubDate>Sat, 19 Aug 2023 15:56:29 +0000</pubDate>
      <link>https://dev.to/khair_al_anam/what-are-some-things-that-one-has-to-be-ready-with-before-starting-a-side-project-2e20</link>
      <guid>https://dev.to/khair_al_anam/what-are-some-things-that-one-has-to-be-ready-with-before-starting-a-side-project-2e20</guid>
      <description>&lt;p&gt;I am currently building a &lt;a href="https://github.com/khairalanam/GPRG" rel="noopener noreferrer"&gt;simple readme generator&lt;/a&gt; website as one of my personal projects. By almost at the end of the project, I realized I could have done some testing before every component is coded. And also realized I could have automated some workflows.&lt;/p&gt;

&lt;p&gt;Sometimes I feel they are unnecessary and are just a waste of time if the project is small. But at the same time, these are some of the most underrated skills of a professional developer.&lt;/p&gt;

&lt;p&gt;Anyways, what are your thoughts on this? And what are some things that a developer has to keep note of before or during the building of the side project?&lt;/p&gt;

</description>
      <category>discuss</category>
      <category>programming</category>
    </item>
    <item>
      <title>Would you rather choose Rust or Golang?</title>
      <dc:creator>Khair Alanam</dc:creator>
      <pubDate>Wed, 09 Aug 2023 09:41:11 +0000</pubDate>
      <link>https://dev.to/khair_al_anam/would-you-rather-choose-rust-or-golang-385k</link>
      <guid>https://dev.to/khair_al_anam/would-you-rather-choose-rust-or-golang-385k</guid>
      <description>&lt;p&gt;When it comes to programming languages, I am proficient in Python and JavaScript. However, I wanted to learn a language that deals with low-level stuff and thought of learning Rust. So far, I have enjoyed a lot about Rust including the concept of ownership, borrowing, structs and impls etc. I am learning Rust since I wanted to use it for a side project.&lt;/p&gt;

&lt;p&gt;What are your opinions? Rust or Golang? I heard Golang is pretty good option when it comes to usage and the number of jobs that ask for Golang.&lt;/p&gt;

</description>
      <category>discuss</category>
      <category>programming</category>
    </item>
    <item>
      <title>@Developers What's your before and after tech stack?</title>
      <dc:creator>Khair Alanam</dc:creator>
      <pubDate>Wed, 26 Jul 2023 03:20:05 +0000</pubDate>
      <link>https://dev.to/khair_al_anam/developers-whats-your-before-and-after-tech-stack-20i1</link>
      <guid>https://dev.to/khair_al_anam/developers-whats-your-before-and-after-tech-stack-20i1</guid>
      <description>&lt;p&gt;A couple months ago, I learnt MERN stack, and built 1-2 projects with it. But later I thought of trying out Django as I heard Django is such a strong backend framework. I am still learning it but I really love how Django is powerful when it comes to having all batteries included despite being complex.&lt;/p&gt;

</description>
      <category>discuss</category>
      <category>webdev</category>
      <category>devdiscuss</category>
    </item>
    <item>
      <title>What is the easiest way to do auth?</title>
      <dc:creator>Khair Alanam</dc:creator>
      <pubDate>Sat, 15 Jul 2023 13:35:37 +0000</pubDate>
      <link>https://dev.to/khair_al_anam/what-is-the-easiest-way-to-do-auth-1a7f</link>
      <guid>https://dev.to/khair_al_anam/what-is-the-easiest-way-to-do-auth-1a7f</guid>
      <description>&lt;p&gt;I am learning fullstack development but I always get stuck with authentication. Do you know of any frameworks/ways that makes auth super easy to implement?&lt;/p&gt;

</description>
      <category>discuss</category>
      <category>security</category>
    </item>
    <item>
      <title>Top 10 Obscure HTML tags You may not Know</title>
      <dc:creator>Khair Alanam</dc:creator>
      <pubDate>Fri, 10 Mar 2023 16:33:15 +0000</pubDate>
      <link>https://dev.to/khair_al_anam/top-10-obscure-html-tags-you-may-not-know-e9f</link>
      <guid>https://dev.to/khair_al_anam/top-10-obscure-html-tags-you-may-not-know-e9f</guid>
      <description>&lt;p&gt;&lt;em&gt;Reading time: 6 minutes&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Whether you are a professional web developer, a seasoned one, a learner, or even someone who doesn't know web development, everyone knows that at the heart of every website is a simple HTML page or a set of HTML pages.&lt;/p&gt;

&lt;p&gt;In fact, one starts diving into the world of web development by starting with HTML and its semantic tags. In fact, HTML5 is the final version of the HTML and is considered a W3C recommendation.&lt;/p&gt;

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

&lt;p&gt;When it comes to HTML tags, there are so many to learn, that many of us skip and move on to other web technologies. While this is considered totally A-Okay, it's interesting to know that some HTML tags are so obscure that they are rarely used and not even heard of.&lt;/p&gt;

&lt;p&gt;So, without further ado, let me share with you the ten HTML tags you may not know and are not heard of. However, many tags are less used apart from these and if you are interested, you can learn about them in &lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTML" rel="noopener noreferrer"&gt;MDN Web Docs&lt;/a&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  1. &lt;code&gt;&amp;lt;wbr&amp;gt;&lt;/code&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Lorem ipsum &lt;span class="nt"&gt;&amp;lt;wbr&amp;gt;&lt;/span&gt;dolor&lt;span class="nt"&gt;&amp;lt;/wbr&amp;gt;&lt;/span&gt; sit amet 
consectetur adipisicing elit. Molestiae, eveniet.&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Lorem ipsum dolor sit amet consectetur 
adipisicing elit. Minus, corporis.&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;As per MDN Web Docs:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The &lt;code&gt;&amp;lt;wbr&amp;gt;&lt;/code&gt; HTML element represents a word break opportunity—a position within text where the browser may optionally break a line, though its line-breaking rules would not otherwise create a break at that location.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To explain this in simple words, the text wrapped in a &lt;code&gt;&amp;lt;wbr&amp;gt;&lt;/code&gt; tag will not get broken by those annoying hyphens when reducing the width of the screen.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;&amp;lt;wbr&amp;gt;&lt;/code&gt; tag is supported in all the major browsers except IE (But who cares about IE?).&lt;/p&gt;




&lt;h2&gt;
  
  
  2. &lt;code&gt;&amp;lt;var&amp;gt;&lt;/code&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;
    The volume of a box is &lt;span class="nt"&gt;&amp;lt;var&amp;gt;&lt;/span&gt;l&lt;span class="nt"&gt;&amp;lt;/var&amp;gt;&lt;/span&gt; × &lt;span class="nt"&gt;&amp;lt;var&amp;gt;&lt;/span&gt;w&lt;span class="nt"&gt;&amp;lt;/var&amp;gt;&lt;/span&gt; ×
    &lt;span class="nt"&gt;&amp;lt;var&amp;gt;&lt;/span&gt;h&lt;span class="nt"&gt;&amp;lt;/var&amp;gt;&lt;/span&gt;, where &lt;span class="nt"&gt;&amp;lt;var&amp;gt;&lt;/span&gt;l&lt;span class="nt"&gt;&amp;lt;/var&amp;gt;&lt;/span&gt; represents the length
    &lt;span class="nt"&gt;&amp;lt;var&amp;gt;&lt;/span&gt;w&lt;span class="nt"&gt;&amp;lt;/var&amp;gt;&lt;/span&gt; the width and &lt;span class="nt"&gt;&amp;lt;var&amp;gt;&lt;/span&gt;h&lt;span class="nt"&gt;&amp;lt;/var&amp;gt;&lt;/span&gt; the height of the box.
&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;

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

&lt;/div&gt;



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

&lt;p&gt;The &lt;code&gt;&amp;lt;var&amp;gt;&lt;/code&gt; tag helps in emphasizing variables in a mathematical expression by &lt;em&gt;italicizing&lt;/em&gt; the variables (As you usually see in Math Textbooks). They are mainly used for semantic purposes.&lt;/p&gt;

&lt;p&gt;Fortunately, this tag is supported in all browsers including IE (Again, who cares about IE?).&lt;/p&gt;




&lt;h2&gt;
  
  
  3. &lt;code&gt;&amp;lt;u&amp;gt;&lt;/code&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;You could use this element to highlight
&lt;span class="nt"&gt;&amp;lt;u&amp;gt;&lt;/span&gt;speling&lt;span class="nt"&gt;&amp;lt;/u&amp;gt;&lt;/span&gt; mistakes, so the writer can 
&lt;span class="nt"&gt;&amp;lt;u&amp;gt;&lt;/span&gt;corect&lt;span class="nt"&gt;&amp;lt;/u&amp;gt;&lt;/span&gt; them.&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;The &lt;code&gt;&amp;lt;u&amp;gt;&lt;/code&gt; tag is used to annotate text in HTML using some underlined decoration. This tag can be useful to mark incorrect text and other types of annotations.&lt;/p&gt;

&lt;p&gt;Many of us including me know this &lt;code&gt;&amp;lt;u&amp;gt;&lt;/code&gt; tag to give underline to the text. However, this has been deprecated in HTML4 and is replaced with the current &lt;code&gt;&amp;lt;u&amp;gt;&lt;/code&gt; tag used for annotations.&lt;/p&gt;




&lt;h2&gt;
  
  
  4. &lt;code&gt;&amp;lt;dfn&amp;gt;&lt;/code&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;A &lt;span class="nt"&gt;&amp;lt;dfn&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"def-validator"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;validator&lt;span class="nt"&gt;&amp;lt;/dfn&amp;gt;&lt;/span&gt;
is a program that checks for syntax 
errors in code or documents.&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;A &lt;code&gt;&amp;lt;dfn&amp;gt;&lt;/code&gt; tag is used to indicate a word to be defined within a paragraph or text. This tag looks for the parent tag like a &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt; or a &lt;code&gt;&amp;lt;section&amp;gt;&lt;/code&gt; and considers all the text inside it to be the word's definition, again for semantic purposes.&lt;/p&gt;




&lt;h2&gt;
  
  
  5. &lt;code&gt;&amp;lt;cite&amp;gt;&lt;/code&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;figure&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;blockquote&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;It was a bright cold day in April, and the clocks were striking thirteen.&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/blockquote&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;figcaption&amp;gt;&lt;/span&gt;First sentence in &lt;span class="nt"&gt;&amp;lt;cite&amp;gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"http://www.george-orwell.org/1984/0.html"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Nineteen Eighty-Four&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&amp;lt;/cite&amp;gt;&lt;/span&gt; by George Orwell (Part 1, Chapter 1).&lt;span class="nt"&gt;&amp;lt;/figcaption&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/figure&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;The &lt;code&gt;&amp;lt;cite&amp;gt;&lt;/code&gt; tag is used to indicate a cited work or article in a block of text. This is almost the same as &lt;code&gt;&amp;lt;a&amp;gt;&lt;/code&gt; tag except for semantic purposes.&lt;/p&gt;




&lt;h2&gt;
  
  
  6. &lt;code&gt;&amp;lt;ins&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;del&amp;gt;&lt;/code&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;“You're late!”&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;del&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;“I apologize for the delay.”&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/del&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;ins&lt;/span&gt; &lt;span class="na"&gt;cite=&lt;/span&gt;&lt;span class="s"&gt;"../howtobeawizard.html"&lt;/span&gt; &lt;span class="na"&gt;datetime=&lt;/span&gt;&lt;span class="s"&gt;"2018-05"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;“A wizard is never late …”&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/ins&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;So... technically it's 11 tags instead of 10 :) But whatever. I wanted to put the two tags together because they both do the same thing but with different purposes.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;&amp;lt;ins&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;del&amp;gt;&lt;/code&gt; tags are the tags that are used to indicate the insertion of text and deletion of text respectively in a given block of text.&lt;/p&gt;

&lt;p&gt;Think of these as the same kind used in the GitHub commits where you can see the insertion and deletion of lines of code in a file.&lt;/p&gt;




&lt;h2&gt;
  
  
  7. &lt;code&gt;&amp;lt;noscript&amp;gt;&lt;/code&gt; tag
&lt;/h2&gt;

&lt;p&gt;Now, if you have any experience using React, then you must have come across the &lt;code&gt;&amp;lt;noscript&amp;gt;&lt;/code&gt; tag when going through the index.html page created by the CRA tool. However, many of us including me don't know the exact reason for this. You may even think about why a &lt;code&gt;&amp;lt;noscript&amp;gt;&lt;/code&gt; tag is used when you have a &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; tag.&lt;/p&gt;

&lt;p&gt;To give you a clear idea, the &lt;code&gt;&amp;lt;noscript&amp;gt;&lt;/code&gt; tag is used to define a section of the HTML page that contains the alternative text for those browsers that don't support scripting. That is, when scripting is enabled, the content inside &lt;code&gt;&amp;lt;noscript&amp;gt;&lt;/code&gt; tag is hidden and vice versa.&lt;/p&gt;




&lt;h2&gt;
  
  
  8. &lt;code&gt;&amp;lt;pre&amp;gt;&lt;/code&gt; tag
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;pre&amp;gt;&lt;/span&gt;
  L          TE
    A       A
      C    V
       R A
       DOU
       LOU
      REUSE
      QUE TU
      PORTES
    ET QUI T'
    ORNE O CI
     VILISÉ
    OTE-  TU VEUX
     LA    BIEN
    SI      RESPI
            RER       - Apollinaire
&lt;span class="nt"&gt;&amp;lt;/pre&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;Did you know? HTML5 by default ignores some extra spacing, blank lines, and new lines when you are typing out a text block inside any HTML tag.&lt;/p&gt;

&lt;p&gt;However, with the &lt;code&gt;&amp;lt;pre&amp;gt;&lt;/code&gt; tag, the text including all the spacing and blank lines is exactly formatted when rendered on the web. In other words, all the space and blank lines are considered as typed out when a &lt;code&gt;&amp;lt;pre&amp;gt;&lt;/code&gt; tag is used.&lt;/p&gt;




&lt;h2&gt;
  
  
  9. &lt;code&gt;&amp;lt;q&amp;gt;&lt;/code&gt; tag
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;When Dave asks HAL to open the pod bay door, HAL answers: &lt;span class="nt"&gt;&amp;lt;q&lt;/span&gt; &lt;span class="na"&gt;cite=&lt;/span&gt;&lt;span class="s"&gt;"https://www.imdb.com/title/tt0062622/quotes/qt0396921"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;I'm sorry, Dave. I'm afraid I can't do that.&lt;span class="nt"&gt;&amp;lt;/q&amp;gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;The &lt;code&gt;&amp;lt;q&amp;gt;&lt;/code&gt; tag is similar to the &lt;code&gt;&amp;lt;em&amp;gt;&lt;/code&gt; tag in the sense that they emphasize the inline text. The &lt;code&gt;&amp;lt;q&amp;gt;&lt;/code&gt; tag is used to emphasize a small quote in a given paragraph or a block of text.&lt;/p&gt;




&lt;h2&gt;
  
  
  10. &lt;code&gt;&amp;lt;ruby&amp;gt;&lt;/code&gt; tag
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;ruby&amp;gt;&lt;/span&gt;
明日 &lt;span class="nt"&gt;&amp;lt;rp&amp;gt;&lt;/span&gt;(&lt;span class="nt"&gt;&amp;lt;/rp&amp;gt;&amp;lt;rt&amp;gt;&lt;/span&gt;Ashita&lt;span class="nt"&gt;&amp;lt;/rt&amp;gt;&amp;lt;rp&amp;gt;&lt;/span&gt;)&lt;span class="nt"&gt;&amp;lt;/rp&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/ruby&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;&lt;code&gt;&amp;lt;ruby&amp;gt;&lt;/code&gt; tag? Now that's a very weird name for an HTML tag.&lt;/p&gt;

&lt;p&gt;Initially, I thought it may have something to do with the Ruby language but I was way out of my assumptions.&lt;/p&gt;

&lt;p&gt;In fact, the &lt;code&gt;&amp;lt;ruby&amp;gt;&lt;/code&gt; tag is used to annotate small text rendered above or below some text, and this tag is mainly used for text written in East Asian languages like Japanese, Chinese, etc to denote the meaning and/or pronunciation of the characters.&lt;/p&gt;

&lt;p&gt;Fun fact: the term "ruby" refers to a unit of measurement used in typesetters for newspapers to print very small but legible text.&lt;/p&gt;







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

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

&lt;p&gt;So, there you have it! These are, in my opinion, some of the obscure HTML5 tags that are rarely heard of. Apart from these, there are many more that you can check out in &lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTML" rel="noopener noreferrer"&gt;MDN Web Docs&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Also, I would like to tell you that you don't have to know any of these at all. Just stick with the most commonly used tags and you are good to go and become an awesome web developer!&lt;/p&gt;

&lt;p&gt;If you like my content, you can check out my socials:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.linkedin.com/in/khair-alanam-b27b69221/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;br&gt;
&lt;a href="https://twitter.com/khair_alanam" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>php</category>
      <category>help</category>
      <category>discuss</category>
    </item>
    <item>
      <title>5 YouTube Channels I follow for Web Development (in no particular order)</title>
      <dc:creator>Khair Alanam</dc:creator>
      <pubDate>Fri, 04 Nov 2022 17:08:20 +0000</pubDate>
      <link>https://dev.to/khair_al_anam/5-youtube-channels-i-follow-for-web-development-in-no-particular-order-3abl</link>
      <guid>https://dev.to/khair_al_anam/5-youtube-channels-i-follow-for-web-development-in-no-particular-order-3abl</guid>
      <description>&lt;p&gt;&lt;em&gt;Reading time: 5 minutes&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;As someone still learning web development (and will never stop learning), I have come across a bajillion of web development content across YouTube. Some are good, some are fine, and some are meh.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr3m8tdc07r5z25hsteb4.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr3m8tdc07r5z25hsteb4.jpg" alt="tutorial hell meme" width="506" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, with all the web development content I have seen (and with practice), I have seen some channels that have helped me in my web development journey. I still follow these channels for my ongoing learning. I have found them to be helpful in many unique ways.&lt;/p&gt;

&lt;p&gt;So without further ado, Here are 5 YouTube channels that I follow for web development in no particular order. All these channels are best in their own right, and I have found them immensely helpful in my web development journey.&lt;br&gt;
 &lt;/p&gt;
&lt;h2&gt;
  
  
  1. FreeCodeCamp
&lt;/h2&gt;

&lt;p&gt; &lt;/p&gt;

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

&lt;p&gt;This one needs no introduction. I can't stress how much this gold mine of educational content has helped me in my learning and still does it to this day. FreeCodeCamp has revolutionalized tech education.&lt;/p&gt;

&lt;p&gt;FreeCodeCamp is a non-profit organization started by Quincy Larson to make learning web development (and software development in general) accessible to everyone, and it's all for free!&lt;/p&gt;

&lt;p&gt;You can find a lot of tutorials covering various development fields like Web development, Machine learning, Game development, DevOps, Mobile app development, etc., in FreeCodeCamp.&lt;/p&gt;

&lt;p&gt;FreeCodeCamp also provides tutorials covering all the major programming languages like C++, Java, Python, JavaScript, Kotlin, etc. Plus, they update their tutorials to keep up with the trends.&lt;br&gt;
 &lt;/p&gt;
&lt;h2&gt;
  
  
  2. Traversy Media
&lt;/h2&gt;

&lt;p&gt; &lt;/p&gt;

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

&lt;p&gt;Traversy Media is a great channel for web developers and those learning web development. It is a youtube channel started by Brad Traversy that provides web development tutorials and other videos that are useful for a developer that wants to make their careers fruitful.&lt;/p&gt;

&lt;p&gt;What I like about his channel is that in his tutorials (including other awesome developers' tutorials), the technologies and concepts are taught concisely without being overwhelmed by the complexity of that particular subject. &lt;/p&gt;

&lt;p&gt;Plus, I love his videos on web development guides as I would get to know which technologies are relevant then and prepare myself to learn and apply them.&lt;/p&gt;

&lt;p&gt;I recommend this channel to every web developer and those learning web development.&lt;br&gt;
 &lt;/p&gt;
&lt;h2&gt;
  
  
  3. Web Dev Simplified
&lt;/h2&gt;

&lt;p&gt; &lt;/p&gt;

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

&lt;p&gt;Web Dev Simplified is another great channel mainly focused on web development, specifically the front end. If there's one thing  Web Dev Simplified stands out from others, it's that this channel teaches every concept within minutes, with clarity and conciseness.&lt;/p&gt;

&lt;p&gt;Personally speaking, I found Kyle's (the one behind Web Dev Simplified) CSS Flexbox and Grid videos to be one of the best tutorials on YouTube that talks about Flexbox and Grid. Some of his JavaScript tutorials were also really helpful, especially as a revision of whatever I have learned in modern JavaScript.&lt;br&gt;
 &lt;/p&gt;
&lt;h2&gt;
  
  
  4. Kevin Powell
&lt;/h2&gt;

&lt;p&gt; &lt;/p&gt;

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

&lt;p&gt;Kevin Powell's channel is the one that made me fall in love with CSS. Many things in CSS are underused, some are ahead of their time, and some are just unique. Anyways, to get to the point, I realized the power of CSS because of this great channel.&lt;/p&gt;

&lt;p&gt;Very well known as the "King of CSS," Kevin Powell's YouTube channel is a channel that's heavily focused on CSS and its efficient use in making well-optimized websites. Despite its only focus on CSS, I found the channel to be great for web development learners.&lt;/p&gt;

&lt;p&gt;While learning CSS, I found this channel to be particularly useful in one of the most important aspects of web development; responsiveness. And because of him, I enjoyed making my website projects responsive and well-optimized.&lt;/p&gt;

&lt;p&gt;Outside his YouTube channel, he has a course called "Conquering Responsive Layouts" on his official website which is, in my opinion, the best course on responsive layouts. I would highly recommend every web developer check it out. From this course, I learned the use of em, rem, ch, vw, vh, and other units from Kevin Powell and much useful information on responsiveness.&lt;/p&gt;

&lt;p&gt;I highly recommend every web developer and learner check out his channel.&lt;br&gt;
 &lt;/p&gt;
&lt;h2&gt;
  
  
  5. The Net Ninja
&lt;/h2&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/98BzS5Oz5E4"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;The four channels that I have mentioned earlier are well-known in the web development community. But for some reason, this channel is not very well-known. Let me tell you, this channel is just too good.&lt;/p&gt;

&lt;p&gt;To begin with, The Net Ninja is a YouTube channel that is heavily focused on tutorials in the field of full-stack development. Going through many of his playlists on JavaScript and React, I have one question only: "How are these tutorials free of cost?!?!"&lt;/p&gt;

&lt;p&gt;The tutorials in The Net Ninja are well structured and well thought-out in playlists for all kinds of learners to understand every concept and technology explained in these tutorials. Shaun (The one behind The Net Ninja) is such an awesome instructor and knows very well how to make his students understand everything in his tutorials.&lt;/p&gt;

&lt;p&gt;I learned Modern JavaScript, Asynchronous Programming with JavaScript, and Modern React from The Net Ninja. For all these playlists, I give them 11/10 because they are just so good and well-structured.&lt;/p&gt;

&lt;p&gt;I recommend everyone The Net Ninja channel if you are learning web development.&lt;br&gt;
 &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5hp0rvb34fx1ggxhr22r.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5hp0rvb34fx1ggxhr22r.jpg" alt="Responsiveness testing meme" width="600" height="449"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And that's pretty much it! Those are the 5 YouTube channels I follow for web development. &lt;/p&gt;

&lt;p&gt;All these channels are the best in their right. Besides, these are not the only best channels out there. So if you got any good channels for learning web development, comment below!&lt;/p&gt;

&lt;p&gt;You can follow me on:&lt;br&gt;
Twitter: &lt;a href="https://twitter.com/khair_alanam" rel="noopener noreferrer"&gt;https://twitter.com/khair_alanam&lt;/a&gt;&lt;br&gt;
LinkedIn: &lt;a href="https://www.linkedin.com/in/khair-alanam-b27b69221/" rel="noopener noreferrer"&gt;https://www.linkedin.com/in/khair-alanam-b27b69221/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>programming</category>
    </item>
    <item>
      <title>How to host your website on GitHub Pages in 5 minutes?</title>
      <dc:creator>Khair Alanam</dc:creator>
      <pubDate>Fri, 07 Oct 2022 09:35:25 +0000</pubDate>
      <link>https://dev.to/khair_al_anam/how-to-host-your-website-on-github-pages-in-5-minutes-3e6h</link>
      <guid>https://dev.to/khair_al_anam/how-to-host-your-website-on-github-pages-in-5-minutes-3e6h</guid>
      <description>&lt;p&gt;&lt;em&gt;Reading time: 2 minutes&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Now, you have finally made your website using HTML, CSS and JavaScript, but now you need to host it on some platform for free. So, where will you want to host the website of yours?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1x87mrxfbq3ms4t0art0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1x87mrxfbq3ms4t0art0.png" alt="meme on servers" width="480" height="336"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There are many options to try out web hosting; &lt;a href="https://www.netlify.com/" rel="noopener noreferrer"&gt;Netlify&lt;/a&gt;, &lt;a href="https://vercel.com/" rel="noopener noreferrer"&gt;Vercel&lt;/a&gt;, &lt;a href="https://aws.amazon.com/s3/" rel="noopener noreferrer"&gt;Amazon S3&lt;/a&gt; etc.&lt;/p&gt;

&lt;p&gt;But here, in this article, we will be hosting our website on something called &lt;a href="https://pages.github.com/" rel="noopener noreferrer"&gt;GitHub Pages&lt;/a&gt; in just 5 minutes.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is GitHub Pages?
&lt;/h3&gt;

&lt;p&gt;GitHub Pages is a static site hosting service by GitHub which takes the necessary files from your git repository (HTML, CSS and JavaScript files), builds them, and publishes the website for you.&lt;/p&gt;

&lt;p&gt;You can use a custom domain along with the GitHub Pages to host your website without that "github.io" sub-domain.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fovbvyrkrwa0w8u1yx5lu.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fovbvyrkrwa0w8u1yx5lu.PNG" alt="github pages website" width="800" height="366"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  How to GitHub Pages?
&lt;/h3&gt;

&lt;p&gt;So, let's start!&lt;/p&gt;

&lt;p&gt;I have made a sample website that just shows "Hello World" and a button which when clicked will generate some sentences.&lt;/p&gt;

&lt;p&gt;This is how it looks like:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxn6sbpou7gz47gy2hxln.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxn6sbpou7gz47gy2hxln.PNG" alt="website showing hello world" width="800" height="367"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;All the files will be present in a GitHub repository which I have linked to, at the end of this article.&lt;/p&gt;

&lt;p&gt;Now, onto the steps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Push all your files to a GitHub repository.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4gbo8mgksfnsszldfx9j.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4gbo8mgksfnsszldfx9j.PNG" alt="github repo" width="800" height="361"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Go to Settings.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyg04c6s03xe6sawxsook.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyg04c6s03xe6sawxsook.PNG" alt="github repo settings page" width="800" height="361"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Scroll down until you see &lt;strong&gt;Pages&lt;/strong&gt; tab on the left and click on it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz8zcsy6t1n78yzrfzucr.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz8zcsy6t1n78yzrfzucr.PNG" alt="github pages section" width="800" height="361"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Under the &lt;strong&gt;Branch&lt;/strong&gt; sub-heading, click on the tab that shows &lt;code&gt;None&lt;/code&gt; and then select &lt;code&gt;main&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpawrlhxzzf1bnubt7zpo.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpawrlhxzzf1bnubt7zpo.PNG" alt="selecting The branch in github pages section" width="800" height="359"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdtvwx14rwv6iqbqtsaob.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdtvwx14rwv6iqbqtsaob.PNG" alt="selecting the main branch in github pages section" width="800" height="360"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy9xdkwp82qqox0dp55j3.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy9xdkwp82qqox0dp55j3.PNG" alt="showing the main branch" width="800" height="361"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Click &lt;strong&gt;Save&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8l106s3ye2ihtrrmbwwv.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8l106s3ye2ihtrrmbwwv.PNG" alt="clicking save to publish the website" width="800" height="361"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The GitHub Pages source is saved and now, you can wait for sometime until your website is published.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F21qz2q5kzkfe0rsfr66q.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F21qz2q5kzkfe0rsfr66q.PNG" alt="github pages source saved" width="800" height="361"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Try reloading the current page 2-3 times until you get the below page.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fiy6plyn15nhxzhepffcb.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fiy6plyn15nhxzhepffcb.PNG" alt="reloading the page" width="800" height="360"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Your website is ready! Now you can click &lt;code&gt;Visit Site&lt;/code&gt; to see your website!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Facsy3e2fi6g5hel8ruot.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Facsy3e2fi6g5hel8ruot.jpg" alt="commenting out code meme" width="800" height="718"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Congratulations! You have hosted your website on GitHub Pages in 5 minutes!&lt;/p&gt;

&lt;p&gt;I hope you have learnt something new and as always, Happy Learning!&lt;/p&gt;

&lt;p&gt;If you would like to follow me, you can follow me on &lt;a href="https://www.linkedin.com/in/khair-alanam-b27b69221/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Here's the GitHub repository: &lt;a href="https://github.com/khairalanam/Hello-World" rel="noopener noreferrer"&gt;https://github.com/khairalanam/Hello-World&lt;/a&gt;&lt;/p&gt;

</description>
      <category>github</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
