An enum in Rust is a custom data type that represents data that can be anyone among several possible variants. Each variant in the enum can optionally have data associated with it. An enumerated type is defined using the enum keyword before the name of the enumeration. It also consists of methods.
Syntax:
enum name_of_enum{
variant_1,
variant_2,
.
.
variant_n
}
Here the name_of_enum is the name given to the Enum and variant_1, variant_2, ...... variant_n are the values of the enumeration.
Example:
enum Month_name{
January,
February,
March,
April,
May,
June,
July,
August,
September,
October,
November,
December
}
In the above example, Month_name is the name of enum, and January, February, March, April, May, June, July, August, September, October, November, December are values of an enum. A value of the enum can match any of the variants. For this reason, an enum is sometimes called a ‘sum type’: the set of possible values of the enum is the sum of the sets of possible values for each variant.
Let's create the instance of each of the variants. It looks like:
let jan = Month_name :: January;
let feb = Month_name :: February;
let mar = Month_name :: March;
let apr = Month_name :: April;
let may = Month_name :: May;
let jun = Month_name :: June;
let jul = Month_name :: July;
let aug = Month_name :: August;
let sep = Month_name :: September;
let oct = Month_name :: October;
let nov = Month_name :: November;
let dec = Month_name :: December;
Each variant of the enum has been namespaced under its identifier, and a double colon is used.
Let's look at the code.
Rust
// The `derive` attribute automatically creates the implementation
// required to make this `enum` printable with `fmt::Debug`.
#[derive(Debug)]
enum Month_name {
January,
February,
March,
April,
May,
June,
July,
August,
September,
October,
November,
December
}
fn main() {
let jan = Month_name :: January;
let feb = Month_name :: February;
let mar = Month_name :: March;
let apr = Month_name :: April;
let may = Month_name :: May;
let jun = Month_name :: June;
let jul = Month_name :: July;
let aug = Month_name :: August;
let sep = Month_name :: September;
let oct = Month_name :: October;
let nov = Month_name :: November;
let dec = Month_name :: December;
println!("{:?}",jan);
println!("{:?}",feb);
println!("{:?}",dec);
}
Output:
January
February
December
In the above example, Month_name is the name of the enumeration. Here jan, feb and dec are names of the month representing variations of the enum Month_name for January, February, and December respectively.
Similar Reads
Rust - Lifetime
Lifetimes in Rust refer to the lifetime of how long a variable would live. Lifetimes are associated with references as references cannot live longer than the object from which it is derived. Lifetimes basically describe the scope that a reference is valid for. Lifetimes come into play when we are de
3 min read
Rust - Generics
Generics in Rust is a method of generalizing data types. Generics are always defined at run time. As generics can have multiple forms over given parameters and can be applied to methods, functions, structures, traits, etc they are often referred to as parametric polymorphism in type theory. Generics
2 min read
Rust - Comments
In Rust programming, a comment is a programmer-readable explanation or annotation in the source code of a computer program. Comments are statements that are not executed by the compiler and interpreter. To write comments in Rust, we have two types of comments as in any other programming language. No
2 min read
Rust Vs OpenCV
Rust and OpenCV are both powerful tools in the world of software development, but they serve different purposes and excel in distinct areas. Rust Vs OpenCVThis article will provide a comprehensive comparison of Rust and OpenCV, highlighting their strengths, use cases, and key features.What is Rust?R
4 min read
Rust - Bounds
In Rust, we have a concept of Bounds. Bounds are used in situations when we are working with generics and are trying to figure out the nature and type of parameters that are required for stipulating the functionality that the type in Rust implements. Syntax: fn gfg <T: GeeksforGeeks>(x: T) {
2 min read
Rust - Casting
Type Conversion or casting in Rust is a way to convert one type to another. As its name suggests, type conversion is the process of converting variables from one data type to another. So that, the compiler treats the variable as a new datatype. Rust doesn't allow us to implicitly convert the datatyp
3 min read
Rust - Result Type
In Rust, we have a result-type operator that is used for error-handling operations with the result-type operator. The Result type is wrapped in an enum that has two values - Ok and Err. Ok (T) denotes success while Err(E) is used for representing errors and error values (if present). While working
3 min read
File I/O in Rust
File I/O in Rust enables us to read from and write to files on our computer. To write or read or write in a file, we need to import modules (like libraries in C++) that hold the necessary functions. This article focuses on discussing File I/O in Rust. Methods for File I/O Given below are some method
7 min read
Rust - Conventions
In Rust, there are certain conventions that need to be followed when we are creating dependencies. There are dependencies on libraries in Rust. Cargo! is a dependency manager in Rust that is similar to npm (in JavaScript) and pip (in Python). All the project dependencies are handled by cargo. For in
2 min read
Rust - Literals
A literal is a source code that represents a fixed value and can be represented in the code without the need for computation. The compiler uses by default i32 for integers and f64 for float types/. In Rust, literals are described by adding them in the type as a suffix. Example:  Integer literal 6 h
1 min read