With the is_alphanumeric()
method, we can check if a character is alphanumeric in Rust. Characters that are said to be alphanumeric, are comprise of the twenty-six alphabets and the numbers 0
to 9
.
Characters that are not alphanumeric include punctuation characters like the comma,,
, and some mathematical characters like the plus sign, +
.
character.is_alphanumeric()
This method takes the parameter, character
, to check if it is alphanumeric.
A boolean value is returned. If the character is alphanumeric, then a true
is returned. Otherwise, false
is returned.
fn main(){// initialise some characterslet char1 = 'E';let char2 = '+';let char3 = '=';let char4 = '3';let char5 = '!';// check if characters are alphanumericprintln!("{}", char1.is_alphanumeric());println!("{}", char2.is_alphanumeric());println!("{}", char3.is_alphanumeric());println!("{}", char4.is_alphanumeric());println!("{}", char5.is_alphanumeric());}
In the code above,
is_alphanumeric()
method. Then we print the result to the console.