Structs and enums are similar to each other.
When would it be better to use a struct as opposed to an enum (or vice-versa)? Can someone give a clear example where using a struct is preferable to using an enum?
Structs and enums are similar to each other.
When would it be better to use a struct as opposed to an enum (or vice-versa)? Can someone give a clear example where using a struct is preferable to using an enum?
An
Enum
is a type with a constrained set of values.You can store data in the Enum if you need it.
A struct is a way to represent a thing.
Now you can make new
Window
objects that represent a window on your screen.Perhaps the easiest way to explain the fundamental difference is that an enum contains "variants", of which you can only ever have one at a time, whereas a struct contains one or more fields, all of which you must have.
So you might use an
enum
to model something like an error code, where you can only ever have one at a time:Enum variants can contain values if needed. For example, we could add a case to
ErrorCode
like so:In this case, an instance of
ErrorCode::BadHTTPCode
always contains au16
.This makes each individual variant behave kind of like either a tuple struct or unit struct:
However, the advantage of writing them as enum variants is that each of the cases of
ErrorCode
can be stored in a value of typeErrorCode
, as below (this would not be possible with unrelated structs).You can then
match
on the enum to determine which variant you've been given, and perform different actions depending on which one it is.By contrast, the third type of struct that I didn't mention above is the most commonly used - it's the type of struct that everyone is referring to when they simply say "struct".
Note that in that case, in order to create a
Response
, all of its fields must be given values.Also, the way that the value of
response
is created (i.e.HTTPResponse::Something
) implies thatHTTPResponse
is an enum. It might look something like this:Enums have multiple possibilities. Structs have only one possible "type" of thing they can be. Mathematically, we say a struct is a product type and an enum is a sum of products. If you only have one possibility, use a struct. For example, a point in space is always going to be three numbers. It's never going to be a string, or a function, or something else. So it should be a struct containing three numbers. On the other hand, if you're building a mathematical expression, it could be (for instance) a number or two expressions joined by an operator. It has multiple possibilities, so it should be an enum.
In short, if a struct works, use a struct. Rust can optimize around it, and it's going to be clearer to anyone reading your code what the value is supposed to be treated as.