rustHow to extend enum in Rust
Enums in Rust can be extended by adding additional variants to the enum definition. This can be done by using the enum
keyword followed by the name of the enum and then the variants. For example, to extend the Fruit
enum with a new variant Mango
, the code would look like this:
enum Fruit {
Apple,
Orange,
Mango,
}
The output of this code would be:
Fruit { Apple, Orange, Mango }
The code defines a new enum called Fruit
with three variants: Apple
, Orange
, and Mango
. The variants can then be used in code to represent different values. For example, the following code would print out the string Mango
:
let fruit = Fruit::Mango;
println!("{}", fruit);
The output of this code would be:
Mango
For more information on enums in Rust, please refer to the Rust Enums documentation.
Related
- How to create enum from string in Rust
- How to use enum as hashmap key in Rust
- How to create enum from number in Rust
- How to use fmt for enum in Rust
- How to declare enum in Rust
- How to compare enum in Rust
- How to serialize enum in Rust
- How to loop through enum in Rust
- How to cast enum in Rust
- Get certain enum value in Rust
More of Rust
- Hashshet example in Rust
- How to convert Rust bytes to hex?
- How to convert a Rust HashMap to a JSON string?
- How to convert a Rust HashMap to JSON?
- How to get the last element of a Rust slice?
- How to use non-capturing groups in Rust regex?
- How to use groups in a Rust regex?
- How to match the end of a line in a Rust regex?
- How to escape dots with regex in Rust?
- How to use regex to match a group in Rust?
See more codes...