Rust provides several powerful collection types in its standard library, including
LinkedList, and of course
Vec (aka vector). This post will concern the lattermost.
Let's get started
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
vec.len(). You can pretend to be a stack with
vec.pop() (but only if it's
mutable). Yes, you can even access the value at the second index of a vector with good ol'
&str, you get to store
&str and only
&str. Thankfully, an intrepid Rust developer can easily get around this supposed limitation by using
Structs, both of which are awesome concepts and completely out of the scope of this article.
Creating your very own
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];
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:
Thanks for reading.