-
Book Overview & Buying
-
Table Of Contents
Learning Rust
By :
If are you are used to C, you will be well used to enumerations, for example:
enum myEnum {start = 4, next, nextone, lastone=999}; This creates an enum type that auto-fills next and nextone to be start + 1 and start + 2 respectively. If the first named parameter has nothing to give an initial value to, it is given the value 0 with everything after it being one larger than the last. They are accessed as myEnum.nextone.
An enum type in Rust has a very similar structure to a struct type, as shown in the following code:
enum MyEnum
{
TupleType(f32, i8, &str),
StructType { varone: i32, vartwo: f64 },
NewTypeTuple(i32),
SomeVarName
} As with C though, an enum is a single type, but the value of the enum can match any of its members.
Given the possibility of the contents of a Rust enum, you may be thinking that accessing one of the members within the enumeration may not be the simplest of tasks. Thankfully, it is, as an enum variable...