To check if a character is numeric in Rust, we use the is_numeric
method. This method returns a Boolean, true
or false
depending on whether the character is numeric. A numeric character is a numeral, for instance, 1, 2, 3
, and so on.
character.is_numeric()
character: This is the character we want to check if it is numeric or not.
A Boolean value is returned. This returned value is true
if the character in question is numeric. Otherwise, false
is returned.
fn main(){// create some characterslet char1 = 'E';let char2 = 'i';let char3 = '2';let char4 = '7';let char5 = '0';// check if characters are numericprintln!("{}", char1.is_numeric()); // falseprintln!("{}", char2.is_numeric()); // falseprintln!("{}", char3.is_numeric()); // trueprintln!("{}", char4.is_numeric()); // trueprintln!("{}", char5.is_numeric()); // true}
Lines 3-7: Here, we create some characters.
Lines 10-14: Here, we are checking if each character we created is numeric using the is_numeric()
method. Then we printed the result to the console.