rustHow do I capitalize a string in Rust?
To capitalize a string in Rust, you can use the to_uppercase()
method. This method is available on all String
types.
Example code
let my_string = "hello world";
let my_string_uppercase = my_string.to_uppercase();
Output example
HELLO WORLD
The code above takes a string, my_string
, and calls the to_uppercase()
method on it. This returns a new string, my_string_uppercase
, which is the original string with all characters capitalized.
Parts of the code:
let my_string = "hello world";
: This declares a variable,my_string
, and assigns it the value of the string"hello world"
.let my_string_uppercase = my_string.to_uppercase();
: This calls theto_uppercase()
method on themy_string
variable, and assigns the result to themy_string_uppercase
variable.
Helpful links
More of Rust
- How to get a capture group using Rust regex?
- How to use regex with bytes in Rust?
- How to parse JSON string in Rust?
- How to convert a Rust HashMap to a BTreeMap?
- How to use regex to match a group in Rust?
- How to replace a capture group using Rust regex?
- How to use regex to match a double quote in Rust?
- How to implement PartialEq for a Rust HashMap?
- How to use a tuple as a key in a Rust HashMap?
- How to convert a Rust HashMap to a JSON string?
See more codes...