Check out a free preview of the full The Rust Programming Language course

The "Structs" Lesson is part of the full, The Rust Programming Language course featured in this preview video. Here's what you'd learn in this lesson:

Richard demonstrates how to define structs in Rust, destructure a struct, ignore values in a struct, and demonstrates how to define mutable structs. Student's questions regarding what point is being mutated in the example, if the order of the struct matters, and if nested structs can be made are also covered in this segment.


Transcript from the "Structs" Lesson

>> So here we have that same point that we've done previously. But now I'm defining it using the struct keyword rather than using a tuple. So here I'm actually giving names to each of these elements. I'm saying this one is named x. This one's named y. This one's named z.

If I want too, I could construct one of these by making a function called new point, for example. I don't necessarily need to do a function but this is just an example. Let's say I take x, y and z as i64 arguments. And this is actually going to return a point.

The way that I build one of these structs. Now with tuples, I would just use parentheses there with a struct. You not only use curly braces instead of parentheses, but you actually need to say the name of the struct. So I actually have to write out point saying that's basically saying I wanna use this point struct right here, and curly braces.

Here, I'm saying x colon x, y colon y, z colon z. So that's to say that the x field here is gonna come from this x. The y is gonna come from for this argument. The z is gonna come from there. Also as a convenience, you can also write it this way.

So basically, if you just say x without the colon, that's syntax show that x colon, x y, same thing with y colon y, and z is syntax show for z colon z. So this is basically just a convenience way to do exactly the same thing as what we did.

So these two functions doexactly the same thing. And both of them will give you back a point struct with x, y, and z populated. So we can also get values out of a point. So let's say that I've defined my point like this. I can say x equals point dot x.

I can also destruct ructions, similar to what we did with tuples. So that looks like this, I say let and then again, I have to say the name of it point. And then x comma y, comma z. And now x, y and z will be local variables in scope.

And they'll correspond to point dot x, point dot y, and point dot z. So you can see that we have a pretty similar feature set between structs and tuples. The main difference is that with tuples, they're anonymous. You don't have to pick a name for them upfront, like you do a structs.

With structs, you have to pick a name, you have to use a struct keyword, to find the name. And when you're creating one of them, or when you're destructuring one of them, you have to use that same name. And in the case of tuples, they're accessed by position, rather than by name.

So with a tuple, it's dot 0, dot 1, dot 2. Whereas in struct, it's actually the fields are named. If we want to we can also use underscore and restructuring just like we did with tuple. And that looks a bit like this. We say y colon underscore rather than just having an underscore kind of hanging out there to say.

Is basically saying, I care about x and z, but not about y. And actually, if you want, there's also a dot dot syntax which basically means all the other fields pretend they're underscores. I don't care about them. I only care about x and z. However, many of the fields are on the struct.

I wanna completely ignore them and make them be underscores. And if you want, you can go as far as to just say, I only want, I only care about one field. And all the others are underscores. Okay, similarly to tuples, we can also make mutable structs. So here we're saying that, mut point equals and again defining point in the same way.

And then saying point dot x equals 5. So setting, changing that x after the fact mutating it. One other note about structs is that much like with tuples, you cannot change the number of fields in a struct at runtime. However many you've got in here. So in this case, 3, that's all you get for the entire life of the program.

It can never have 3, never more than 3. Because I've defined it to be exactly 3, that's all that you get. Can't add any new fields, can't change the names of the fields all of that is completely fixed. All I can do is just change the values that are associated with fields, like I did here.

Any questions on that before we move on?
>> So in that latest example, are you actually changing the original struct.
>> Yes.
>> Or are you just creating a new, x with a value of 5 on this new point?
>> Yeah, so that's a great question. In this example, we are actually mutating the original point.

So in this example, there's only one point. And it initially has an x of 1 and then we change that to be an x of 5. So we're not creating a new point when we do this, we're mutating the existing one..
>> Because these are named and not positional, that means that you.

If you were to like reverse your destructuring syntax or your assignment syntax, it still be applied to each variable here. Second to the last example you kind of did that. Yeah, right, because these are based on name rather than by order. Yeah, you can rearrange these however you want when you're destructuring.

And also when you're assigning, you don't have to assign x first. I happen to here but you can totally scramble these up, the order doesn't matter. At least as far as assigning and reading things out of them.
>> Can you make nested tuples or structs?
>> Sure, yeah you can make nested tuples and structs.

You can nest a struct inside another struct. You can nest a tuple inside a struct. The tuple inside a struct, inside a tuple. You can go totally as far as you want with that.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now