1. 26
    Rustlings enums2: Defining enum variants using classic, unit, and tuple structs
    1m 17s

Rustlings enums2: Defining enum variants using classic, unit, and tuple structs

Chris Biscardi
InstructorChris Biscardi
Share this video with your friends

Social Share Links

Send Tweet

README for this exercise.

Chris Biscardi: [0:00] In enums2, we have an empty enum called Message. We also have an impl Message that defines a function call(). This call() function, we use in our main body. The call() function takes an argument of self which is what allows us to call message.call.

[0:16] The list of messages that we're operating on, include Message: :Move, Message: :Echo, Message: :Changecolor, and Message: :Quit.

[0:23] In addition to the enums that we've defined before, a variant inside of an enum can be any struct. You can see that in the rust errors, we can see no variant named 'Move' for enum 'Message,' no variant or associated item 'Echo' for each of the variants that we're using.

[0:39] We first needed to find a variant Move that has an x which will make an u32 bit integer, and a y which is also u32 bit integer. These are arbitrary choices you can choose. Really, any integer type here. Echo, on the other hand, takes a {String}.

[0:54] ChangeColor takes three integers, which from context, we can infer to be the RGB channels and, say, an RGB declaration. In this case, we'll use u8 integers. Finally, we also have to define Quit which takes no arguments.

[1:09] As you can see in this enum, each of the variants can be defined to take additional arguments, whether those are Struct-like or single values.