Vec

The Vec data type stores a vector, which is a word commonly used in programming to indicate a one-dimensional, variable-size array. Like actual arrays, they can store multiple data values, as long as those data values all have the same data type. Like Strings, Vecs can change size, and so they are specialized smart pointers that store their contained values on the heap.

To create an empty Vec, we can use Vec::new(), like so:

let mut vector = Vec::new();

Then we can append a data value to it using push:

vector.push(1.5);

Now, so far, we haven't said a word about what type of data the vector can contain, and Rust is perfectly happy because we didn't need to. Everything we wrote is consistent with the vector containing one of the floating point primitive data types, so that's what Rust figures it contains.

What happens if we do something that isn't consistent, such as try to store an &str in the vector?

vector.push("nope");

Now Rust can't figure out what data type the vector is supposed to contain, so it refuses to compile:

However, we can do something like this:

let x: f64 = 99.9;
vector.push(x);

Here, we've got a variable named x that has f64 for its data type. That's compatible with the "some kind of floating point number" that Rust was able to figure out before, so adding it to the vector doesn't cause any problems. In fact, it tells Rust that our earlier 1.5 should be treated as an f64 too, and that the vector contains f64 values, specifically.

We used numbers for that example, but Rust can store any data type in a Vec, as long is we follow the rule of only one data type per vector.

Adding an &str to our vector of numbers was a problem, but we can create a vector of &str without any trouble:

let mut second_vector = Vec::new();
second_vector.push("This");
second_vector.push("works");
second_vector.push("fine");

We can access elements contained in a vector using the same syntax we would use for an array:

    println!("{} {} {}.", second_vector[0], second_vector[1], second_vector[2]);

Vec implements a number of functions for accessing the stored data values, such as the following:

  • pop, which removes and returns the last item in the vector
  • remove, which removes the item at a specific index
  • insert, which adds an item at a specific index, pushing the item that was at that index and everything after it back one
  • append, which moves values out of another vector and adds them at the end
  • len, which just tells us how many items are in the vector
  • iter, which returns an iterator for the contained data

Creating an empty vector and then pushing a bunch of values to it can get a little bit tedious, so there's a macro to make things easier:

let third_vector = vec!["This", "works", "too"];

We recognize the macro by its ! as always, but this time it's not really pretending to be a function. Instead, it almost looks like a prefixed array expression. Macros have a lot of flexibility about how they look, and for this one, looking similar to an array expression makes sense. The end result of this is just like we'd created a vector with new and then added information to it with push. It's just a more convenient way of writing the same thing.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.74.231