How to check if a character is an ASCII punctuation in Rust

Overview

We can check if a character is an ASCII punctuation with the is_ascii_punctuation() method. ASCII is a form of character encoding which is used in encoding and decoding characters during communication between computers. Other encodings include UTF-8, ISO-8859-1, GBK, ISO-8859-11, and more.

Syntax

character.is_ascii_punctuation()
Syntax for is_ascii_punctuation() in Rust

Parameter

character: This is the character we are checking to see if it is an ASCII punctuation character or not.

Return value

The value returned is a Boolean value. We get true if the character is an ASCII punctuation. Otherwise, false is returned.

Code example

fn main(){
// check if some characters are really digits in some radix
println!("{}", '#'.is_ascii_punctuation()); // true
println!("{}", 'š•Š'.is_ascii_punctuation()); // false
println!("{}", 'šŸ’£'.is_ascii_punctuation()); // false
println!("{}", '?'.is_ascii_punctuation()); // true
println!("{}", '!'.is_ascii_punctuation()); // true
println!("{}", '.'.is_ascii_punctuation()); // true
}

Explanation

  • Lines 4ā€“8: We use the is_ascii_punctuation() method to check if some characters are ASCII punctuation characters. Then we print the results to the console.