avatarAnkit Tanna

Summary

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

2115

Abstract

This <code>T</code> is known as Type Parameter and can be passed along</li><li><code>Option</code> has 2 <code>variants</code>, <code>None</code> and <code>Some<T></code>. <code>None</code> means undefined/empty/null and <code>Some<T></code> means some elements of type <code>T</code>.</li><li><code>Result</code> has 2 <code>variants</code>, <code>Ok<T></code> and <code>Err<E></code>. <code>Ok</code> is for success message and <code>Err</code> is for error message.</li></ol><p id="a3e0">Since <code>Option</code> and <code>Result</code> are built in to the system, you do not need the namespace syntax, i.e. <code>::</code>, for accessing the <code>None</code> or <code>Some</code> and <code>Ok</code> or <code>Error</code>. You can directly use them. Lets have a look at below code snippet:</p><div id="f5b7"><pre><span class="hljs-keyword">fn</span> <span class="hljs-title function_">main</span>() { <span class="hljs-keyword">let</span> <span class="hljs-keyword">mut </span><span class="hljs-variable">my_string</span>: <span class="hljs-type">String</span> = <span class="hljs-string">"hello world"</span>.<span class="hljs-title function_ invoke__">to_string</span>(); <span class="hljs-keyword">let</span> <span class="hljs-keyword">mut </span><span class="hljs-variable">email_string</span>: <span class="hljs-type">String</span> = <span class="hljs-string">"[email protected]"</span>.<span class="hljs-title function_ invoke__">to_string</span>(); <span class="hljs-comment">// let last_char: char = my_string.pop();</span> <span class="hljs-keyword">let</span> <span class="hljs-variable">last_char</span>: <span class="hljs-type">Option</span><<span class="hljs-type">char</span>> = my_string.<span class="hljs-title function_ invoke__">pop</span>(); <span class="hljs-keyword">let</span> <span class="hljs-variable">email</span>: <span class="hljs-type">Option</span><<span class="hljs-type">String</span>> = <span class="hljs-title function_ invoke__">Some</span>(email_string); <span class="hljs-keyword">let</span> <span class="hljs-varia

Options

ble">empty_string</span>: <span class="hljs-type">Option</span><<span class="hljs-type">String</span>> = <span class="hljs-literal">None</span>; <span class="hljs-keyword">let</span> <span class="hljs-variable">success</span>: <span class="hljs-type">Result</span><<span class="hljs-type">String</span>, <span class="hljs-type">String</span>> = <span class="hljs-title function_ invoke__">Ok</span>(<span class="hljs-string">"Success"</span>.<span class="hljs-title function_ invoke__">to_string</span>()); <span class="hljs-keyword">let</span> <span class="hljs-variable">error</span>: <span class="hljs-type">Result</span><<span class="hljs-type">String</span>, <span class="hljs-type">String</span>> = <span class="hljs-title function_ invoke__">Err</span>(<span class="hljs-string">"Error"</span>.<span class="hljs-title function_ invoke__">to_string</span>()); }</pre></div><h2 id="ab3a">Observations:</h2><ol><li><code>Option<char></code> is used to store the result coming from <code>my_string.pop()</code> which can either be a character or <code>undefined</code> or <code>null</code> if string is empty.</li><li><code>Some(email_string)</code> is used to represent some <code>char</code> from <code>email_string</code></li><li>Notice that <code>Some</code> is not used as <code>Option::Some()</code>, i.e. with namespace, it is directly used.</li><li>Similarly <code>Ok</code> and <code>Err</code> are used without a namespace.</li></ol><p id="a2f6">Using Type Parameters, we can build a flexible types and prevent Rust compiler from panicking when it encounters <code>undefined</code> and <code>null</code>.</p><p id="1e0e">I hope you liked this article about Type Parameters. Please share your feedback in the comments.</p><p id="5d83">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></article></body>

The Rust Programming Language

The Rust Programming Language — Pattern Matching — Type Parameters

Every coin has two sides — good and bad. Similarly, every programming language does have to deal with something that is defined and undefined or null. Rust particularly does not shine well when it comes to handling undefined or null. Rust will panic and start throwing an error when it comes across something that is undefined or null.

For these edge cases where things do go bad, Rust has given us Type Parameters and twoenum types, Option and Result. These two enum types are built into Rust language and can be used across the language and packages.

Option is used to handle the cases where there are chances or receiving undefined or null.

Result is used to handle the cases where there are chances getting Success and Error, for e.g. an asynchronous task.

Let’s see the definition of Option and Result in the below snippets:

enum Option<T> {
    None,
    Some(T),
}
enum Result<T, E> {
    Ok(T),
    Err(E),
}

Observations:

  1. These enums Options and Result are part of the Rust Language itself.
  2. It passes types using T inside the <> brackets, just like generics in TypeScript.
  3. This T is known as Type Parameter and can be passed along
  4. Option has 2 variants, None and Some<T>. None means undefined/empty/null and Some<T> means some elements of type T.
  5. Result has 2 variants, Ok<T> and Err<E>. Ok is for success message and Err is for error message.

Since Option and Result are built in to the system, you do not need the namespace syntax, i.e. ::, for accessing the None or Some and Ok or Error. You can directly use them. Lets have a look at below code snippet:

fn main() {
    let mut my_string: String = "hello world".to_string();
    let mut email_string: String = "[email protected]".to_string();
    // let last_char: char = my_string.pop();
    let last_char: Option<char> = my_string.pop();
    let email: Option<String> = Some(email_string);
    let empty_string: Option<String> = None;
    let success: Result<String, String> = Ok("Success".to_string());
    let error: Result<String, String> = Err("Error".to_string());
}

Observations:

  1. Option<char> is used to store the result coming from my_string.pop() which can either be a character or undefined or null if string is empty.
  2. Some(email_string) is used to represent some char from email_string
  3. Notice that Some is not used as Option::Some(), i.e. with namespace, it is directly used.
  4. Similarly Ok and Err are used without a namespace.

Using Type Parameters, we can build a flexible types and prevent Rust compiler from panicking when it encounters undefined and null.

I hope you liked this article about Type Parameters. Please share your feedback in the comments.

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

Rust
Rust Programming Language
Webassembly
Web Development
Performance
Recommended from ReadMedium