Check whether a char is a letter or a number?

14,428

is_alphabetic, is_digit, is_alphanumeric, is_numeric are what you are looking for.

e.g. :

fn main() {
    println!("1 is a digit {}", '1'.is_digit(10));
    println!("f is a hex digit {}", 'f'.is_digit(16));
    println!("a is alphabetic {}", 'a'.is_alphabetic());
    println!("こis alphabetic {}", 'こ'.is_alphabetic());
    println!("a is alphanumeric {}", 'a'.is_alphanumeric());
    println!("1 is alphanumeric {}", '1'.is_alphanumeric());
}

returns:

1 is a digit true
f is a hex digit true
a is alphabetic true
こis alphabetic true
a is alphanumeric true
1 is alphanumeric true

They are described in detail in the Rust standard library docs for chars.

Share:
14,428

Related videos on Youtube

unnamed_addr
Author by

unnamed_addr

Updated on November 28, 2022

Comments

  • unnamed_addr
    unnamed_addr 11 months

    What are the Rust equivalents to C's isalpha, isdigit and isalnum?

    • bluss
      bluss over 8 years
      Those C functions are locale dependent, while Rust is fixed to Unicode chars.