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 the end of a line in a Rust regex?
- How to use regex to match a double quote in Rust?
- How to split a string with Rust regex?
- How to use regex to match a group in Rust?
- How to get a capture group using Rust regex?
- How to use regex with bytes in Rust?
- How to replace a capture group using Rust regex?
- How to perform matrix operations in Rust?
- How to parse JSON string in Rust?
- How to use a tuple as a key in a Rust HashMap?
See more codes...