Updated June 2, 2023
Introduction to Rust enum
The Rust enum is one of the feature and it is a custom data type that contains some user-defined or pre-defined values which depends upon the requirement, and it is a keyword used before the name of the enumeration types. It contains some default methods for utilizing the enum attributes and their elements on the code the rust enum is more ever similar to the struct keyword and concepts. It has a list of pre-defined enums for each Enum with different types of options and statements for comparing the values with different datatypes in the Rust language.
Syntax:
In rust language, we use different keywords, variables, and methods for utilising the rust based applications more sophisticated, and it is mainly used for system programming language. It is mainly used for memory space allocations and error messages, and it especially supports memory safety and the minimum time required for performing the task.
enum enumName {
values;
}
fn main()
{
variable name = enumName:: values;
---some logics depends upon the requirement -----
}
The above codes are the basic syntax for creating and utilising the enumeration in Rust language. Depends upon the requirement, we need to use the enumeration in different areas and at different times.
How enum Function Work in Rust?
- Rust’s enumeration feature, also known as enums, allows the use of nominal enumerated types to create objects in the language. Enums can be used to create objects and perform pattern matching on user input values for the corresponding enumerated types. While using enum constructors, you can employ them for variable names or unnamed fields. When we compile the enums in Rust, it looks and same like other compiled languages like C, C++, etc.
- But compared to other languages, it has some other differences, like more powerful for compilation time for any other compiled languages. We also use some calls in Rust language that we have commonly referred to as mathematical functions like Algebraic features that can be related to the data types it supports background programming languages. If suppose we have not mentioned or declared the Enum values, then the discriminant can be chosen directly accessed on the fields. You can use casting with enumerations in Rust to convert integer types using the “as” operator and input values by numeric type.
Examples of Rust enum
Given below are the examples of Rust enum:
Example #1
Code:
#[derive(Debug)]
enum Demo {
First(String),Second(i32), Third(String),
}
fn main() {
let vars1 = Demo::First(String::from("Welcome To My Domain have a nice day user"));
let vars2 = Demo::Second(873256);
let vars3 = Demo::Third(String::from("Its a first example for Rust Programming language and it is moreever is mainly used for the system programming language and please provide your input values with specific formats "));
println!("{:?}",vars1);
println!("{:?}",vars2);
println!("{:?}",vars3);
match vars1 {
Demo::First(val)=> {
println!("{}",val);
print!("Its a first Value");
}
Demo::Second(val)=> {
println!("{}",val);
print!("Its a second Value");
}
Demo::Third(val)=> {
println!("{}",val);
print!("Its a third Value");
}
}
match vars2 {
Demo::First(val)=> {
println!("{}",val);
print!("Its a first Value");
}
Demo::Second(val)=> {
println!("{}",val);
print!("Its a second Value");
}
Demo::Third(val)=> {
println!("{}",val);
print!("Its a third Value");
}
}
match vars3 {
Demo::First(val)=> {
println!("{}",val);
print!("Its a first Value");
}
Demo::Second(val)=> {
println!("{}",val);
print!("Its a second Value");
}
Demo::Third(val)=> {
println!("{}",val);
print!("Its a thrid Value");
}
}
}
Output:
In the above example, we used single Enum in the language. With the help of the main function, we can access the enum variables, and also, using the match function, we can match the values.
Example #2
Code:
#[derive(Debug)]
enum Demo {
Vars1,Vars2, Vars3,Vars4,Vars5,
}
#[derive(Debug)]
struct Demo1 {
vars6:String,
vars7:Demo,
vars8:Demo,
vars9:Demo,
vars10:Demo,
vars11:Demo,
}
fn main() {
let vars12 = Demo1 {
vars6:String::from("Welcome To My Domain have a nice day user Its a Second example for Rust Programming language and it is moreever is mainly used for the system programming language and please provide your input values with specific formats "),
vars7:Demo::Vars1,
vars8:Demo::Vars2,
vars9:Demo::Vars3,
vars10:Demo::Vars4,
vars11:Demo::Vars5,
};
let vars13 = Demo1 {
vars6:String::from("Welcome To My Domain have a nice day user Its a Second example for Rust Programming language and it is moreever is mainly used for the system programming language and please provide your input values with specific formats"),
vars7:Demo::Vars2,
vars8:Demo::Vars1,
vars9:Demo::Vars3,
vars10:Demo::Vars4,
vars11:Demo::Vars5,
};
let vars14 = Demo1 {
vars6:String::from("Welcome To My Domain have a nice day user Its a Second example for Rust Programming language and it is moreever is mainly used for the system programming language and please provide your input values with specific formats"),
vars7:Demo::Vars2,
vars8:Demo::Vars1,
vars9:Demo::Vars3,
vars10:Demo::Vars4,
vars11:Demo::Vars5,
};
let vars15 = Demo1 {
vars6:String::from("Welcome To My Domain have a nice day user Its a Second example for Rust Programming language and it is moreever is mainly used for the system programming language and please provide your input values with specific formats"),
vars7:Demo::Vars2,
vars8:Demo::Vars1,
vars9:Demo::Vars3,
vars10:Demo::Vars4,
vars11:Demo::Vars5,
};
let vars16 = Demo1 {
vars6:String::from("Welcome To My Domain have a nice day user Its a Second example for Rust Programming language and it is moreever is mainly used for the system programming language and please provide your input values with specific formats"),
vars7:Demo::Vars2,
vars8:Demo::Vars1,
vars9:Demo::Vars3,
vars10:Demo::Vars4,
vars11:Demo::Vars5,
};
println!("{:?}",vars12);
println!("{:?}",vars13);
println!("{:?}",vars14);
println!("{:?}",vars15);
println!("{:?}",vars16);
}
Output:
In the second example, we used Enum and struct (structure) to utilize the user inputs differently. Struct is also the same as Enum when we use more than one enums, it is a better option.
Example #3
Code:
enum Demo {
First,
Second,
Third
}
fn demo1(vars:Demo) {
match vars {
Demo::First => {
println!("Welcome Its a first Output");
},
Demo::Second => {
println!("Welcome Its a second Output");
},
Demo::Third =>{
println!("Welcome Its a third Output");
}
}
}
fn main(){
demo1(Demo::Third);
demo1(Demo::First);
demo1(Demo::Second);
}
Output:
In the final example, we utilize a single Enum and employ a function for accessing the application. We can create a customized function for storing and retrieving the datas.
Conclusion
The Rust language uses default standard libraries for focusing the number of modules in the application. Each modules have a different set of features and methods with keywords. The Enum feature in Rust enables the declaration of variables with diverse data types, including primitives and non-primitives. Using the function and main function, we can call and utilize the enum types wherever required.
Recommended Articles
We hope that this EDUCBA information on “Rust enum” was beneficial to you. You can view EDUCBA’s recommended articles for more information.