Skip to content
Tutorials Center
My learning

Topics

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.

Rust
let letter = 'k';
let heart = '\u{2764}';
let tab = '\t';

Try it

Rust Classifying and converting

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.

Rust Encoding and escapes

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

Rust
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 u8 truncates; c as u8 for 'Γ©' is 233 (lossy, though valid Latin-1) while '€' as u8 is 172. Use u8::try_from(c) to check that a char fits in one byte.
  • Only u8 converts to char with as. Others go through char::from_u32 or char::from_digit.
  • char is Copy, Ord and Hash. Ordering is by code point, which is not alphabetical for many languages.
  • Case mapping is not always one-to-one: 'ß'.to_uppercase() yields SS. Compare text with to_lowercase() on strings, not on single chars, when correctness matters.
  • is_alphabetic is Unicode; is_ascii_alphabetic only covers A-Z and a-z. Pick on purpose when parsing identifiers.
  • Strings are not arrays of char. String stores UTF-8 bytes, so there is no s[i]; use chars().nth(i) (O(n)) or byte slicing at known boundaries.
  • Format: {} prints the character, {:?} quotes and escapes it ('\n').