To convert the first character in a string to uppercase in Rust, you can use methods such as to_uppercase()
and string manipulation techniques.
In this example,
str1
with a value of "hello world"
.str1
and convert it to uppercase using the to_uppercase()
method.fn main() {
let mut str1 = String::from("hello world");
let first_char = str1.chars().next().unwrap();
str1.replace_range(0..1, &first_char.to_uppercase());
println!("Modified string is: {}", str1);
}
Modified string is: Hello world
In this example,
str2
with a value of "rust"
.str2
and convert it to uppercase using the to_uppercase()
method.fn main() {
let mut str2 = String::from("rust");
let first_char = str2.chars().next().unwrap();
str2.replace_range(0..1, &first_char.to_uppercase());
println!("Modified string is: {}", str2);
}
Modified string is: Rust
In this tutorial, we learned How to convert First Character in String to Uppercase in Rust language with well detailed examples.