<?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: Anshuman Sathua</title>
    <description>The latest articles on DEV Community by Anshuman Sathua (@anshu21).</description>
    <link>https://dev.to/anshu21</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%2F773230%2F7f644cac-25d4-40f2-beaa-4f3204773cf3.jpeg</url>
      <title>DEV Community: Anshuman Sathua</title>
      <link>https://dev.to/anshu21</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/anshu21"/>
    <language>en</language>
    <item>
      <title>Programming fundamentals in Rust - Part 1 : Variables and Mutability</title>
      <dc:creator>Anshuman Sathua</dc:creator>
      <pubDate>Fri, 09 Feb 2024 18:13:40 +0000</pubDate>
      <link>https://dev.to/anshu21/programming-fundamentals-in-rust-part-1-variables-and-mutability-cce</link>
      <guid>https://dev.to/anshu21/programming-fundamentals-in-rust-part-1-variables-and-mutability-cce</guid>
      <description>&lt;p&gt;By default, Rust embraces immutability for variables as a deliberate choice. This serves as one of Rust's guiding principles, encouraging developers to write code that capitalizes on the safety and seamless concurrency that the language offers. Nevertheless, Rust provides the flexibility to make variables mutable, offering developers the choice to opt out of this default behavior. Let's delve into how and why Rust encourages immutability, and when you might consider opting for mutability.&lt;/p&gt;

&lt;p&gt;In Rust, when a variable is immutable, once a value is bound to a name, you cannot alter that value. To illustrate this concept, let's create a new project named "variables" in our projects directory using &lt;code&gt;cargo new variables&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now, navigate to the newly created "variables" directory, open the &lt;code&gt;src/main.rs&lt;/code&gt; file, and replace its existing code with the following snippet, which won't compile just yet:&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;x&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;"The value of x is: {x}"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// This line will cause a compilation error&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;"The value of x is: {x}"&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;Save your changes and execute the program using &lt;code&gt;cargo run&lt;/code&gt;. Expect to encounter an error message indicating an immutability violation, as illustrated in the following output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="err"&gt;$&lt;/span&gt; &lt;span class="n"&gt;cargo&lt;/span&gt; &lt;span class="n"&gt;run&lt;/span&gt;
   &lt;span class="n"&gt;Compiling&lt;/span&gt; &lt;span class="n"&gt;variables&lt;/span&gt; &lt;span class="n"&gt;v0&lt;/span&gt;&lt;span class="na"&gt;.1.0&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="cd"&gt;///projects/variables)&lt;/span&gt;
&lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;E0384&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt; &lt;span class="n"&gt;cannot&lt;/span&gt; &lt;span class="n"&gt;assign&lt;/span&gt; &lt;span class="n"&gt;twice&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;immutable&lt;/span&gt; &lt;span class="n"&gt;variable&lt;/span&gt; &lt;span class="err"&gt;`&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="err"&gt;`&lt;/span&gt;
 &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;src&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="py"&gt;.rs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;
  &lt;span class="p"&gt;|&lt;/span&gt;
&lt;span class="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;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;|&lt;/span&gt;         &lt;span class="o"&gt;-&lt;/span&gt;
  &lt;span class="p"&gt;|&lt;/span&gt;         &lt;span class="p"&gt;|&lt;/span&gt;
  &lt;span class="p"&gt;|&lt;/span&gt;         &lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="n"&gt;assignment&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="err"&gt;`&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="err"&gt;`&lt;/span&gt;
  &lt;span class="p"&gt;|&lt;/span&gt;         &lt;span class="n"&gt;help&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;consider&lt;/span&gt; &lt;span class="n"&gt;making&lt;/span&gt; &lt;span class="n"&gt;this&lt;/span&gt; &lt;span class="n"&gt;binding&lt;/span&gt; &lt;span class="n"&gt;mutable&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="err"&gt;`&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="err"&gt;`&lt;/span&gt;
&lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt;     &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The value of x is: {x}"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt;     &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;|&lt;/span&gt;     &lt;span class="o"&gt;^^^^^&lt;/span&gt; &lt;span class="n"&gt;cannot&lt;/span&gt; &lt;span class="n"&gt;assign&lt;/span&gt; &lt;span class="n"&gt;twice&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;immutable&lt;/span&gt; &lt;span class="n"&gt;variable&lt;/span&gt;

&lt;span class="n"&gt;For&lt;/span&gt; &lt;span class="n"&gt;more&lt;/span&gt; &lt;span class="n"&gt;information&lt;/span&gt; &lt;span class="n"&gt;about&lt;/span&gt; &lt;span class="n"&gt;this&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="err"&gt;`&lt;/span&gt;&lt;span class="n"&gt;rustc&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="n"&gt;explain&lt;/span&gt; &lt;span class="n"&gt;E0384&lt;/span&gt;&lt;span class="err"&gt;`&lt;/span&gt;&lt;span class="py"&gt;.
error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;could&lt;/span&gt; &lt;span class="n"&gt;not&lt;/span&gt; &lt;span class="n"&gt;compile&lt;/span&gt; &lt;span class="err"&gt;`&lt;/span&gt;&lt;span class="n"&gt;variables&lt;/span&gt;&lt;span class="err"&gt;`&lt;/span&gt; &lt;span class="n"&gt;due&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;previous&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This example highlights the compiler's role in pinpointing errors within your programs. While compiler errors may seem vexing, they essentially signify that your program is not yet securely achieving its intended functionality.&lt;/p&gt;

&lt;p&gt;The error message &lt;code&gt;cannot assign twice to immutable variable x&lt;/code&gt; was triggered because an attempt was made to assign a second value to the immutable variable x.&lt;/p&gt;

&lt;p&gt;This compile-time error is crucial as it prevents scenarios where unintentional value changes can lead to bugs. When one part of your code assumes that a value remains constant, and another part modifies that value, it introduces the potential for unexpected behavior. Identifying the source of such bugs after the fact can be challenging, particularly when the second piece of code alters the value selectively.&lt;/p&gt;

&lt;p&gt;Rust's compiler provides assurance that if you declare a value as immutable, it genuinely remains unchanged. This eliminates the need for manual tracking, making your code more predictable and easier to comprehend.&lt;/p&gt;

&lt;p&gt;However, embracing mutability can prove highly beneficial, enhancing the convenience of code composition. Despite the default immutability of variables, you have the flexibility to render them mutable by prefixing the variable name with &lt;code&gt;mut&lt;/code&gt;. This not only alters the variable's behavior but also serves as a clear indication to future code readers that other segments of the codebase will modify the value associated with this variable.&lt;/p&gt;

&lt;p&gt;For example, let’s change src/main.rs to the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;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;x&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;"The value of x is: {x}"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;6&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;"The value of x is: {x}"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When we run the program now, we get 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="err"&gt;$&lt;/span&gt; &lt;span class="n"&gt;cargo&lt;/span&gt; &lt;span class="n"&gt;run&lt;/span&gt;
   &lt;span class="n"&gt;Compiling&lt;/span&gt; &lt;span class="n"&gt;variables&lt;/span&gt; &lt;span class="n"&gt;v0&lt;/span&gt;&lt;span class="na"&gt;.1.0&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="cd"&gt;///projects/variables)&lt;/span&gt;
    &lt;span class="n"&gt;Finished&lt;/span&gt; &lt;span class="n"&gt;dev&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;unoptimized&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;debuginfo&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="nf"&gt;target&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="mf"&gt;0.30&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;
     &lt;span class="n"&gt;Running&lt;/span&gt; &lt;span class="err"&gt;`&lt;/span&gt;&lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;debug&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;variables&lt;/span&gt;&lt;span class="err"&gt;`&lt;/span&gt;
&lt;span class="n"&gt;The&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="n"&gt;of&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;
&lt;span class="n"&gt;The&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="n"&gt;of&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The introduction of &lt;code&gt;mut&lt;/code&gt; grants us the liberty to alter the value assigned to &lt;code&gt;x&lt;/code&gt; from 5 to 6. The choice of whether to embrace mutability or not rests entirely on your discretion and hinges on what you deem most lucid in that specific scenario.&lt;/p&gt;

&lt;h2&gt;
  
  
  Constants
&lt;/h2&gt;

&lt;p&gt;Similar to immutable variables, constants represent values tied to a name and are strictly off-limits when it comes to alterations. However, there are a few key differences between constants and variables.&lt;/p&gt;

&lt;p&gt;Firstly, constants are inherently immutable — you can't use mut with them. Unlike variables declared with the &lt;code&gt;let&lt;/code&gt; keyword, constants use &lt;code&gt;const&lt;/code&gt;, and you must annotate the type of the value. Don't worry too much about the details of types and annotations for now, we'll dive into that in the next section, called "Data Types," where we'll get a more comprehensive understanding. Just keep in mind that type annotations are always required for constants.&lt;/p&gt;

&lt;p&gt;Another thing to note is that constants can be declared in any scope, including the global scope. This makes them useful for values that many parts of the code need to know about.&lt;/p&gt;

&lt;p&gt;The last difference is that constants may only be set to a constant expression, not the result of a value that could only be computed at runtime.&lt;/p&gt;

&lt;p&gt;We'll explore these concepts further as we progress. For now, let's look at an example of a constant declaration:&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;const&lt;/span&gt; &lt;span class="n"&gt;THREE_HOURS_IN_SECONDS&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;60&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;60&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The constant is named &lt;code&gt;THREE_HOURS_IN_SECONDS&lt;/code&gt;, and its value is determined by multiplying 60 (the number of seconds in a minute) by 60 (the number of minutes in an hour) by 3 (representing the desired count of hours in this program). Rust follows a naming convention for constants, utilizing all uppercase letters with underscores between words. The compiler can evaluate a restricted set of operations during compile time, allowing us to express this value in a more comprehensible and verifiable manner, as opposed to directly setting it to the numeric result, such as 10,800.&lt;/p&gt;

&lt;p&gt;Constants remain valid throughout the entire program runtime, confined to the scope in which they were declared. This enduring nature makes constants particularly advantageous for values within your application domain that multiple program sections might need to reference, like the maximum points a player can earn in a game or the speed of light.&lt;/p&gt;

&lt;p&gt;Employing constants for hardcoded values across your program enhances code clarity for future maintainers and streamlines future updates. By centralizing such values as constants, you establish a single, easily locatable point for any necessary modifications in the future.&lt;/p&gt;

&lt;h2&gt;
  
  
  Shadowing
&lt;/h2&gt;

&lt;p&gt;In Rust, we have the flexibility to declare a new variable with the same name as a previous one. Rust developers often refer to this as "shadowing," where the first variable is overshadowed by the second. This implies that, when we use the variable name, the compiler recognizes the second variable. Essentially, the second variable supersedes the first, capturing any references to the variable name until either it gets shadowed itself or the current scope concludes. To shadow a variable, we can simply reuse the same variable name and employ the let keyword again, as illustrated 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;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="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;"The value of x in the inner scope is: {x}"&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;"The value of x is: {x}"&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 initially binds the variable &lt;code&gt;x&lt;/code&gt; to a value of 5. Subsequently, it introduces a new variable &lt;code&gt;x&lt;/code&gt; by reusing the &lt;code&gt;let x =&lt;/code&gt; construct, taking the original value and incrementing it by 1, resulting in &lt;code&gt;x&lt;/code&gt; being assigned a new value of 6. Then, within an inner scope defined by curly brackets, the third &lt;code&gt;let&lt;/code&gt; statement further shadows &lt;code&gt;x&lt;/code&gt;, creating yet another variable. This time, the value is obtained by multiplying the previous value by 2, making &lt;code&gt;x&lt;/code&gt; assume a value of 12. Upon the conclusion of this inner scope, the inner shadowing ceases, and &lt;code&gt;x&lt;/code&gt; reverts to being 6. Upon executing this program, the output will be as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="err"&gt;$&lt;/span&gt; &lt;span class="n"&gt;cargo&lt;/span&gt; &lt;span class="n"&gt;run&lt;/span&gt;
   &lt;span class="n"&gt;Compiling&lt;/span&gt; &lt;span class="n"&gt;variables&lt;/span&gt; &lt;span class="n"&gt;v0&lt;/span&gt;&lt;span class="na"&gt;.1.0&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="cd"&gt;///projects/variables)&lt;/span&gt;
    &lt;span class="n"&gt;Finished&lt;/span&gt; &lt;span class="n"&gt;dev&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;unoptimized&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;debuginfo&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="nf"&gt;target&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="mf"&gt;0.31&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;
     &lt;span class="n"&gt;Running&lt;/span&gt; &lt;span class="err"&gt;`&lt;/span&gt;&lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;debug&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;variables&lt;/span&gt;&lt;span class="err"&gt;`&lt;/span&gt;
&lt;span class="n"&gt;The&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="n"&gt;of&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;inner&lt;/span&gt; &lt;span class="n"&gt;scope&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;
&lt;span class="n"&gt;The&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="n"&gt;of&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Shadowing differs from marking a variable as mutable (&lt;code&gt;mut&lt;/code&gt;) because attempting to reassign a value without using the &lt;code&gt;let&lt;/code&gt; keyword results in a compile-time error. The use of &lt;code&gt;let&lt;/code&gt; allows us to apply various transformations to a value while maintaining immutability after those transformations are completed.&lt;/p&gt;

&lt;p&gt;Another distinction between &lt;code&gt;mut&lt;/code&gt; and shadowing lies in the fact that, with shadowing, a new variable is effectively created when the &lt;code&gt;let&lt;/code&gt; keyword is employed again. This provides the flexibility to change the type of the value while reusing the same variable name. For instance, consider a scenario where our program prompts a user to specify the desired number of spaces between some text using space characters, and subsequently, we aim to store that input as a number:&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;spaces&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"   "&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;spaces&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;spaces&lt;/span&gt;&lt;span class="nf"&gt;.len&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The initial &lt;code&gt;spaces&lt;/code&gt; variable is of type &lt;code&gt;string&lt;/code&gt;, while the subsequent &lt;code&gt;spaces&lt;/code&gt; variable is of type &lt;code&gt;number&lt;/code&gt;. Shadowing offers the advantage of avoiding the need for distinct names, such as &lt;code&gt;spaces_str&lt;/code&gt; and &lt;code&gt;spaces_num&lt;/code&gt;, instead, we can reuse the more straightforward &lt;code&gt;spaces&lt;/code&gt; name. However, if we attempt to use &lt;code&gt;mut&lt;/code&gt; for this purpose, as illustrated here, a compile-time error will be triggered:&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;spaces&lt;/span&gt; &lt;span class="o"&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;spaces&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;spaces&lt;/span&gt;&lt;span class="nf"&gt;.len&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The error says we’re not allowed to mutate a variable’s type:&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="err"&gt;$&lt;/span&gt; &lt;span class="n"&gt;cargo&lt;/span&gt; &lt;span class="n"&gt;run&lt;/span&gt;
   &lt;span class="n"&gt;Compiling&lt;/span&gt; &lt;span class="n"&gt;variables&lt;/span&gt; &lt;span class="n"&gt;v0&lt;/span&gt;&lt;span class="na"&gt;.1.0&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="cd"&gt;///projects/variables)&lt;/span&gt;
&lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;E0308&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt; &lt;span class="n"&gt;mismatched&lt;/span&gt; &lt;span class="n"&gt;types&lt;/span&gt;
 &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;src&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="py"&gt;.rs&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;14&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="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;spaces&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"   "&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;|&lt;/span&gt;                      &lt;span class="o"&gt;-----&lt;/span&gt; &lt;span class="n"&gt;expected&lt;/span&gt; &lt;span class="n"&gt;due&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;this&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;
&lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt;     &lt;span class="n"&gt;spaces&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;spaces&lt;/span&gt;&lt;span class="nf"&gt;.len&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;|&lt;/span&gt;              &lt;span class="o"&gt;^^^^^^^^^^^^&lt;/span&gt; &lt;span class="n"&gt;expected&lt;/span&gt; &lt;span class="err"&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="err"&gt;`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;found&lt;/span&gt; &lt;span class="err"&gt;`&lt;/span&gt;&lt;span class="nb"&gt;usize&lt;/span&gt;&lt;span class="err"&gt;`&lt;/span&gt;

&lt;span class="n"&gt;For&lt;/span&gt; &lt;span class="n"&gt;more&lt;/span&gt; &lt;span class="n"&gt;information&lt;/span&gt; &lt;span class="n"&gt;about&lt;/span&gt; &lt;span class="n"&gt;this&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="err"&gt;`&lt;/span&gt;&lt;span class="n"&gt;rustc&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="n"&gt;explain&lt;/span&gt; &lt;span class="n"&gt;E0308&lt;/span&gt;&lt;span class="err"&gt;`&lt;/span&gt;&lt;span class="py"&gt;.
error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;could&lt;/span&gt; &lt;span class="n"&gt;not&lt;/span&gt; &lt;span class="n"&gt;compile&lt;/span&gt; &lt;span class="err"&gt;`&lt;/span&gt;&lt;span class="n"&gt;variables&lt;/span&gt;&lt;span class="err"&gt;`&lt;/span&gt; &lt;span class="n"&gt;due&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;previous&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Rust defaults to immutability for variables, encouraging safer code and easier concurrency handling.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Immutable variables cannot be altered once a value is assigned to them.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Attempting to modify an immutable variable results in a compile-time error.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Immutability ensures predictability in code behavior and prevents unintentional value changes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Despite the default immutability, Rust allows variables to be declared mutable using the &lt;code&gt;mut&lt;/code&gt; keyword.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Mutability offers flexibility in code composition but should be used judiciously.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Constants in Rust are similar to immutable variables but have a few key differences.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Constants are declared using the &lt;code&gt;const&lt;/code&gt; keyword and must have a type annotation.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Constants are useful for values that remain constant throughout the program and can be declared in any scope.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Rust supports shadowing, allowing variables to be redeclared within the same scope.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Shadowing creates a new variable with the same name as an existing one, effectively hiding the original variable.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Shadowing differs from mutability in that it allows for the reuse of variable names and changing variable types.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Constants, immutability, mutability, and shadowing are fundamental concepts in Rust that contribute to code safety and readability.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As we wrap up this chapter, we've laid a solid foundation by comprehending the intricacies of variables and their behavior, including concepts like immutability, mutability, and shadowing. Our journey into Rust programming is far from over, though. In the upcoming chapters, we will dive into the realm of data types, unlocking a new layer of knowledge that will pave the way for crafting even more sophisticated and efficient Rust programs. Stay tuned for the next chapter, where we explore the diverse world of Rust data types and further elevate our programming skills.&lt;/p&gt;

</description>
      <category>rust</category>
      <category>beginners</category>
      <category>programming</category>
      <category>learning</category>
    </item>
    <item>
      <title>Mastering Cargo: A Comprehensive Guide to Harnessing the Power of Rust's Package Manager</title>
      <dc:creator>Anshuman Sathua</dc:creator>
      <pubDate>Thu, 01 Feb 2024 19:53:03 +0000</pubDate>
      <link>https://dev.to/anshu21/mastering-cargo-a-comprehensive-guide-to-harnessing-the-power-of-rusts-package-manager-185i</link>
      <guid>https://dev.to/anshu21/mastering-cargo-a-comprehensive-guide-to-harnessing-the-power-of-rusts-package-manager-185i</guid>
      <description>&lt;p&gt;Now that we've embarked on our Rust programming journey and mastered the art of writing a Hello World program from the command line, it's time to delve into Cargo – Rust's package manager and build system. In this blog post, we'll not only explore the reasons why Cargo is a game-changer but also provide a hands-on guide on how to leverage its features effectively.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Choose Cargo?
&lt;/h2&gt;

&lt;p&gt;Before we dive into the how, let's quickly recap why Cargo is an essential tool for Rust developers:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Dependency Management:&lt;/strong&gt; Cargo simplifies the process of adding and managing dependencies, ensuring a smooth integration of external libraries into your projects.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Consistent Project Structure:&lt;/strong&gt; Cargo generates a standardized project structure, fostering consistency and making collaboration more accessible.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Efficient Build Process:&lt;/strong&gt; With a single command, &lt;code&gt;cargo build&lt;/code&gt;, you can compile your code, handle dependencies, and build your project effortlessly.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Automated Testing:&lt;/strong&gt; Cargo integrates seamlessly with Rust's testing framework, allowing you to run tests with a simple &lt;code&gt;cargo test&lt;/code&gt; command.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Easy Distribution:&lt;/strong&gt; Cargo provides a streamlined way to package and distribute your Rust projects, making it convenient for both developers and users.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now, let's get hands-on with Cargo.&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting Started with Cargo:
&lt;/h2&gt;

&lt;p&gt;Cargo comes installed with Rust if you used the official installers discussed in the previous &lt;a href="https://dev.to/anshu21/write-your-first-hello-world-program-in-rust-579o"&gt;blog&lt;/a&gt;, if not then you can &lt;a href="https://www.rust-lang.org/tools/install"&gt;click here&lt;/a&gt; for a smooth setup. Check whether Cargo is installed by entering the following in your terminal:&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="err"&gt;$&lt;/span&gt; &lt;span class="n"&gt;cargo&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="n"&gt;version&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you see a version number, you have it! if not please go ahead and install it.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Creating a New Rust Project:
&lt;/h2&gt;

&lt;p&gt;To initialize a new Rust project, use the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="n"&gt;cargo&lt;/span&gt; &lt;span class="n"&gt;new&lt;/span&gt; &lt;span class="n"&gt;my_project&lt;/span&gt;
&lt;span class="n"&gt;cd&lt;/span&gt; &lt;span class="n"&gt;my_project&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will create a new directory named &lt;code&gt;my_project&lt;/code&gt; with the standard project structure.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Adding Dependencies:
&lt;/h2&gt;

&lt;p&gt;Open the &lt;code&gt;Cargo.toml&lt;/code&gt; file in your project directory. This file serves as the configuration file for your project. To add a dependency, modify the &lt;code&gt;[dependencies]&lt;/code&gt; section. 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="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;package&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"my_project"&lt;/span&gt;
&lt;span class="n"&gt;version&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"0.1.0"&lt;/span&gt;
&lt;span class="n"&gt;edition&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"2021"&lt;/span&gt;

&lt;span class="cs"&gt;# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html&lt;/span&gt;

&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;dependencies&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;rand&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"0.8.5"&lt;/span&gt; 

&lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="n"&gt;Rand&lt;/span&gt; &lt;span class="n"&gt;provides&lt;/span&gt; &lt;span class="n"&gt;utilities&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;generate&lt;/span&gt; &lt;span class="n"&gt;random&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;convert&lt;/span&gt; &lt;span class="n"&gt;them&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;useful&lt;/span&gt; &lt;span class="n"&gt;types&lt;/span&gt; &lt;span class="n"&gt;and&lt;/span&gt; &lt;span class="n"&gt;distributions&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;and&lt;/span&gt; &lt;span class="n"&gt;some&lt;/span&gt; &lt;span class="n"&gt;randomness&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;related&lt;/span&gt; &lt;span class="n"&gt;algorithms&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt; 

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

&lt;/div&gt;



&lt;p&gt;Run &lt;code&gt;cargo build&lt;/code&gt; to fetch and compile the specified dependencies. For now we don't need to add any dependencies but for future projects this is the place to add dependencies.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Building and Running a Cargo Project:
&lt;/h2&gt;

&lt;p&gt;Let's take a look at &lt;code&gt;src/main.rs&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;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;p&gt;Cargo has generated a “Hello, world!” program, just like the one we've written in the previous blog. Key differences are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Cargo placed the code in the src directory.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We've a Cargo.toml configuration file in the top directory.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the Cargo convention, our source files are anticipated to reside within the src directory. The primary project directory is designated for README files, license information, configuration files, and any other content unrelated to our actual code.&lt;/p&gt;

&lt;p&gt;As we've seen previously that in rust compiling and running are two differenet steps. For that in cargo we've the command &lt;code&gt;cargo build&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="err"&gt;$&lt;/span&gt; &lt;span class="n"&gt;cargo&lt;/span&gt; &lt;span class="n"&gt;build&lt;/span&gt;
   &lt;span class="n"&gt;Compiling&lt;/span&gt; &lt;span class="n"&gt;hello_cargo&lt;/span&gt; &lt;span class="n"&gt;v0&lt;/span&gt;&lt;span class="na"&gt;.1.0&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="cd"&gt;///projects/my_project)&lt;/span&gt;
    &lt;span class="n"&gt;Finished&lt;/span&gt; &lt;span class="n"&gt;dev&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;unoptimized&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;debuginfo&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="nf"&gt;target&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="mf"&gt;2.85&lt;/span&gt; &lt;span class="n"&gt;secs&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When you run this command, an executable file is created in &lt;code&gt;target/debug/my_project (or target\debug\my_project.exe on Windows)&lt;/code&gt;, not in your current directory. Since it's a default debug build, Cargo puts the binary in a debug directory. To run the executable, use this command:&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="err"&gt;$&lt;/span&gt; &lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;debug&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;my_project&lt;/span&gt; &lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="n"&gt;or&lt;/span&gt; &lt;span class="err"&gt;.\&lt;/span&gt;&lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="n"&gt;debug&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="n"&gt;my_project&lt;/span&gt;&lt;span class="py"&gt;.exe&lt;/span&gt; &lt;span class="n"&gt;on&lt;/span&gt; &lt;span class="n"&gt;Windows&lt;/span&gt;
&lt;span class="n"&gt;Hello&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nd"&gt;world!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After constructing our project using &lt;code&gt;cargo build&lt;/code&gt; and executing it with &lt;code&gt;./target/debug/my_project&lt;/code&gt;, there's an alternative approach. You can streamline the process by using &lt;code&gt;cargo run&lt;/code&gt;, which compiles the code and immediately executes the resulting executable in a single command:&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="err"&gt;$&lt;/span&gt; &lt;span class="n"&gt;cargo&lt;/span&gt; &lt;span class="n"&gt;run&lt;/span&gt;
    &lt;span class="n"&gt;Finished&lt;/span&gt; &lt;span class="n"&gt;dev&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;unoptimized&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;debuginfo&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="nf"&gt;target&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="mf"&gt;0.0&lt;/span&gt; &lt;span class="n"&gt;secs&lt;/span&gt;
     &lt;span class="n"&gt;Running&lt;/span&gt; &lt;span class="err"&gt;`&lt;/span&gt;&lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;debug&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;my_project&lt;/span&gt;&lt;span class="err"&gt;`&lt;/span&gt;
&lt;span class="n"&gt;Hello&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nd"&gt;world!&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Opting for &lt;code&gt;cargo run&lt;/code&gt; is more convenient compared to the two-step process of remembering to run &lt;code&gt;cargo build&lt;/code&gt; and then specifying the full path to the binary. Hence, the majority of developers prefer the simplicity of &lt;code&gt;cargo run&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;It's noteworthy that this time, no output indicated that Cargo was compiling &lt;code&gt;my_project&lt;/code&gt;. Cargo intelligently recognized that the files hadn't undergone any changes, opting to run the existing binary rather than initiating a rebuild. If modifications were made to the source code, Cargo would have executed a rebuild before running.&lt;/p&gt;

&lt;p&gt;Cargo also furnishes a command named &lt;code&gt;cargo check&lt;/code&gt;. This command swiftly verifies your code for compilation correctness without generating an executable:&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="err"&gt;$&lt;/span&gt; &lt;span class="n"&gt;cargo&lt;/span&gt; &lt;span class="n"&gt;check&lt;/span&gt;
   &lt;span class="n"&gt;Checking&lt;/span&gt; &lt;span class="n"&gt;hello_cargo&lt;/span&gt; &lt;span class="n"&gt;v0&lt;/span&gt;&lt;span class="na"&gt;.1.0&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="cd"&gt;///projects/my_project)&lt;/span&gt;
    &lt;span class="n"&gt;Finished&lt;/span&gt; &lt;span class="n"&gt;dev&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;unoptimized&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;debuginfo&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="nf"&gt;target&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="mf"&gt;0.32&lt;/span&gt; &lt;span class="n"&gt;secs&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Why might you opt not to generate an executable? Frequently, &lt;code&gt;cargo check&lt;/code&gt; proves significantly faster than &lt;code&gt;cargo build&lt;/code&gt; since it bypasses the step of creating an executable. If you find yourself consistently checking your work during the coding process, employing &lt;code&gt;cargo check&lt;/code&gt;expedites the feedback loop, promptly notifying you if your project is still compiling. Many Rust developers intermittently run &lt;code&gt;cargo check&lt;/code&gt; as they code to ensure compilation correctness, reserving &lt;code&gt;cargo build&lt;/code&gt; for when they're prepared to generate the executable.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Building for Release
&lt;/h2&gt;

&lt;p&gt;When your project is prepared for its final release, employ &lt;code&gt;cargo build --release&lt;/code&gt; to compile it with optimizations. This command produces an executable in &lt;code&gt;target/release&lt;/code&gt; rather than &lt;code&gt;target/debug&lt;/code&gt;. While these optimizations enhance the runtime performance of your Rust code, it's essential to note that enabling them prolongs the compilation time of your program.&lt;/p&gt;

&lt;p&gt;To accommodate different needs, Cargo provides two distinct profiles: one tailored for development, facilitating quick and frequent rebuilds, and another optimized for constructing the final program intended for users. This final version is designed for optimal runtime speed and is not subjected to repeated rebuilds.&lt;/p&gt;

&lt;p&gt;For accurate benchmarking of your code's execution time, ensure to execute &lt;code&gt;cargo build --release&lt;/code&gt; and conduct benchmarks using the resulting executable in the &lt;code&gt;target/release&lt;/code&gt; directory.&lt;/p&gt;

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

&lt;p&gt;Cargo is more than just a package manager – it's a versatile tool that significantly improves the Rust development experience. As we continue our Rust journey, we'll explore more advanced Cargo features, and leverage its power to streamline our workflow. Let's summarize the key points we've covered about Cargo:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Initiating a new project is accomplished with &lt;code&gt;cargo new&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Building a project is executed via &lt;code&gt;cargo build&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The combined process of building and running a project can be streamlined with &lt;code&gt;cargo run&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Conducting a swift code check for errors, without producing an executable, is achieved using &lt;code&gt;cargo check&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Cargo stores build results in the &lt;code&gt;target/debug&lt;/code&gt; directory, diverging from the project's source code directory.&lt;/li&gt;
&lt;li&gt;For optimal performance in a release-ready project, utilize &lt;code&gt;cargo build --release&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;A noteworthy advantage of Cargo is its cross-platform consistency, where commands remain the same regardless of the operating system.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Thanks for reading, i hope it was worth your time. Happy coding!&lt;/p&gt;

</description>
      <category>rust</category>
      <category>beginners</category>
      <category>learning</category>
      <category>programming</category>
    </item>
    <item>
      <title>Write your first Hello World! program in Rust</title>
      <dc:creator>Anshuman Sathua</dc:creator>
      <pubDate>Fri, 26 Jan 2024 12:20:23 +0000</pubDate>
      <link>https://dev.to/anshu21/write-your-first-hello-world-program-in-rust-579o</link>
      <guid>https://dev.to/anshu21/write-your-first-hello-world-program-in-rust-579o</guid>
      <description>&lt;h2&gt;
  
  
  Why Rust:
&lt;/h2&gt;

&lt;p&gt;Imagine crafting a rocket with Lego, where the pieces not only magically stay together but also soar through the skies at astonishing speeds without a hint of a crash. That's Rust in a nutshell: a programming language that's powerful, safe, and remarkably enjoyable to work with.&lt;/p&gt;

&lt;h2&gt;
  
  
  A Word of Caution:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Young Ecosystem: With fewer tools compared to a seasoned language, consider yourself a pioneer forging new paths!&lt;/li&gt;
&lt;li&gt;Learning Curve: It's more of a mountain than a hill, but the view from the top is breathtaking! ⛰️&lt;/li&gt;
&lt;li&gt;Not for Everyone: If your goal is a quick website, stick to simpler tools for now.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Alright, enough talk—let's dive in.&lt;/p&gt;

&lt;h2&gt;
  
  
  Installation:
&lt;/h2&gt;

&lt;p&gt;To embark on our Rust journey, we'll use &lt;code&gt;rustup&lt;/code&gt;, a command-line tool for managing Rust versions and associated tools.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;For Linux and macOS users, follow these &lt;a href="https://rust-book.cs.brown.edu/ch01-01-installation.html#installing-rustup-on-linux-or-macos"&gt;steps&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Windows users can click &lt;a href="https://www.rust-lang.org/tools/install"&gt;here&lt;/a&gt;. The installation process is straightforward, but if you encounter any issues, drop a comment below, and I'll do my best to assist.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Writing and Running a Rust Program
&lt;/h2&gt;

&lt;p&gt;Now that Rust is at our fingertips, let's write our inaugural Rust program. Open a terminal and enter the following commands to create a projects directory and a subdirectory for the “Hello, world!” project:&lt;/p&gt;

&lt;p&gt;For Windows CMD:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; mkdir "%USERPROFILE%\projects"
&amp;gt; cd /d "%USERPROFILE%\projects"
&amp;gt; mkdir hello_world
&amp;gt; cd hello_world
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For Linux, macOS and PowerShell on Windows:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;Next, create a new source file named &lt;code&gt;main.rs&lt;/code&gt;. Rust files always end with the &lt;code&gt;.rs&lt;/code&gt; extension. Add the following code to &lt;code&gt;main.rs&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;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;p&gt;Save the file and return to your terminal in the same directory. Compile and run the file using the following commands:&lt;/p&gt;

&lt;p&gt;For Windows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; rustc main.rs
&amp;gt; .\main.exe
Hello, world!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On Linux and macOS, use &lt;code&gt;./main&lt;/code&gt; instead of &lt;code&gt;.\main.exe&lt;/code&gt;. The terminal should display the string "Hello, world!"&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding the Basics: Anatomy of a Rust Program
&lt;/h2&gt;

&lt;p&gt;Let's dissect the program. Initially, we have:&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first line declares a function named &lt;code&gt;main&lt;/code&gt; with no parameters and no return value. The function body is enclosed in curly braces &lt;code&gt;{}&lt;/code&gt;. The main function is special—it's always the first code that runs in every executable Rust program.&lt;/p&gt;

&lt;p&gt;Inside the body of the &lt;code&gt;main&lt;/code&gt; function, you'll find:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;    &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"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;This line prints the string to the terminal.&lt;/p&gt;

&lt;p&gt;A couple of things to note: end a line with a semicolon (&lt;code&gt;;&lt;/code&gt;) to indicate the completion of the expression. Additionally, &lt;code&gt;println!&lt;/code&gt; calls a Rust macro. At this point, let's consider macros a mystery to unravel in our future, more advanced explorations. Remember, if you're writing &lt;code&gt;println!&lt;/code&gt;, you're calling a Rust macro, not a regular function. For regular functions, use &lt;code&gt;println&lt;/code&gt; without the &lt;code&gt;!&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Compiling and Running
&lt;/h2&gt;

&lt;p&gt;To run a Rust program, first, compile it using the Rust compiler with the &lt;code&gt;rustc&lt;/code&gt; command:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;If you're familiar with languages like C or C++, this is similar to using the gcc compiler. For those accustomed to dynamic languages like Ruby, Python, or JavaScript, the concept of compiling and running as separate steps might be new. Rust is an ahead-of-time compiled language, generating a binary executable file. It doesn't matter where your code lives, as long as you have the executable file, you can run the program even without having Rust installed—something not possible with dynamic languages like Ruby, Python, or JavaScript.&lt;/p&gt;

&lt;p&gt;After compilation, on Linux and macOS, you can see the executable file:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;On Windows, it might look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; dir /file names
main.exe
main.pdb
main.rs
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This displays the source code file with the &lt;code&gt;.rs&lt;/code&gt; extension, the executable file (main.exe on Windows, main on other platforms), and, when using Windows, a file containing debugging information with the .pdb extension. To run the program, use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ ./main  or .\main.exe on Windows
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This should print &lt;code&gt;Hello, world!&lt;/code&gt; to your terminal. Congrats!🎉 You've written your first Rust program. Keep an eye out for future updates, we'll explore more about Rust in the future. Thanks for reading, I hope it was worth your time. Have a fantastic day!&lt;/p&gt;

</description>
      <category>rust</category>
      <category>beginners</category>
      <category>learning</category>
      <category>development</category>
    </item>
    <item>
      <title>Understanding the Document Object Model (DOM) in Frontend Development</title>
      <dc:creator>Anshuman Sathua</dc:creator>
      <pubDate>Thu, 30 Nov 2023 07:16:16 +0000</pubDate>
      <link>https://dev.to/anshu21/understanding-the-document-object-model-dom-in-frontend-development-2h17</link>
      <guid>https://dev.to/anshu21/understanding-the-document-object-model-dom-in-frontend-development-2h17</guid>
      <description>&lt;p&gt;While exploring the world of frontend development, the term "DOM" frequently surfaces. Let's understand it. The DOM, or Document Object Model, is pivotal in creating dynamic and interactive web pages.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Static Nature of HTML
&lt;/h2&gt;

&lt;p&gt;HTML, a markup language, allows us to define the structure of a webpage using various tags. However, this structure is inherently static. Without a mechanism to alter the content within these tags, our webpage remains unchanged. Enter JavaScript and the DOM API.&lt;/p&gt;

&lt;h2&gt;
  
  
  JavaScript's DOM API
&lt;/h2&gt;

&lt;p&gt;JavaScript provides an essential tool: the DOM API. This API empowers developers to dynamically manipulate the structure of a webpage. Essentially, it serves as a bridge between the static HTML structure and the dynamic content we aspire to create.&lt;/p&gt;

&lt;h2&gt;
  
  
  Visualizing the DOM: A Tree-Like Structure
&lt;/h2&gt;

&lt;p&gt;Imagine transforming your HTML code into a tree-like representation. Each tag becomes a node in this hierarchical structure. This tree, known as the DOM tree, becomes the foundation for JavaScript's DOM API to navigate, manipulate, and render changes.&lt;/p&gt;

&lt;p&gt;HTML Code :-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;html&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;head&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;My&lt;/span&gt; &lt;span class="nx"&gt;Webpage&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/title&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/head&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;I&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;m a Heading&amp;lt;/h1&amp;gt;
    &amp;lt;p&amp;gt;This is a paragraph.&amp;lt;/p&amp;gt;
  &amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;DOM Representation :-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;HTML&lt;/span&gt;
&lt;span class="o"&gt;|&lt;/span&gt;
&lt;span class="o"&gt;|--&lt;/span&gt; &lt;span class="nx"&gt;HEAD&lt;/span&gt;
&lt;span class="o"&gt;|&lt;/span&gt;   &lt;span class="o"&gt;|&lt;/span&gt;
&lt;span class="o"&gt;|&lt;/span&gt;   &lt;span class="o"&gt;|--&lt;/span&gt; &lt;span class="nx"&gt;TITLE&lt;/span&gt;
&lt;span class="o"&gt;|&lt;/span&gt;   &lt;span class="o"&gt;|&lt;/span&gt;   &lt;span class="o"&gt;|&lt;/span&gt;
&lt;span class="o"&gt;|&lt;/span&gt;   &lt;span class="o"&gt;|&lt;/span&gt;   &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;My&lt;/span&gt; &lt;span class="nx"&gt;Webpage&lt;/span&gt;
&lt;span class="o"&gt;|&lt;/span&gt;
&lt;span class="o"&gt;|--&lt;/span&gt; &lt;span class="nx"&gt;BODY&lt;/span&gt;
&lt;span class="o"&gt;|&lt;/span&gt;   &lt;span class="o"&gt;|&lt;/span&gt;
&lt;span class="o"&gt;|&lt;/span&gt;   &lt;span class="o"&gt;|--&lt;/span&gt; &lt;span class="nx"&gt;H1&lt;/span&gt;
&lt;span class="o"&gt;|&lt;/span&gt;   &lt;span class="o"&gt;|&lt;/span&gt;   &lt;span class="o"&gt;|&lt;/span&gt;
&lt;span class="o"&gt;|&lt;/span&gt;   &lt;span class="o"&gt;|&lt;/span&gt;   &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;I&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;m a Heading
|   |
|   |-- P
|   |   |
|   |   | This is a paragraph.
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  DOM Manipulation in Three Steps
&lt;/h2&gt;

&lt;p&gt;DOM manipulation involves three fundamental steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Querying the DOM:&lt;/strong&gt; The process of locating a specific element within the DOM tree is aptly named querying. JavaScript searches and identifies the desired element to interact with.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;DOM Update:&lt;/strong&gt; Once located, the identified element undergoes changes as needed. This could involve altering text, updating attributes, or manipulating the structure itself.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Rendering the DOM:&lt;/strong&gt; The final step involves rendering or re-rendering the DOM. It's at this point that the browser displays the updated content. Notably, re-rendering can be a resource-intensive operation.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  The Balancing Act: Performance Considerations
&lt;/h2&gt;

&lt;p&gt;While querying and updating the DOM are relatively lightweight operations, frequent re-rendering can impact performance. This is where modern libraries and frameworks, such as React, come to the rescue.&lt;/p&gt;

&lt;h2&gt;
  
  
  Looking Ahead
&lt;/h2&gt;

&lt;p&gt;In subsequent discussions, we'll explore how frameworks like React address performance concerns and offer efficient solutions for building dynamic and responsive web applications.&lt;/p&gt;

&lt;p&gt;Thanks for exploring the fundamentals of the DOM with me. Stay tuned for more insights into the world of frontend development!&lt;/p&gt;

&lt;p&gt;Thanks for reading! Have a great day... :)&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>dom</category>
    </item>
  </channel>
</rss>
