rust

Convert a char to upper case


I have a variable which contains a single char. I want to convert this char to upper case. However, the to_uppercase function returns a rustc_unicode::char::ToUppercase struct instead of a char.


Solution

  • ToUppercase is an iterator, because the uppercase version of the character may be composed of several codepoints, as delnan pointed in the comments. You can convert that to a Vector of characters:

    c.to_uppercase().collect::<Vec<_>>();
    

    Then, you should collect those characters into a string, as ker pointed.