How to check if a character is an ASCII hexadecimal digit in Rust

Overview

We can check if a character is an ASCII hexadecimal digit in Rust using the is_ascii_hexdigit() method. A hexadecimal digit is a typical digit of the base-16 number system.

Syntax

character.is_hexdigit()
Syntax for character.is_ascii_hexdigit() in Rust

Parameters

character: This is the character we want to check if it is an ASCII hexadecimal digit.

Return value

The value returned is a boolean value.

Example

fn main() {
// check if some characters are ASCII hexadecimal digits
println!("{}", 'i'.is_ascii_hexdigit());
println!("{}", ','.is_ascii_hexdigit());
println!("{}", '0'.is_ascii_hexdigit());
println!("{}", 'c'.is_ascii_hexdigit());
println!("{}", 'f'.is_ascii_hexdigit());
println!("{}", 'i'.is_ascii_hexdigit());
}

Explanation

  • Lines 3–8: We check if some characters in Rust were ASCII hexadecimal digits using the is_ascii_hexdigit() method. Then we print the results to the console.

Free Resources