How to check if a character in Rust is numeric

Overview

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.

Syntax

character.is_numeric()
The syntax for the is_numeric() method

Parameters

character: This is the character we want to check if it is numeric or not.

Return value

A Boolean value is returned. This returned value is true if the character in question is numeric. Otherwise, false is returned.

Example

fn main(){
// create some characters
let char1 = 'E';
let char2 = 'i';
let char3 = '2';
let char4 = '7';
let char5 = '0';
// check if characters are numeric
println!("{}", char1.is_numeric()); // false
println!("{}", char2.is_numeric()); // false
println!("{}", char3.is_numeric()); // true
println!("{}", char4.is_numeric()); // true
println!("{}", char5.is_numeric()); // true
}

Explanation

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.