ENUMERATION
- It is a special type of data type that allows you group related values by their names.
- It is defined by the keyword enum.
- The difference between enum and struct is that enum has multiple variants whereas struct multiple fields,
- Enums enables you to build clearee types and decrease the number of illegal states that yout data can take.
- Enumerations are named via Pascal Case:
- The below code block shows a basic enumeration in rust:
enum Shape { Rectangle, Circle, Square }
- The above block shows a basic structure of enum which groups related data into a single compound type. It can used in main function in the given below format.
fn main() { let rectangle = Shape::Rectangle; }
- Another Enumeration example is given below:
enum Shape { Rectangle {width: f64, height: f64}, Circle {radius:f64, center:(f64, f64)}, Square {width:f64} }
- The below block shows how to call the enum created in the above code block
fn main() { let rectangle = Shape::Rectangle { width: 100.00, height: 200.00 }; }
- You can check if it two enum values are same by adding the PartialEq trait on top of enum:
#[derive[PartialEq)] enum Shape{ Rectangle {width: f64, height: f64} } fn main() { let rectangle = Shape::Rectangle { width: 100.00, height: 200.00 }; if rectange == Shape::Rectangle(width: 100.00, height: 200.00){ println!("It is the same Rectangle"); } }
- You can check if it it a rectangle by below code:
fn main() { let rectangle = Shape::Rectangle { width: 100.00, height: 200.00 }; if let Shapes::Rectangle {width, height } = rectangle { println!("It is a Rectange"); } }
- You can use match case to rewrite the above code in a much better format
fn main() { let rectangle = Shape::Rectangle { width: 100.00, height: 200.00 }; match rectangle { Shapes::Rectangle {width: f64, height: f64} => Println!("It is a Rectangle"), _ => println!("It is not a rectangle") } }
- You can add methods for enum like shown in the given below code block
struct Size { width: f64, height: f64 } enum Shapes{ Rectangle(f64, f64, Size), Circle(f64, f64, f64) } imple Shapes{ fn area(&self) -> f64{ match self{ Shapes::Rectangle(x: &f64, y: &f64, size: &Size) => size.width * size.height, Shapes::Circle(x: &f64, y: &f64,raduis &f64) => 3.14 * radius * radius, } } } fn main() { let rectangle = Shape::Rectangle { width: 100.00, height: 200.00 }; // match casing using if statement if let Shapes::Rectangle(x: f64, y: f64, Size{ width: f64, height: f64}) = rectangle { println!("It is a Rectangle") };
match rectangle { Shapes::Rectangle (x: f64, y:f64, Size{width: f64, height: f64}) => Println!("It is a Rectangle"), _ => println!("It is not a rectangle") } // calling method area let area = rectangle.area(); println!("Area: {}", area); }
- You can use match case as expression as given below:
fn main() { let rectangle = Shape::Rectangle { width: 100.00, height: 200.00 }; let width: f64 = match rectangle { Shapes::Rectangle {width: f64, height: f64} => width, _ => 0 } println!("Width of Rectangle: {}", width); }
Comments
Post a Comment