Rust Reference · Strings and Text
Rust to_uppercase and to_lowercase
Reference for Rust to_uppercase, to_lowercase, to_ascii_uppercase, make_ascii_lowercase and eq_ignore_ascii_case: Unicode rules, ß, and capitalizing words.
Rust has two families of case methods. to_uppercase and to_lowercase follow the Unicode rules and return a new String. The ascii variants (to_ascii_uppercase, to_ascii_lowercase, make_ascii_uppercase, make_ascii_lowercase) change only the letters A-Z and a-z and leave every other character alone, which makes them faster and allows in-place changes.
Unicode case conversion can change the length of the text: the German ß becomes SS in uppercase. That is why to_uppercase returns an owned String and why char::to_uppercase returns an iterator rather than a single char.
assert_eq!("Hello".to_uppercase(), "HELLO"); assert_eq!("Hello".to_ascii_lowercase(), "hello");
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input · what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input · what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Syntax
fn to_uppercase(&self) -> String fn to_lowercase(&self) -> String fn to_ascii_uppercase(&self) -> String fn to_ascii_lowercase(&self) -> String fn make_ascii_uppercase(&mut self) fn make_ascii_lowercase(&mut self) fn eq_ignore_ascii_case(&self, other: &str) -> bool
| Name | Description |
|---|---|
other |
The string to compare with, for eq_ignore_ascii_case. |
Returns: the to_* methods return a new String; the make_ascii_* methods change the string in place (they are available on str, so on String and &mut str); eq_ignore_ascii_case returns bool.
Errors / panics / edge cases: nothing panics. Characters without case (digits, punctuation, most CJK) are copied unchanged. For char, to_uppercase() and to_lowercase() return iterators (ToUppercase, ToLowercase) that can be printed directly or collected.
Usage notes
- Length can change.
"straße".to_uppercase()is"STRASSE": six characters become seven, so positions in the original do not apply to the result. - Not locale-aware. Rust uses the default Unicode mappings. Turkish dotted and dotless
i, for example, need a library such as ICU. - Final sigma is handled by
to_lowercasefor whole strings, but not bychar::to_lowercase, which sees one character in isolation. - Use ASCII methods for protocol data (header names, hex digits, file extensions). They are faster, never change the length and behave the same on every machine.
eq_ignore_ascii_caseonly folds ASCII, so"Ärger"and"ärger"are different. For full Unicode case-insensitive comparison, compare lowercase copies, which is an approximation (true case folding needs a crate).to_lowercaseallocates on every call. Cache the result when you compare against many values.- Sorting case-insensitively: use
sort_by_key(|s| s.to_lowercase()). - There is no built-in
capitalize. Build it fromchars().next()as above.
Related
charmethods,chars,replace,String- Lessons: Strings, Strings overview