Enums

Last updated

What is Enums?

An enum, short for enumeration, is a custom data type that allows you to define a type by enumerating (listing out one-by-one) all of its possible variants. In Rust, if something is one of a given set of possibilities (e.g., Rock or Paper or Scissors), it's probably appropriate to represent that data with an enum, like so: enum RpsChoice { Rock, Paper, Scissors }.

An instance of an enum can be one and only one of the enum's declared variants at any given time. Unlike enumerations in some other languages, variants in Rust are not restricted to a singular data type. When you define an enum, you can decide for each of its possible variants whether or not that variant will hold additional embedded data; each variant of the enum is also allowed to hold data of completely different types and amounts.

Free resources to learn Enums

More in stage 3: Error Handling

Enums: common questions

Can I learn Enums in Hindi?

This lesson is written in English. Once you sign in, open it in the interactive course and ask the AI tutor to explain Enums in Hindi or Hinglish. The tutor answers from this lesson’s own text, so it stays on the syllabus, and it runs on your own Google, OpenAI or Anthropic key.

Is this Enums tutorial for beginners?

It is lesson 16 of 122 in the free Rust roadmap, in stage 3, Error Handling. If you are new to Rust, start from stage 1 and come back here in order; if you already know the earlier stages, this lesson stands on its own.

Still confused? Ask me