Skip to content
Tutorials Center
My learning

Topics

Rust Reference ยท Strings and Text

Rust char methods

Reference for Rust char methods: classification (is_alphabetic, is_numeric), to_digit, from_digit, from_u32, case conversion, len_utf8 and escapes.

A char is one Unicode scalar value, and the standard library gives it a large set of methods: tests that classify it, conversions to and from numbers, case mapping and helpers that describe its UTF-8 encoding. Most of them are const fn and take self by value, because char is Copy.

The classification methods follow Unicode, not just ASCII. 'รฉ'.is_alphabetic() is true, and 'ูฃ'.is_numeric() (an Arabic-Indic digit) is true too. When you want ASCII only, use the is_ascii_* family instead.

Rust
let c = 'q';
let upper = c.to_ascii_uppercase();
let digit = '7'.to_digit(10);

Try it

Rust Classifying characters

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 Numbers, cases and encoding

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
impl char {
    pub const MAX: char;
    pub fn is_alphabetic(self) -> bool;
    pub fn is_numeric(self) -> bool;
    pub fn is_alphanumeric(self) -> bool;
    pub fn is_whitespace(self) -> bool;
    pub fn is_uppercase(self) -> bool;
    pub fn is_lowercase(self) -> bool;
    pub fn is_control(self) -> bool;
    pub fn is_digit(self, radix: u32) -> bool;
    pub fn to_digit(self, radix: u32) -> Option<u32>;
    pub fn from_digit(num: u32, radix: u32) -> Option<char>;
    pub fn from_u32(i: u32) -> Option<char>;
    pub fn to_uppercase(self) -> ToUppercase;
    pub fn to_lowercase(self) -> ToLowercase;
    pub fn to_ascii_uppercase(&self) -> char;
    pub fn to_ascii_lowercase(&self) -> char;
    pub fn len_utf8(self) -> usize;
    pub fn len_utf16(self) -> usize;
    pub fn encode_utf8(self, dst: &mut [u8]) -> &mut str;
    pub fn is_ascii(&self) -> bool;
    pub fn eq_ignore_ascii_case(&self, other: &char) -> bool;
}
Name Description
radix The number base, from 2 to 36. Letters a-z (either case) stand for digits 10 to 35.
num For from_digit, a value that must be less than radix.
i A candidate code point for from_u32.
dst A byte buffer for encode_utf8; it must hold at least len_utf8() bytes.

Returns: bool for the is_* tests, Option for conversions that can fail, and iterators for Unicode case mapping.

Errors and panics: to_digit and from_digit panic if radix is greater than 36. encode_utf8 panics if the buffer is too small.

Usage notes

  • is_alphabetic vs is_ascii_alphabetic: the first accepts any Unicode letter, the second only a-z and A-Z. Use the ASCII version for protocol and identifier checks.
  • is_numeric is broader than digits. It accepts fractions, superscripts and digits from other scripts. For "is this a decimal digit I can convert", use to_digit(10) or is_ascii_digit.
  • to_digit accepts a radix. 'f'.to_digit(16) is Some(15). It is the safe way to parse one digit, with no subtraction from '0'.
  • Case mapping can change the length. to_uppercase() and to_lowercase() return iterators that implement Display, since 'รŸ' uppercases to two letters. Collect them or print them directly. The to_ascii_* versions return a single char and leave non-ASCII characters alone.
  • from_u32 rejects surrogates and values above char::MAX ('\u{10FFFF}'). Use it for untrusted numbers; char::from(u8) is infallible because every byte is a valid scalar value.
  • as u8 truncates, so 'โ‚ฌ' as u8 silently gives the wrong byte. Prefer u8::try_from(c) when the character may not fit.
  • A char is not a "character" in the visual sense. Combining marks and emoji sequences span several char values. See chars, char_indices, bytes for iteration.
  • Width in memory: size_of::<char>() is 4 bytes, but inside a String it takes len_utf8() bytes (1 to 4).