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

2 Answers

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

    if  (ch == ' ' ) || 
        (ch == '\t') || 
        (ch == '\n') || 
        (ch == '\r')
    {
       println!("yes");
    }
    else {
       println!("no");
    } 
}



 
/*
run:
 
yes
 
*/

 



answered May 5, 2023 by avibootz
0 votes
fn main() {
    let char1 = 'W';
    let char2 = ' ';  
    let char3 = '\n';      
    let char4 = 'r';
    let char5 = '\r';
    let char6 = '\t'; 

    println!("{}", char1.is_whitespace()); 
    println!("{}", char2.is_whitespace()); 
    println!("{}", char3.is_whitespace()); 
    println!("{}", char4.is_whitespace());  
    println!("{}", char5.is_whitespace());  
    println!("{}", char6.is_whitespace());  
}



 
/*
run:
 
false
true
true
false
true
true
 
*/

 



answered May 5, 2023 by avibootz

Related questions

2 answers 202 views
2 answers 243 views
1 answer 132 views
1 answer 177 views
1 answer 191 views
...