Rust Reference Β· Primitive Types
The Rust char type
Reference for Rust's char type: Unicode scalar values, 4-byte size, is_alphabetic, to_digit, from_u32, len_utf8, case conversion and escapes.
A char is a single Unicode scalar value: any code point from U+0000 to U+10FFFF except the surrogate range U+D800 to U+DFFF. It is always 4 bytes wide, independent of how many bytes the character takes in UTF-8. Character literals use single quotes ('a', 'Γ©', '\u{1F600}'), while string literals use double quotes.
A char is not a "user-perceived character". An accented letter written as two code points (e plus a combining accent) is two chars, and an emoji with modifiers can be many. Use chars() to iterate scalar values, and a grapheme-segmentation crate when you need what a human counts as one letter.
let letter = 'k'; let heart = '\u{2764}'; let tab = '\t';
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
let c: char = 'x'; let u = '\u{1F980}'; let b = b'x'; // a u8, not a char let from_num = char::from_u32(0x263A);
| Item | Description |
|---|---|
is_alphabetic, is_numeric, is_alphanumeric, is_lowercase, is_uppercase, is_whitespace, is_control |
Unicode-aware classification. |
is_ascii, is_ascii_digit, is_ascii_alphabetic, is_ascii_punctuation, is_ascii_hexdigit |
ASCII-only checks, cheap and predictable. |
to_digit(radix) |
Option<u32>; radix 2 to 36, panics if radix > 36. |
char::from_digit(d, radix) |
Option<char>. |
to_uppercase(), to_lowercase() |
Iterators of char (a single char may become several). |
to_ascii_uppercase(), to_ascii_lowercase() |
Single char, ASCII letters only. |
char::from_u32(n) |
Option<char>; None for surrogates and values above 0x10FFFF. |
len_utf8(), len_utf16(), encode_utf8(&mut [u8]) |
Encoded lengths and encoding. |
MAX, REPLACEMENT_CHARACTER |
Constants (U+10FFFF and U+FFFD). |
Panics: to_digit and from_digit panic for a radix above 36. char::from_u32_unchecked is unsafe.
Usage notes
as u8truncates;c as u8for'Γ©'is 233 (lossy, though valid Latin-1) while'β¬' as u8is 172. Useu8::try_from(c)to check that a char fits in one byte.- Only
u8converts tocharwithas. Others go throughchar::from_u32orchar::from_digit. charisCopy,OrdandHash. Ordering is by code point, which is not alphabetical for many languages.- Case mapping is not always one-to-one:
'Γ'.to_uppercase()yieldsSS. Compare text withto_lowercase()on strings, not on single chars, when correctness matters. is_alphabeticis Unicode;is_ascii_alphabeticonly covers A-Z and a-z. Pick on purpose when parsing identifiers.- Strings are not arrays of
char.Stringstores UTF-8 bytes, so there is nos[i]; usechars().nth(i)(O(n)) or byte slicing at known boundaries. - Format:
{}prints the character,{:?}quotes and escapes it ('\n').
Related
str,String, Unsigned integers,as- Lessons: Scalar types, Strings, Strings overview