ADVERTISEMENT
ADVERTISEMENT

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 »



ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.