avatarAnkit Tanna

Summary

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

2209

Abstract

pan class="hljs-title class_">Color</span> { Red, Yellow, Green }

<span class="hljs-keyword">fn</span> <span class="hljs-title function_">main</span>() { <span class="hljs-keyword">let</span> <span class="hljs-variable">go</span>: Color = Color::Green; <span class="hljs-keyword">let</span> <span class="hljs-variable">caution</span>: Color = Color::Yellow; <span class="hljs-keyword">let</span> <span class="hljs-variable">stop</span>: Color = Color::Red; }</pre></div><p id="4524">An <code>enum</code> can contain a <code>tuple</code> as a variant as well as a <code>struct</code>.</p><p id="521e">Refer to below snippet for an example of <code>tuple</code> as a variant in an <code>enum</code>:</p><div id="a9ec"><pre><span class="hljs-keyword">enum</span> <span class="hljs-title class_">Color</span> { Red, Yellow, Green, <span class="hljs-title function_ invoke__">Custom</span>(<span class="hljs-type">u8</span>, <span class="hljs-type">u8</span>, <span class="hljs-type">u8</span>) }

<span class="hljs-keyword">fn</span> <span class="hljs-title function_">main</span>() { <span class="hljs-keyword">let</span> <span class="hljs-variable">go</span>: Color = Color::Green; <span class="hljs-keyword">let</span> <span class="hljs-variable">caution</span>: Color = Color::Yellow; <span class="hljs-keyword">let</span> <span class="hljs-variable">stop</span>: Color = Color::Red; <span class="hljs-keyword">let</span> <span class="hljs-variable">brown</span> = Color::<span class="hljs-title function_ invoke__">Custom</span>(<span class="hljs-number">128</span>, <span class="hljs-number">64</span>, <span class="hljs-number">0</span>); }</pre></div><h2 id="1777">Observations:</h2><ol><li>You can define a Custom <code>tuple</code> as a <code>variant</code> in the <code>enum</code>.</li><li><code>Custom(u8, u8, u8)</code> is a type added in the <code>Color</code> enum.</li><li>You can assign a <code>Custom(u8, u8, u8)</code> value to a variable <code>brown</code> using the <code>namespace</code> concept.</li></ol><p id="6a5f">Below is an example of how to use <code>structs</code> as a variants in the <code>enum</code>.</p><div id="daa2"><p

Options

re><span class="hljs-keyword">enum</span> <span class="hljs-title class_">Color</span> { Red, Yellow, Green, Custom { red: <span class="hljs-type">u8</span>, green: <span class="hljs-type">u8</span>, blue: <span class="hljs-type">u8</span> } }

<span class="hljs-keyword">fn</span> <span class="hljs-title function_">main</span>() { <span class="hljs-keyword">let</span> <span class="hljs-variable">go</span>: Color = Color::Green; <span class="hljs-keyword">let</span> <span class="hljs-variable">caution</span>: Color = Color::Yellow; <span class="hljs-keyword">let</span> <span class="hljs-variable">stop</span>: Color = Color::Red; <span class="hljs-keyword">let</span> <span class="hljs-variable">purple</span> = Color::Custom { red: <span class="hljs-number">128</span>, green: <span class="hljs-number">0</span>, blue: <span class="hljs-number">128</span> }; }</pre></div><h2 id="04dc">Observations:</h2><ol><li>Just like <code>tuples</code> we can define a <code>struct</code> variant</li><li><code>Custom { red: u8, green: u8, blue: u8 }</code></li></ol><p id="8fb4">I hope you liked this articles on how to create <code>enum</code> in Rust. Please leave your feedback in the comments section.</p><p id="a883">You can subscribe to my newsletter about <b>The Rust Programming Language <a href="https://tinyletter.com/ankittanna"></a></b><a href="https://tinyletter.com/ankittanna">here</a>. You can read about all the articles in this series <a href="https://readmedium.com/the-rust-programming-language-4b22bc717ecc">here</a>.</p><h1 id="0915">Stackademic</h1><p id="d4b7"><i>Thank you for reading until the end. Before you go:</i></p><ul><li><i>Please consider <b>clapping</b> and <b>following</b> the writer! 👏</i></li><li><i>Follow us on <a href="https://twitter.com/stackademichq"><b>Twitter(X)</b></a>, <a href="https://www.linkedin.com/company/stackademic"><b>LinkedIn</b></a>, and <a href="https://www.youtube.com/c/stackademic"><b>YouTube</b></a><b>.</b></i></li><li><i>Visit <a href="http://stackademic.com/"><b>Stackademic.com</b></a> to find out more about how we are democratizing free programming education around the world.</i></li></ul></article></body>

The Rust Programming Language

The Rust Programming Language — Pattern Matching — Enums

enum or more elaborately known as Enumerations, are a group of possible set of values that can be assigned to a variable. Defining a set of values reduces chances of errors of accidentally assigning a wrong value.

Let’s say we want to restrict the types of colours a user can chose from Red,` Green, Yellow. Rust allows us to encode these possibilities as enums. Please check below snippet.

enum Color {
    Red,
    Yellow,
    Green
}

fn main() {
    let go = Color::Green;
    let caution = Color::Yellow;
    let stop = Color::Red;
}

Observations:

  1. You need to use enum keyword to define an enum
  2. You need to give it a name, in our case Color.
  3. In order to access values to be assigned to a variable, we need to access them using the namespace with double-colons ::. The namespace is the name of enum that we game which is Color. For e.g. Color::Green, Color::Yellow or Color::Red.

In this case, Color is known as identifier and Red, Yellow and Green are known as variants. The identifier Color can be used as a type of variable. For e.g. refer to below snippet, the type of go, caution and stop is Color:

enum Color {
    Red,
    Yellow,
    Green
}

fn main() {
    let go: Color = Color::Green;
    let caution: Color = Color::Yellow;
    let stop: Color = Color::Red;
}

An enum can contain a tuple as a variant as well as a struct.

Refer to below snippet for an example of tuple as a variant in an enum:

enum Color {
    Red,
    Yellow,
    Green,
    Custom(u8, u8, u8)
}

fn main() {
    let go: Color = Color::Green;
    let caution: Color = Color::Yellow;
    let stop: Color = Color::Red;
    let brown = Color::Custom(128, 64, 0);
}

Observations:

  1. You can define a Custom tuple as a variant in the enum.
  2. Custom(u8, u8, u8) is a type added in the Color enum.
  3. You can assign a Custom(u8, u8, u8) value to a variable brown using the namespace concept.

Below is an example of how to use structs as a variants in the enum.

enum Color {
    Red,
    Yellow,
    Green,
    Custom { red: u8, green: u8, blue: u8 }
}

fn main() {
    let go: Color = Color::Green;
    let caution: Color = Color::Yellow;
    let stop: Color = Color::Red;
    let purple = Color::Custom { red: 128, green: 0, blue: 128 };
}

Observations:

  1. Just like tuples we can define a struct variant
  2. Custom { red: u8, green: u8, blue: u8 }

I hope you liked this articles on how to create enum in Rust. Please leave your feedback in the comments section.

You can subscribe to my newsletter about The Rust Programming Language here. You can read about all the articles in this series here.

Stackademic

Thank you for reading until the end. Before you go:

  • Please consider clapping and following the writer! 👏
  • Follow us on Twitter(X), LinkedIn, and YouTube.
  • Visit Stackademic.com to find out more about how we are democratizing free programming education around the world.
Rust
Rust Programming Language
Webassembly
Web Development
Rustlang
Recommended from ReadMedium