Rust Arrays and Vectors

Hello,

I'm new to Rust and have been looking a little bit over it on the Rust By Example website. I've got a question regarding this piece of code I wrote:

fn main() {
    let mut array = [];
    array.push("Hello");

    println!("{:?}", array);
}

I know there's an error with it due to the array variable, but I don't understand why I need to use a vector. I come from Python so stuff was easier so I am struggling quite a little with this array stuff and the new concept vector.

Regards,
Hugo.

the size (length) of an array can't be changes after it's created. The equivalent to pythons list in rust is called Vec. You can push to a vec, but not an array.

edit: arrays are useful when you know at compile-time how many values there are, like [r, g, b] or [x, y] coordinates for example.

2 Likes

array

A fixed-size array, denoted [T; N], for the element type, T, and the non-negative compile-time constant size, N.

Vec

A contiguous growable array type, written as Vec<T>, short for ‘vector’.


If you meant to ask why there's a non-growable type then that's because knowing a size at compile-time enables a bunch of things that can't be done with a vec. An array's data can live on the stack while the vec's data always goes to the heap. It can also be embedded in another struct. This avoids pointer indirections and allocator overhead.

It also means carrying less metadata around. A vec needs to keep track of its size and capacity at runtime. The array doesn't because that's already known at compile-time.

So they serve different purposes.

And why there's no push/append method? Because that would be inefficient or even impossible. If an array is inside a struct you can't grow it in-place since there's no additional room inside the struct. You'd have to disassemble the struct, put the array's values into a different type that allows one more element and then also add that additional element. This would be expensive, and would require that the struct holding the array is generic over the array length.

5 Likes

Thank you!

Thank you very much!

I'm not sure how much you're familiar with outside of Python, but overall, it's going to be important for you to familiarize yourself with some sort of introductory-level machine programming concepts such as RAM and memory addresses / pointers, the call stack versus the heap allocator, and the difference between languages such as Python which have dynamic garbage collectors based on pausing the program and doing a graph search to find inaccessible objects, and languages like Rust which don't.

If you don't understand these things, the different concepts in Rust such as arrays versus vectors will seem like a bunch of weird rules made up arbitrarily that you have to memorize. But if you develop a basic mental model of what's actually going on in RAM, then you'll realize that the different ways you can do something in Rust are filling different points in the space of tradeoffs here in a very natural way, and it will make more sense. This also applies to C.

6 Likes