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
More of Rust
- How to use regex captures in Rust?
- How to match whitespace with a regex in Rust?
- How to replace a capture group using Rust regex?
- How to parse a file with Rust regex?
- How to get size of pointer in Rust
- How to replace strings using Rust regex?
- How to split a string with Rust regex?
- How to use negation in Rust regex?
- Regex example to match multiline string in Rust?
- How to use regex lookahead in Rust?
See more codes...