An overview of Vectors in Rust (for people who program in JavaScript)
Rust provides several powerful collection types in its standard library, including HashMap
, LinkedList
, and of course Vec
(aka vector). This post will concern the lattermost.
Let’s get started
Coming from a web development (aka JavaScript) background, my first stumbling block with understanding vectors in Rust was that Rust has a primitive type called array
. Don't be confused like I was, arrays in Rust are very different from our friendly neighborhood JavaScript arrays. The first clue is that arrays in Rust are one of two primitive compound types ( the other being tuple
), meaning that like all other primitive types in Rust, arrays are compiled to binary and stored on the stack at runtime. This means that arrays, similar to int
and &str
, have to be of a known, fixed length or size at compile time. This is obviously very different from arrays in JavaScript, which can grow at runtime. Now, before you throw out your copy of "The Book" and swear off of compiled languages for good, let me give you some good news: Rust has the arrays we know and love (sort of). As you may have guessed already, those "arrays" are called vectors. Vectors, like arrays in JavaScript, do not need to have a fixed length assigned to them at compile time, they can grow or shrink dynamically, and they come ready-made with some helpful utility methods built right in. You can check the length of a vector with vec.len()
. You can pretend to be a stack with vec.push(n)
and vec.pop()
(but only if it's mut
able). Yes, you can even access the value at the second index of a vector with good ol' vec[1]
. One key difference to note between JavaScript arrays and Rust vectors is that vectors can hold only one type of value. This is communicated by the full vector notation Vec<T>
(note the generic data type in '<>'). Basically, unlike JavaScript arrays which can hold any random mix of Numbers, Booleans, Strings or what have you, Rust's vectors can only hold a single type. If you want to store &str
, you get to store &str
and only &str
. Thankfully, an intrepid Rust developer can easily get around this supposed limitation by using Enums
and Structs
, both of which are awesome concepts and completely out of the scope of this article.
Creating your very own Vec
Now that we know a little bit about vectors and feel pretty warm and fuzzy about how much they resemble our tried and true JavaScript arrays, we can move on to the nitty gritty. The hard way to create a Vec
is to bind the result of Vec::new()
to a variable name, for example: let vec = Vec::new()
. However, since vectors are pretty much one of the most useful data types available to Rust developers, seasoned or otherwise, there's also vec!
(pronounced "Vec BANG") to aid us in our vector initialization endeavors. vec!
can be followed with some square brackets to both initialize and add values to our vector like so: let vec = vec![1, 2, 3];
Further reading
There are plenty of vector related topics not covered in this very brief overview, and the power of vectors in Rust can’t be understated. Below is a list of much higher quality content regarding vectors:
Official Rust Docs — std::vec::Vec
“The Book” — Storing Lists of Values with Vectors
Thanks for reading.
Originally published at tndl.io on November 28, 2018.