Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

numeric
rust
character
communitycreator

How to check if a character in Rust is numeric

okwudili onyejiaku

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
}
Check if a character is numeric in Rust using the is_numeric() method

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.

RELATED TAGS

numeric
rust
character
communitycreator
RELATED COURSES

View all Courses

Keep Exploring