Not entirely. In Rust, the runtime representation of enums (tagged unions) is left deliberately unspecified in order to allow for arbitrary optimizations depending on shape.
For example, The Option type in Rust (a.k.a. the Maybe type in Haskell) is a tagged union defined like this: [1]
enum Option<T> {
None,
Some(T)
}
Now say I use this like so:
let foo = Some(6);
The runtime represenation of `foo` will be as follows:
struct FooRepresentation {
discriminator: u8,
value: int
}
...where `discriminator` is the field that determines whether the value is `None` or `Some`, and in the latter case `value` will contain the associated data.
But let's say I declare foo a bit differently, using a pointer to a value rather than a raw value:
let foo = Some(~6); // the tilde denotes a unique pointer
Now the runtime representation of `foo` is only a single pointer, and in order to determine if `foo` is `None` it just checks to see if the pointer is null. Rust is smart enough to perform this optimization on any tagged union with two variants where only one of the variants has associated data, and the type of that variant is a pointer.
On the flipside, if you want to guarantee a specific data layout then you can use a struct, which have the same layout rules as structs in C.
For example, The Option type in Rust (a.k.a. the Maybe type in Haskell) is a tagged union defined like this: [1]
Now say I use this like so: The runtime represenation of `foo` will be as follows: ...where `discriminator` is the field that determines whether the value is `None` or `Some`, and in the latter case `value` will contain the associated data.But let's say I declare foo a bit differently, using a pointer to a value rather than a raw value:
Now the runtime representation of `foo` is only a single pointer, and in order to determine if `foo` is `None` it just checks to see if the pointer is null. Rust is smart enough to perform this optimization on any tagged union with two variants where only one of the variants has associated data, and the type of that variant is a pointer.On the flipside, if you want to guarantee a specific data layout then you can use a struct, which have the same layout rules as structs in C.
[1] https://github.com/mozilla/rust/blob/master/src/libstd/optio...