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
.
相关问题
- Share Arc between closures
- Function references: expected bound lifetime param
- Pattern matching on slices
- How can I iteratively call Sha256::digest, passing
- Destructure a vector into variables and give away
相关文章
- How can I convert a f64 to f32 and get the closest
- What is a good way of cleaning up after a unit tes
- How can I unpack (destructure) elements from a vec
- How to import macros in Rust?
- How to get struct field names in Rust? [duplicate]
- Confusion between [T] and &[T]
- How do I initialize an opaque C struct when using
- What's the most idiomatic way to test two Opti
Explanation
ToUppercase
is anIterator
, that may yield more than onechar
. This is necessary, because some Unicode characters consist of multiple "Unicode Scalar Values" (which a Rustchar
represents).A nice example are the so called ligatures. Try this for example (on playground):
The 'fi' ligature is a single character whose uppercase version consists of two letters/characters.
Solution
There are multiple possibilities how to deal with that:
&str
: if your data is actually in string form, usestr::to_uppercase
which returns aString
which is easier to work with.std::ascii::AsciiExt::to_ascii_uppercase
which returns just achar
. But it only changes the letters'a'
to'z'
and ignores all other characters!String
orVec
like in the example above.ToUppercase
is an iterator,because there may be more that one uppercase version of some Unicode characterbecause 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:Then, you should collect those characters into a string, as ker pointed.