<?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>Forem: Sonu Bardai</title>
    <description>The latest articles on Forem by Sonu Bardai (@sonubardai).</description>
    <link>https://forem.com/sonubardai</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%2F1112025%2F10a2b4db-45ef-4ebc-be4b-334493247944.jpg</url>
      <title>Forem: Sonu Bardai</title>
      <link>https://forem.com/sonubardai</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/sonubardai"/>
    <language>en</language>
    <item>
      <title>Structs and Enums in Rust</title>
      <dc:creator>Sonu Bardai</dc:creator>
      <pubDate>Wed, 12 Jul 2023 13:46:56 +0000</pubDate>
      <link>https://forem.com/sonubardai/structs-and-enums-in-rust-hme</link>
      <guid>https://forem.com/sonubardai/structs-and-enums-in-rust-hme</guid>
      <description>&lt;p&gt;Rust is a powerful systems programming language that has been rapidly gaining popularity among developers. One of the reasons for its success is its rich type system, which provides a high level of expressiveness and flexibility when defining custom data types. Two of the most important constructs in Rust’s type system are structures (&lt;code&gt;struct&lt;/code&gt;) and enumerations (&lt;code&gt;enum&lt;/code&gt;), which allow developers to create complex data types that accurately model the problem domain.&lt;/p&gt;

&lt;p&gt;In this blog post, we will take a deep dive into &lt;code&gt;struct&lt;/code&gt; and &lt;code&gt;enum&lt;/code&gt; in Rust, exploring their syntax, usage, and best practices. We will learn how to use these constructs to create expressive and efficient code that is easy to read, understand, and maintain. Whether you’re new to Rust or an experienced developer looking to deepen your understanding of the language, this post is for you! So grab a cup of coffee and let’s get started on our journey to mastering &lt;code&gt;struct&lt;/code&gt; and &lt;code&gt;enum&lt;/code&gt; in Rust!&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding Structs
&lt;/h2&gt;

&lt;p&gt;Structs are a way to define custom data types in Rust. They allow you to group related data together into a single, cohesive unit. Structs are similar to classes in other languages, but with some important differences.&lt;/p&gt;

&lt;p&gt;Let’s say we’re creating a Mario game and we want to represent Mario as a character in our game. We could use a &lt;code&gt;struct&lt;/code&gt; to define a &lt;code&gt;Mario&lt;/code&gt; type 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;struct&lt;/span&gt; &lt;span class="n"&gt;Position&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;u32&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="nb"&gt;u32&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;Mario&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;position&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Position&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;coins&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;u32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;power_up&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Option&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;PowerUp&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we’ve defined a &lt;code&gt;Mario&lt;/code&gt; struct with three fields: &lt;code&gt;position&lt;/code&gt;, &lt;code&gt;coins&lt;/code&gt;, and &lt;code&gt;power_up&lt;/code&gt;. The &lt;code&gt;position&lt;/code&gt; field is of type &lt;code&gt;Position&lt;/code&gt;, which we defined earlier. The &lt;code&gt;coins&lt;/code&gt; field is of type &lt;code&gt;u32&lt;/code&gt;, representing the number of coins Mario has collected. The &lt;code&gt;power_up&lt;/code&gt; field is of type &lt;code&gt;Option&amp;lt;PowerUp&amp;gt;&lt;/code&gt;, representing an optional power-up that Mario may have. We will discuss this type later when we come to Enums.&lt;/p&gt;

&lt;p&gt;To create an instance of this struct, we can use the following syntax:&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;mario&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Mario&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;position&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Position&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="mi"&gt;0&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="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="n"&gt;coins&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;power_up&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;None&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can access the fields of a struct using dot notation, like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;pos&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mario&lt;/span&gt;&lt;span class="py"&gt;.position&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;coins&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mario&lt;/span&gt;&lt;span class="py"&gt;.coins&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;power_up&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mario&lt;/span&gt;&lt;span class="py"&gt;.power_up&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Structs can also have methods, which are functions associated with the struct. Here’s an example of how to define a method for our &lt;code&gt;Mario&lt;/code&gt; struct:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;impl&lt;/span&gt; &lt;span class="n"&gt;Mario&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;jump&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="py"&gt;.position.y&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="k"&gt;if&lt;/span&gt; &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nf"&gt;Some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;power_up&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="py"&gt;.power_up&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;power_up&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="nn"&gt;PowerUp&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;SuperMushroom&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="py"&gt;.position.y&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="nn"&gt;PowerUp&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;FireFlower&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="py"&gt;.position.y&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;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 example, we’ve defined a method called &lt;code&gt;jump&lt;/code&gt; that makes Mario jump. The height of the jump depends on whether Mario has a power-up and what type of power-up it is. To call this method, we can use the following syntax:&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;mario&lt;/span&gt;&lt;span class="nf"&gt;.jump&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, structs are a powerful way to define custom data types in Rust. They allow you to group related data together and provide methods for working with that data. In the next section, we’ll explore another important construct in Rust’s type system: &lt;code&gt;enums&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Enum1, Enum2, Enum3
&lt;/h2&gt;

&lt;p&gt;Enums, short for enumerations, are another way to define custom data types in Rust. They allow you to define a type with a fixed set of values, known as variants. Enums are useful when you need to represent a value that can be one of several different options.&lt;/p&gt;

&lt;p&gt;Let’s say we want to represent the different power-ups that Mario can have in our game. We could use an &lt;code&gt;enum&lt;/code&gt; to define a &lt;code&gt;PowerUp&lt;/code&gt; type like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;PowerUp&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;SuperMushroom&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;FireFlower&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;Starman&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;CapeFeather&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we’ve defined an &lt;code&gt;enum&lt;/code&gt; called &lt;code&gt;PowerUp&lt;/code&gt; with four variants: &lt;code&gt;SuperMushroom&lt;/code&gt;, &lt;code&gt;FireFlower&lt;/code&gt;, &lt;code&gt;Starman&lt;/code&gt;, and &lt;code&gt;CapeFeather&lt;/code&gt;. Each variant represents a different type of power-up that Mario can have.&lt;/p&gt;

&lt;p&gt;To create an instance of this enum, we can use the following syntax:&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;power_up&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;PowerUp&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;SuperMushroom&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can use pattern matching to check which variant an enum value is. Here’s an example of how to do 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;match&lt;/span&gt; &lt;span class="n"&gt;power_up&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nn"&gt;PowerUp&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;SuperMushroom&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;"Mario got a Super Mushroom!"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nn"&gt;PowerUp&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;FireFlower&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;"Mario got a Fire Flower!"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nn"&gt;PowerUp&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Starman&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;"Mario got a Starman!"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nn"&gt;PowerUp&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;CapeFeather&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;"Mario got a Cape Feather!"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we’re using a &lt;code&gt;match&lt;/code&gt; expression to check which variant the &lt;code&gt;power_up&lt;/code&gt; value is. Depending on the variant, we print a different message to the screen.&lt;/p&gt;

&lt;p&gt;As you can see, enums allow you to represent values that can be one of several different options and provide an expressive way to work with those values. In the next section, we’ll explore how enums and structs can be combined to create even more complex data types.&lt;/p&gt;

&lt;h2&gt;
  
  
  Combining both our super powers! Structs and Enums
&lt;/h2&gt;

&lt;p&gt;Structs and enums are powerful constructs on their own, but they can be even more powerful when combined. By using enums to group related structs or adding a field to a struct that is an enum type, you can create complex data types that accurately model your problem domain.&lt;/p&gt;

&lt;p&gt;Say we want to represent the different enemies that Mario can encounter in our game. We could use an &lt;code&gt;enum&lt;/code&gt; to define an &lt;code&gt;Enemy&lt;/code&gt; type like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;Enemy&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;Goomba&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Goomba&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nf"&gt;KoopaTroopa&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;KoopaTroopa&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nf"&gt;HammerBro&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;HammerBro&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we’ve defined an &lt;code&gt;enum&lt;/code&gt; called &lt;code&gt;Enemy&lt;/code&gt; with three variants: &lt;code&gt;Goomba&lt;/code&gt;, &lt;code&gt;KoopaTroopa&lt;/code&gt;, and &lt;code&gt;HammerBro&lt;/code&gt;. Each variant is associated with a different struct that represents the specific type of enemy.&lt;/p&gt;

&lt;p&gt;Here’s what the &lt;code&gt;Goomba&lt;/code&gt; struct might 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;struct&lt;/span&gt; &lt;span class="n"&gt;Goomba&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;position&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Position&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;is_stomped&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;bool&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And here’s what the &lt;code&gt;KoopaTroopa&lt;/code&gt; struct might 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;struct&lt;/span&gt; &lt;span class="n"&gt;KoopaTroopa&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;position&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Position&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;is_stomped&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;bool&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;is_in_shell&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;bool&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By using an enum to group these related structs together, we can create a single &lt;code&gt;Enemy&lt;/code&gt; type that can represent any of the different types of enemies in our game. This makes it easy to write code that can work with any type of enemy, without having to worry about the specific details of each enemy type.&lt;/p&gt;

&lt;p&gt;Here’s an example of how we might use this &lt;code&gt;Enemy&lt;/code&gt; enum in our 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;stomp_enemy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;enemy&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;Enemy&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="n"&gt;enemy&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nn"&gt;Enemy&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;Goomba&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;goomba&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;goomba&lt;/span&gt;&lt;span class="py"&gt;.is_stomped&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="nn"&gt;Enemy&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;KoopaTroopa&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;koopa&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="n"&gt;koopa&lt;/span&gt;&lt;span class="py"&gt;.is_stomped&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="n"&gt;koopa&lt;/span&gt;&lt;span class="py"&gt;.is_in_shell&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="p"&gt;}&lt;/span&gt;
        &lt;span class="nn"&gt;Enemy&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;HammerBro&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="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we’ve defined a function called &lt;code&gt;stomp_enemy&lt;/code&gt; that takes an &lt;code&gt;Enemy&lt;/code&gt; as an argument and stomps on it. The behavior of the function depends on which variant of the &lt;code&gt;Enemy&lt;/code&gt; enum is passed in. If it’s a &lt;code&gt;Goomba&lt;/code&gt;, we set its &lt;code&gt;is_stomped&lt;/code&gt; field to &lt;code&gt;true&lt;/code&gt;. If it’s a &lt;code&gt;KoopaTroopa&lt;/code&gt;, we set both its &lt;code&gt;is_stomped&lt;/code&gt; and &lt;code&gt;is_in_shell&lt;/code&gt; fields to &lt;code&gt;true&lt;/code&gt;. If it’s a &lt;code&gt;HammerBro&lt;/code&gt;, we don’t do anything.&lt;/p&gt;

&lt;p&gt;Combining structs and enums allows us to create complex data types that accurately model our problem domain. By using enums to group related structs together, we can write code that is expressive, flexible, and easy to understand.&lt;/p&gt;

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

&lt;p&gt;In conclusion, &lt;code&gt;struct&lt;/code&gt; and &lt;code&gt;enum&lt;/code&gt; are two powerful constructs in Rust's type system that allow developers to define custom data types. Structs are used to group related data together into a single unit, while enums are used to define types with a fixed set of values. By combining structs and enums, developers can create complex data types that accurately model their problem domain.&lt;/p&gt;

&lt;p&gt;In this blog post, we've explored the syntax and usage of &lt;code&gt;struct&lt;/code&gt; and &lt;code&gt;enum&lt;/code&gt; in Rust, providing examples and best practices along the way. We've also seen how these constructs can be combined to create even more powerful data types. Whether you're new to Rust or an experienced developer, we hope this post has deepened your understanding of these important features of the language.&lt;/p&gt;

&lt;h2&gt;
  
  
  About me
&lt;/h2&gt;

&lt;p&gt;I’m a software engineer and a tech blogger with a passion for writing about the latest developments in the world of technology. I have experience working with a variety of programming languages, including Python, TypeScript, Rust, and Go, and I love sharing my knowledge and insights with others.&lt;/p&gt;

&lt;p&gt;When I’m not coding or writing, you can find me exploring the great outdoors, making art. I’m a digital artist who enjoys drawing and painting.&lt;/p&gt;

&lt;p&gt;I’m always looking to connect with other tech enthusiasts and artists, so feel free to reach out to me on my socials!&lt;/p&gt;

&lt;h3&gt;
  
  
  You can find me on:
&lt;/h3&gt;

&lt;p&gt;Twitter: &lt;a href="https://twitter.com/SonuBardai"&gt;https://twitter.com/SonuBardai&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;LinkedIn: &lt;a href="https://www.linkedin.com/mwlite/in/sonu-bardai"&gt;https://www.linkedin.com/mwlite/in/sonu-bardai&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;GitHub: &lt;a href="https://github.com/SonuBardai"&gt;https://github.com/SonuBardai&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Personal Website: &lt;a href="https://sonubardai-portfolio.web.app"&gt;https://sonubardai-portfolio.web.app&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thanks for stopping by, and I hope you enjoyed my blog! Happy coding! 😊&lt;/p&gt;

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