The Rust Programming Language

Collections Recap and Q&A

Richard Feldman

Richard Feldman

Vendr, Inc.
The Rust Programming Language

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

The "Collections Recap and Q&A" 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 provides a brief overview of the collection section in part 2 including tuples, structs, arrays, and memory. Students questions regarding if functions can be put in a struct, what kind of data iterable structure can be put in different types, if it's possible to make more dynamic arrays, and if structs labels or values can be iterated upon are also covered in this segment.

Preview
Close

Transcript from the "Collections Recap and Q&A" Lesson

[00:00:00]
>> So to summarize, we have tuple, structs, and arrays. Arrays are the only one of the three that can be iterated over, and tuples and structs cannot. And the reason for this is that arrays have to have a consistent element type. So when you're inside your for loop, each iteration of that loop is gonna have the same type for the element.

[00:00:15]
And that restriction is what allows them to be iterated over, but also it's a restriction that means you're not allowed to sort of mix and match the types like you can with tuples and structs. But in memory, regardless, if you have the same data in a tuple, a struct, or an array, they're all represented as exactly the same thing, just adjacent bytes with no extra metadata included.

[00:00:35]
Okay, let's review and then we'll pause for questions. So to sum up, we talked about tuples. So here's an example of defining a tuple. We can basically just use parentheses and commas, you can have tuples as small as two with the exception of unit, which is the empty tuple that's just open paren and closed paren.

[00:00:51]
And here we've said I've made a tuple that has an i64 followed by a bool. And I've defined the value of that to be one and then true. We can also do structs, which are similar to tuples, except you have to use the struct keyword to define it upfront.

[00:01:05]
So here I defined a struct named foo. I'm using labels to name the elements inside of this rather than using positions like we did with tuples. But otherwise they do exactly the same thing and they have the same trade offs. And arrays are similar to tuples in that they are positional, but they all have to have the same element type, so in this case u32.

[00:01:27]
Unlike arrays in most languages, they do have to have a fixed length. So in this case, this array has a fixed length of three, we'll talk about more flexible ones later on. And the advantage of that is of course that we can iterate over them. And finally we talked about the memory.

[00:01:40]
So u8, as we talked about in the previous section, is 8 bits which is to say 1 byte, u16 to 16 bits which is 2 bytes. But in either case, the memory is just all ones and zeros, it's really is the matter of how we're choosing to interpret those ones and zeros that determines what the program actually does.

[00:01:55]
>> Can I put functions in a struct?
>> There are ways to do it, but it might not be as straightforward as what you're used to, [LAUGH] I'll put it that way. We're not gonna do that in this course, but it is possible, essentially.
>> What kind of data iterable structure can I put in different types.

[00:02:12]
>> What kind of data iterable structure can I put in different types? So Rust does have a concept of iterables, meaning you can make a custom thing that is iterable. It's not just arrays that are iterable. That also gets into traits which we're not gonna cover in this introductory course.

[00:02:28]
But basically if you want to you're allowed to make up your own new iterable thing from scratch and define what iteration means in the context of that thing, and at which point it can be using for loops. So that's not something that's baked into the language and hard coded for arrays.

[00:02:42]
It's a more general concept that can be extended by you and is used in a number of ways throughout the standard library.
>> So based off of the fixed length of arrays, is it not possible to make say a dynamic array based off a length of a dictionary?

[00:03:00]
>> Yeah, so we'll talk about how to make more dynamic things than arrays in two sections from now, but you're on the right track. It is like we're using the same memory concepts, but we'll introduce the concept of a length that also gets stored in memory and stuff like that.

[00:03:16]
So using these same building blocks, we will do that later on. Will we cover iterating over structs labels and or values? So that can't be done, there's no way to iterate over a struct. If you want you could define your own custom iterator for your own custom struct, but it wouldn't be iterating over just the labels.

[00:03:32]
You'd have to figure out something else to do. So I know that can be done in a number of languages, but Rust is just not one of them. If you've got a struct or if you've got a tuple, you cannot iterate over the values or the fields.

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