When to use each of Array, Vector, Tuple, Struct, Enum and Union? please.

I am new to Rust,
What are the favorite situations to use each of Array, Vector, Tuple, Struct, Enum and Union? please.

Hi there, you should probably read a coding guide or the rust manual, but here’s a rundown:
Array
A continuous array of elements of immutable size. Defined as [type; size] like: [string; 3]
Vector
Basically an array, but with mutable size.
Example: let names = Vec::new() creates a new vector, then we can push items into it: names.push("retu")
Tuple
Basically a struct with unnamed members: a tuple contains any n number of elements, but doesn’t care about their types, but is not of mutable size.
Example: let user_data = ("retu", 1029384, User::new())
Tuples are usually used to return multiple items from a function and you access their members using a number similar to an array’s index: user_data.0, user_data.1 etc.
Struct
The basic data structure in rust. Defined like so:

struct MyStruct{/*my members*/}
struct MyStruct(/*member types*/);
struct MyStruct; //empty structure

Structures are a bit too complicated to talk about in just one post, so please take a look at the rust book.
Enum
A type of variable that can be any of the types declared within the enum. These again, are pretty complicated if we’re talking about rust enums, so please refer to the rust book.
Union
A type of variable that contains only one of the objects defined within it, and is the size of the largest object that it can contain. Probably not something you’ll use too often. Unless you’re interfacing with low-level libraries in C or C++

3 Likes

Please no: a tuple is basically the same as a struct, the only difference being that it and its fields are not named.

The definition as a “sort of fixed sized array” will be very familiar people coming from e.g. Python, where tuples are often used as just that. Then they will be thorougly confused why they can’t iterate over, or even get the length of, a tuple in Rust.

7 Likes

Though you would usually do that with an Enum, because an enum will actually store which of the possible types is currently held in an instance. Unions were only added because C has them and integrating with C code is easier now that rust has support for them, but it’s unsafe to read a union and other than interfacing with C there is no real reason to use them.

3 Likes

You can use unions to create memory efficient data structures, without even thinking about ffi.

1 Like

Good point, are any popular crates using it that way? Given OP's question though, i think it's rather unlikely they'd want to explore that option anytime soon.

1 Like

I haven't looked into the internals of many crates, so idk.

Yeah, that is true.

1 Like

thank you all

Can you think of a scenario where a union is considerably better than an enum?

… other than FFI where the union is required.

Unions can enable space optimizations, for example: https://github.com/servo/rust-smallvec/pull/94

3 Likes

Unions can be useful whenever the discriminant contains redundant information (for instance, if there is some other data available which effectively encodes the discriminant value.) Elimination of that redundancy could potentially save space or time, so unions are pretty much always an optimization or FFI concern.

1 Like