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.
let c = 'q'; let upper = c.to_ascii_uppercase(); let digit = '7'.to_digit(10);
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
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_alphabeticvsis_ascii_alphabetic: the first accepts any Unicode letter, the second onlya-zandA-Z. Use the ASCII version for protocol and identifier checks.is_numericis broader than digits. It accepts fractions, superscripts and digits from other scripts. For "is this a decimal digit I can convert", useto_digit(10)oris_ascii_digit.to_digitaccepts a radix.'f'.to_digit(16)isSome(15). It is the safe way to parse one digit, with no subtraction from'0'.- Case mapping can change the length.
to_uppercase()andto_lowercase()return iterators that implementDisplay, since'ร'uppercases to two letters. Collect them or print them directly. Theto_ascii_*versions return a singlecharand leave non-ASCII characters alone. from_u32rejects surrogates and values abovechar::MAX('\u{10FFFF}'). Use it for untrusted numbers;char::from(u8)is infallible because every byte is a valid scalar value.as u8truncates, so'โฌ' as u8silently gives the wrong byte. Preferu8::try_from(c)when the character may not fit.- A
charis not a "character" in the visual sense. Combining marks and emoji sequences span severalcharvalues. Seechars,char_indices,bytesfor iteration. - Width in memory:
size_of::<char>()is 4 bytes, but inside aStringit takeslen_utf8()bytes (1 to 4).
Related
char,str,chars, char_indices, bytes,std::char- Lessons: Scalar types, Strings