Rust program to check whether a character variable contains a digit or not

Given a character variable, we have to check whether it contains a digit or not using Rust program.
Submitted by Nidhi, on September 28, 2021

Problem Solution:

Here, we will create two-character variables and check created variable contains a digit or not.

Program/Source Code:

The source code to check a character variable contains a digit or not is given below. The given program is compiled and executed successfully.

// Rust program to check whether a character variable
// contains a digit or not

fn main() {
    let mut ch:char = '5';
    let mut ch1:char = 'B';
    
    if (ch >= '0' && ch <= '9')
    {
       println!("Character '{}' is a digit",ch);
    }
    else
    {
        println!("Character '{}' is not a digit",ch);
    }
    
    if (ch1 >= '0' && ch1 <= '9')
    {
       println!("Character '{}' is a digit",ch1);
    }
    else
    {
        println!("Character '{}' is not a digit",ch1);
    }
}

Output:

Character '5' is a digit
Character 'B' is not a digit

Explanation:

Here, we created two characters variables ch and ch1 that are initialized with '5' and 'B' respectively. Then we checked the given characters contains digits or not and printed the appropriate message.

Rust Basic Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.