rustGet all enum values in Rust
In Rust, enum values can be accessed using the variants()
method. This method returns an iterator over all the variants of the enum. To get all the enum values, the iterator can be collected into a vector. The ## Code example below shows how to do this:
enum Color {
Red,
Green,
Blue,
}
fn main() {
let colors: Vec<Color> = Color::variants().collect();
println!("{:?}", colors);
}
The output of this code is:
[Red, Green, Blue]
The variants()
method returns an iterator over all the variants of the enum. This iterator is then collected into a vector, which is then printed out. The {:?}
format specifier is used to print out the vector in a readable format.
Helpful links
Related
- How to uppercase enum in Rust
- Enum as string in Rust
- How to use enum as hashmap key in Rust
- How to create enum from string in Rust
- How to print enum in Rust
- Using enum match in Rust
- How to get enum len in Rust
- How to get all enum values in Rust
- How to use fmt for enum in Rust
- How to display enum in Rust
More of Rust
- How to parse JSON string in Rust?
- How to match whitespace with a regex in Rust?
- How to replace strings using Rust regex?
- How to split a string with Rust regex?
- How to escape dots with regex in Rust?
- How to use regex lookahead in Rust?
- How to use look behind in regex in Rust?
- How to perform matrix operations in Rust?
- How to add matrices in Rust?
- How to get a value by key from JSON in Rust?
See more codes...