What is character.is_digit() in Rust?
Overview
We can use the is_digit() function to check if a certain character is a digit in a given radix. Radix here means a number base. For example, we have base two, base eight, base 10, and so on. The is_digit() function recognizes the characters 0-9, a-z, and A-Z.
Syntax
character.is_digit(radix)
The syntax for the is_digit() function
Parameters
radix: This is the number base we want to check for the character.
Return value
The value returned is a Boolean value. If the character is specified in the radix, then a true is returned. Otherwise, false is returned.
Example
fn main(){// check if some characters are really digits in some radixprintln!("{}", 'a'.is_digit(10)); // falseprintln!("{}", '1'.is_digit(2)); // trueprintln!("{}", '1'.is_digit(8)); // trueprintln!("{}", 'c'.is_digit(16)); // trueprintln!("{}", 'f'.is_digit(16)); // trueprintln!("{}", 'f'.is_digit(10)); // false}
Explanation
- Line 3-10: We use the
is_digit()function to check if some characters are digits in some radix. We provide these radix as base2, base8, base10, and base16. If a certain character is a digit in any of the radix,trueis printed to the console. Otherwise,falseis returned.