How to check whether a character is a punctuation mark or not in Rust

2 Answers

0 votes
fn main() {
    let ch:char = '#';
    
    if ch == '.' || ch == '?' || ch == '#' || ch == '$' || ch == ';' || ch == '&' || 
       ch == '\'' || ch == '(' || ch == ')' || ch == ',' || ch == '+' || ch == '*' || 
       ch == '-' || ch == '!' || ch == '/' || ch == ':' || ch == '%' || ch == '<' || 
       ch == '=' || ch == '>' || ch == '\\' || ch == '@' || ch == '[' || ch == '\\' || 
       ch == ']' || ch == '^' || ch == '`' || ch == '{' || ch == '|' || ch == '}'
    {
       println!("yes");
    }
    else {
       println!("no");
    }  
}




/*
run:
 
yes
 
*/

 



answered May 6, 2023 by avibootz
0 votes
fn main() {
    let char1 = 'A';
    let char2 = '-';
    let char3 = '=';
    let char4 = '8';
    let char5 = '@';
    let char6 = '.';
 
    println!("{}", char1.is_ascii_punctuation());
    println!("{}", char2.is_ascii_punctuation());
    println!("{}", char3.is_ascii_punctuation());
    println!("{}", char4.is_ascii_punctuation());
    println!("{}", char5.is_ascii_punctuation());
    println!("{}", char6.is_ascii_punctuation());
}
 
 
 
 
/*
run:
 
false
true
true
false
true
true
 
*/

 



answered May 6, 2023 by avibootz

Related questions

2 answers 182 views
2 answers 224 views
1 answer 118 views
1 answer 160 views
1 answer 178 views
1 answer 134 views
...