How to check whether a character is a digit or not in Rust

2 Answers

0 votes
fn main() {
    let ch:char = '9';

    if ch >= '0' && ch <= '9' {
       println!("Character '{}' is a digit", ch);
    }
    else {
        println!("Character '{}' is not a digit", ch);
    }
}



 
/*
run:
 
Character '9' is a digit
 
*/

 



answered May 5, 2023 by avibootz
0 votes
fn main() {
    let char1 = 'Z';
    let char2 = '9';
    let char3 = 'c';
    let char4 = '0';

    println!("{}", char1.is_numeric());  
    println!("{}", char2.is_numeric());  
    println!("{}", char3.is_numeric());  
    println!("{}", char4.is_numeric());  
}



 
/*
run:
 
false
true
false
true
 
*/

 



answered May 5, 2023 by avibootz

Related questions

2 answers 185 views
2 answers 182 views
1 answer 118 views
1 answer 160 views
1 answer 178 views
1 answer 112 views
...