What is the simplest way to convert a string to upper case in Rust?

If you use the std::string::String type instead of &str, there is a less verbose way with the additional benefit of Unicode support:

fn main() {
    let test_str = "├╝bercode"; // type &str

    let uppercase_test_string = test_str.to_uppercase(); // type String

    let uppercase_test_str = uppercase_test_string.as_str(); // back to type &str

    println!{"{}", test_str};
    println!{"{}", uppercase_test_string};
    println!{"{}", uppercase_test_str};
}

Leave a Comment