rustHow to uppercase enum in Rust
To uppercase an enum in Rust, you can use the to_uppercase()
method on the enum's Variant
type. For example, if you have an enum called MyEnum
with variants Variant1
and Variant2
, you can uppercase them like this:
let my_enum = MyEnum::Variant1;
let uppercase_enum = my_enum.variant().to_uppercase();
This will set uppercase_enum
to VARIANT1
.
The to_uppercase()
method is part of the std::string::ToString
trait, which is implemented for all Variant
types. This means that you can use to_uppercase()
on any enum variant.
Example ### Output
uppercase_enum = VARIANT1
Explanation
The let my_enum = MyEnum::Variant1;
line creates a new MyEnum
enum with the Variant1
variant. The let uppercase_enum = my_enum.variant().to_uppercase();
line then calls the to_uppercase()
method on the Variant1
variant, which returns the uppercase version of the variant.
Helpful links
Related
More of Rust
- How to match whitespace with a regex in Rust?
- How to replace strings using Rust regex?
- How to get a capture group using Rust regex?
- How to get an element from a HashSet in Rust?
- How to replace a capture group using Rust regex?
- How to replace all matches using Rust regex?
- How to split a string with Rust regex?
- How to declare a matrix in Rust?
- Bitwise operator example in Rust
- How do I split strings in Rust?
See more codes...