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
.
character.is_digit(radix)
radix
: This is the number base we want to check for the character.
The value returned is a Boolean value. If the character is specified in the radix, then a true
is returned. Otherwise, false
is returned.
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}
is_digit()
function to check if some characters are digits in some radix. We provide these radix as base 2
, base 8
, base 10
, and base 16
. If a certain character is a digit in any of the radix, true
is printed to the console. Otherwise, false
is returned.