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 radix
println!("{}", 'a'.is_digit(10)); // false
println!("{}", '1'.is_digit(2)); // true
println!("{}", '1'.is_digit(8)); // true
println!("{}", 'c'.is_digit(16)); // true
println!("{}", 'f'.is_digit(16)); // true
println!("{}", '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 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.

Free Resources