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
Published 4 years ago
Updated 3 years ago

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.

egghead
egghead
~ 18 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today