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 for before the name of the enumeration types. It contains some default methods for utilising the enum attributes and its elements on the code the rust enum is more ever similar to the struct keyword and concepts. It has some list of pre-defined enums for each enum has different type of options and statements for to comparing the values with different datatypes on 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, error messages and mainly it supports memory safety and 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 different times.
How enum Function Work in Rust?
- The Rust enumeration is the feature which is same and parallelly we used for the nominal enumerated types as similar as the set of constructors for creating the objects in the language. In enum side we can used for to create or some pattern matching for the user input values of the corresponding enumerated types. While we used enum constructors it can be used for either named or unnamed fields of the variables. When we compile the enums in Rust it looks and same like other compiled languages like C, C++ etc.
- But when we compare 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 is more ever we have commonly referred as mathematical functions like Algebraic features that can be related with the data types it supports background programming languages. If suppose we have not mention or declare the values in the enum then the discriminant can be chosen directly accessed on the fields. It can be used for casting the enumerations for to convert the integer types with the operator like “as” and the 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 main function we can access the enum variables and also using 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 second example we used enum and struct (structure) for utilising the user inputs in different ways. Struct also same like enum when we use more than one enums it is more 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 final example we used single enum and function is used for accessing the application. We can create the 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. For each modules have different set of features, methods with keywords. Like that Enum is one of the feature it is used for declaring the variables with different data types like primitive and non-primitive type. By using function and main function we can call and utilize the enum types wherever it requires.
Recommended Articles
This is a guide to Rust enum. Here we discuss the introduction, how enum function work in rust? and examples respectively. You may also have a look at the following articles to learn more –
41 Online Courses | 13 Hands-on Projects | 322+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses