avatarAnkit Tanna

Summary

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

2937

Abstract

point.z);

<span class="hljs-built_in">println!</span>(<span class="hljs-string">"Point x: {}"</span>, point1.x);
<span class="hljs-built_in">println!</span>(<span class="hljs-string">"Point y: {}"</span>, point1.y);
<span class="hljs-built_in">println!</span>(<span class="hljs-string">"Point z: {}"</span>, point1.z);

<span class="hljs-built_in">println!</span>(<span class="hljs-string">"Point x: {}"</span>, point2.x);
<span class="hljs-built_in">println!</span>(<span class="hljs-string">"Point y: {}"</span>, point2.y);
<span class="hljs-built_in">println!</span>(<span class="hljs-string">"Point z: {}"</span>, point2.z);

}

<span class="hljs-keyword">fn</span> <span class="hljs-title function_">new_point</span>(a: <span class="hljs-type">i64</span>, b: <span class="hljs-type">i64</span>, c: <span class="hljs-type">i64</span>) <span class="hljs-punctuation">-></span> Point { <span class="hljs-keyword">return</span> Point { x: a, y: b, z: c }; }

<span class="hljs-keyword">fn</span> <span class="hljs-title function_">new_point2</span>(x: <span class="hljs-type">i64</span>, y: <span class="hljs-type">i64</span>, z: <span class="hljs-type">i64</span>) <span class="hljs-punctuation">-></span> Point { Point { x, y, z } }</pre></div><h2 id="81e8">Observations:</h2><ol><li>When assigning value to <code>point</code>, we are using name of the struct <code>Point</code> before assigning the value. <code>let point: Point = Point { x: 0, y: 1, z: 2 };</code></li><li>Return type of functions <code>new_point</code> and <code>new_point2</code> is <code>Point</code>.</li><li><code>new_point</code> returns via statement convention where as <code>new_point2</code> returns via expression as a last line of the function.</li><li>We need to mention the name, i.e. <code>Point</code>, of the struct before returning.</li></ol><p id="0999">Just like tuples, you can de-structure the <code>struct</code>. Look at the below code snippet:</p><div id="b899"><pre><span class="hljs-keyword">struct</span> <span class="hljs-title class_">Point</span> { x: <span class="hljs-type">i64</span>, y: <span class="hljs-type">i64</span>, z: <span class="hljs-type">i64</span> }

<span class="hljs-keyword">fn</span> <span class="hljs-title function_">main</span>() { <span class="hljs-keyword">let</span> <span class="hljs-variable">point</span>: Point = Point { x: <span class="hljs-number">0</span>, y: <span class="hljs-number">1</span>, z: <span class="hljs-number">2</span> }; <span class="hljs-keyword">let</span> <span class="hljs-variable">point1</span>: Point = <span class="hljs-title function_ invoke__">new_point</span>(<span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>); <span class="hljs-keyword">let</span> <span class="hljs-variable">point2</span>: Point = <span class="hljs-title function_ invoke__">new_point

Options

2</span>(<span class="hljs-number">6</span>, <span class="hljs-number">7</span>, <span class="hljs-number">8</span>);

<span class="hljs-keyword">let</span> <span class="hljs-variable">Point</span> {x, y, z} = point;
<span class="hljs-keyword">let</span> <span class="hljs-variable">Point</span> {x, y: _, z} = point1;
<span class="hljs-keyword">let</span> <span class="hljs-variable">Point</span> {x, ..} = point2;

}

<span class="hljs-keyword">fn</span> <span class="hljs-title function_">new_point</span>(a: <span class="hljs-type">i64</span>, b: <span class="hljs-type">i64</span>, c: <span class="hljs-type">i64</span>) <span class="hljs-punctuation">-></span> Point { <span class="hljs-keyword">return</span> Point { x: a, y: b, z: c }; }

<span class="hljs-keyword">fn</span> <span class="hljs-title function_">new_point2</span>(x: <span class="hljs-type">i64</span>, y: <span class="hljs-type">i64</span>, z: <span class="hljs-type">i64</span>) <span class="hljs-punctuation">-></span> Point { Point { x, y, z } }</pre></div><h2 id="10b3">Observations:</h2><ol><li>Ignoring a property while de-structuring, you need to use <code>_</code> against the property name you wish to ignore.</li><li>Ignoring all the other properties can be done using <code>..</code> and you can focus on only the properties you are interested.</li></ol><p id="f565">Like tuples, structs are also mutable using <code>mut</code> keyword. Refer to below code snippet:</p><div id="0369"><pre><span class="hljs-keyword">struct</span> <span class="hljs-title class_">Point</span> { x: <span class="hljs-type">i64</span>, y: <span class="hljs-type">i64</span>, z: <span class="hljs-type">i64</span> }

<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">point</span>: Point = Point { x: <span class="hljs-number">0</span>, y: <span class="hljs-number">1</span>, z: <span class="hljs-number">2</span> };

point.x = point.x + <span class="hljs-number">1</span>;

<span class="hljs-built_in">println!</span>(<span class="hljs-string">"Point x: {}"</span>, point.x);

}</pre></div><p id="d939">To re-iterate, <code>struct</code> cannot be modified in structure while the code is running. You can make nested tuples and nested structs and tuples can contain structs and structs can contain tuples.</p><p id="f3dd">That’s it for structs in The Rust Programming Language. Please share your feedback in the comments section.</p><p id="87a4">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 — Collections — Structs

Structs in Rust are kind of a mixture of an interface and a class from TypeScript/JavaScript. Structs, like tuple, cannot be altered. We cannot change the properties of the object during the lifecycle of the program.

Structs can be defined using struct keyword in Rust. Unlike tuples, which are index based, we have properties of an object in a struct and hence they are based on the name of the properties in an object. Have a look at the below code snippet:

struct Point {
    x: i64,
    y: i64,
    z: i64
}

Observations:

  1. As you can see, you need to use struct keyword to define a struct.
  2. You need to give a name, in our case Point.
  3. You can define the properties inside the struct, i.e. x, y and z.
  4. You can mention the types of each and every property, i.e. i64.

When declaring a struct, you need to mention the name of the struct you are declaring. See the below code snippet:

struct Point {
    x: i64,
    y: i64,
    z: i64
}

fn main() {
    let point: Point = Point { x: 0, y: 1, z: 2 };
    let point1: Point = new_point(3, 4, 5);
    let point2: Point = new_point2(6, 7, 8);

    println!("Point x: {}", point.x);
    println!("Point y: {}", point.y);
    println!("Point z: {}", point.z);

    println!("Point x: {}", point1.x);
    println!("Point y: {}", point1.y);
    println!("Point z: {}", point1.z);

    println!("Point x: {}", point2.x);
    println!("Point y: {}", point2.y);
    println!("Point z: {}", point2.z);
}

fn new_point(a: i64, b: i64, c: i64) -> Point {
    return Point { x: a, y: b, z: c };
}

fn new_point2(x: i64, y: i64, z: i64) -> Point {
    Point { x, y, z }
}

Observations:

  1. When assigning value to point, we are using name of the struct Point before assigning the value. let point: Point = Point { x: 0, y: 1, z: 2 };
  2. Return type of functions new_point and new_point2 is Point.
  3. new_point returns via statement convention where as new_point2 returns via expression as a last line of the function.
  4. We need to mention the name, i.e. Point, of the struct before returning.

Just like tuples, you can de-structure the struct. Look at the below code snippet:

struct Point {
    x: i64,
    y: i64,
    z: i64
}

fn main() {
    let point: Point = Point { x: 0, y: 1, z: 2 };
    let point1: Point = new_point(3, 4, 5);
    let point2: Point = new_point2(6, 7, 8);

    let Point {x, y, z} = point;
    let Point {x, y: _, z} = point1;
    let Point {x, ..} = point2;
}

fn new_point(a: i64, b: i64, c: i64) -> Point {
    return Point { x: a, y: b, z: c };
}

fn new_point2(x: i64, y: i64, z: i64) -> Point {
    Point { x, y, z }
}

Observations:

  1. Ignoring a property while de-structuring, you need to use _ against the property name you wish to ignore.
  2. Ignoring all the other properties can be done using .. and you can focus on only the properties you are interested.

Like tuples, structs are also mutable using mut keyword. Refer to below code snippet:

struct Point {
    x: i64,
    y: i64,
    z: i64
}

fn main() {
    let mut point: Point = Point { x: 0, y: 1, z: 2 };

    point.x = point.x + 1;

    println!("Point x: {}", point.x);
}

To re-iterate, struct cannot be modified in structure while the code is running. You can make nested tuples and nested structs and tuples can contain structs and structs can contain tuples.

That’s it for structs in The Rust Programming Language. Please share 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.

Rust
Rust Programming Language
Rustlang
Programming
Webassembly
Recommended from ReadMedium