MOCKSTACKS
EN
Questions And Answers

More Tutorials








RUST Enum

Enumerations offer an easy way to work with sets of related constants. An enumeration, or Enum, is a symbolic name for a set of values. Enumerations are treated as data types, and you can use them to create sets of constants for use with variables and properties.

In Rust programming, when we have to select a value from a list of possible variants we
use enumeration data types. An enumerated type is declared using the enum keyword.

Example of RUST Enum


enum GenderCategory {
 Male,Female
}

fn main() {
let male = GenderCategory::Male;
let female = GenderCategory::Female;
println!("{:?}",male);
println!("{:?}",female);
}

Output of above code

Male
Female


Match function - Example of RUST Enum

RUST enum match function allow a logical check based on value see example below

enum GenderCategory {
 Male,Female
}

fn print_gender_message(gender:GenderCategory){
 match gender {
  GenderCategory::Male=> {
   println!("Gender is male");
  },
  GenderCategory::Female=> {
  println!("Gender is female");
  }
 }

fn main() {
 print_gender_message(GenderCategory::Male);
 print_gender_message(GenderCategory::Female);
}

Output of above code

Gender is male
Gender is female



Conclusion

In this page (written and validated by ) you learned about Rust Enum . What's Next? If you are interested in completing Rust tutorial, your next topic will be learning about: Rust Struct.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.