0

Rust – How to check if a String is Numeric

Last Updated on

In Rust, we can use the function is_numeric()  on a char value.

Test Data

Given the following strings, we want to know if each is numeric and only contains numeric digits.

Codes

The codes contain two functions – is_string_numeric(String)  and the main function.

is_string_numeric  function

This is a custom function that accepts a String and returns a bool.

From the input, we loop through each char value by using a for loop with the String.chars()  function. Then, we invoke the is_numeric() function on each char value.

main function

Demo and Output

Tested with Rust 1.37.0. See https://doc.rust-lang.org/std/primitive.char.html.

Karl San Gabriel

Karl San Gabriel

Professional Software Developer