37

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

1
  • 8
    Those C functions are locale dependent, while Rust is fixed to Unicode chars.
    – bluss
    Apr 26, 2015 at 11:50

1 Answer 1

50

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.