Check out a free preview of the full The Rust Programming Language course
The "Arrays" 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 discusses how to define an array in Rust, how to access values in an array, how to redefine values, and how to iterate over an array. Arrays in must all have the same type and a hardcoded length at compile time. A brief comparison of arrays and tuples is also discussed in this segment.
Transcript from the "Arrays" Lesson
[00:00:00]
>> Now, let's move on to arrays, now, this is an example of an array in rust and arrays in rust are pretty different from how you might think of arrays in any other language views unless specifically. You've used c because rust arrays are more like C arrays and they are arrays in like JavaScript Python Ruby.
[00:00:15]
Most languages that use the term arrays means something different than what they mean in rust or in C. Here's what I mean, so here we have an array of years, there are three years in this array 1985, 2000, 2005. See, we have the square bracket syntax, which is the traditional array syntax from C and we have the type, so this says i32 semi colon three.
[00:00:37]
Now, what's interesting about this is that this is saying All of the elements in this array are i32. So unlike tuples, and structs, you cannot mix and match you have to have one consistent type for all of your elements. And the second thing and this is where rust arrays differ from arrays in most other languages you might be familiar with, arrays have a hard coded length at compile time.
[00:00:56]
So these three is part of the type of this array, it says, this array, always has exactly three elements, it can never have more than three, it can never have fewer than three. Now, there is a way to get a similar collection, it's probably more like what you're familiar with where at that can't grow and shrink, we're getting that in the future section.
[00:01:11]
But for arrays, this actually much like with structs and with tuples, what you see is what you get, you get exactly three elements forever with this array called years. So that's sort of a, arrays are fixed length in rust, now, if I want, I can say I want to get the first year out of this, I can say years bracket zero, so that's going to give me back 1995.
[00:01:32]
I can also do destructuring, here like this with the square brackets and underscoring the appropriate positions of notes, I can also put variables in here instead of zero. So I could say, years bracket x, years bracket y, if that, if I put something that's out of bounds or like I say, years bracket foo.
[00:01:48]
And it's outside the bounds of this array, that will panic at runtime, so don't do that but if we're using it with hard coded numbers like this, then. It's basically gonna work the same way as a tuple would because, I've defined this as mutable, I can also reassign this year's bracket to equals 2010.
[00:02:04]
And again, I could also put yours bracket x or yours bracket foo equals 2010 if I wanted to, so this is an example of that, both of these do essentially the same thing. Worth noting that, yeah, in this case, this will never pack it, this will never panic, because rust would give me a compile time error.
[00:02:20]
If I put in a hard coded number that's outside the bounds of the array, rust says, okay, I know that's gonna be a problem but if I do with x, rust necessarily know what next is going to be. So it'll do a runtime check instead and potentially panic if I do something that's outside the bounds of the array, so be careful with doing that.
[00:02:38]
One thing you can do with arrays that you cannot do with tuples and structures, you can iterate over them, so here's how we do that, I'd say four year in years dot enter. This is a method call, we haven't gotten into methods yet, we'll talk about them in the future, but their syntax is probably familiar to you depending on what other languages you've used.
[00:02:55]
And here, this is basically going to do a loop where, each time we go through one of the elements in this array, it's gonna run this line of code. So this print line that says next year colon and then year plus one, so first it'll say next year 1996, cuz that's 1995 plus one, then next year 2001 then next year 2006.
[00:03:14]
So this is something you do with arrays, you cannot iterate over tuples and structs though, okay, so in summary, some of the differences between arrays and tuples. So here we have our array, we can iterate over it, it's got a fixed type to it, so all of the elements in the array have to have this type and it's got a hard coded length.
[00:03:37]
And this sort of will illustrate why we're allowed to iterate over arrays but not over tuples, so because we have this hard coded type for all the elements. That means that, rust knows every single time we go into this loop, I know Know exactly what the type of error is, it's i32.
[00:03:52]
It has to be because every single of these elements has to be an i32 and that's good because rust. As we've discussed previously, wants to know the exact type for every single thing and every single thing has to have only one type. There's no such thing as may be i32, may be string, rust doesn't do that, it's always got to be one type, so because of this, it knows.
[00:04:09]
Okay, no matter how many elements we decided to hard code, there always gonna be i32s there for year when I run this code is always gonna be i32. Now, in contrast, if we had a tuple here, remember tuples you can mix and match, you can say i32, i32 bool, well, I mean, you can imagine when we're going through the loop.
[00:04:27]
What is the type of year like the first two iterations of loopy i32, the third time it would be bool, what's bool plus one, that's not really a thing. So this would give you a compile error, if you tried to do this, it would say, no, you can't iterate over tuples, you can only iterate over arrays or the things that we'll talk about later.
[00:04:44]
Okay, so in summary, arrays can be iterated over tuples and structs cannot but the downside of that. So the trade off is that array elements must all have the same type, which is both what allows them to be iterated over but also what makes them less flexible than tuples and structs.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops