Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

character
punctuation
rust
ascii
communitycreator

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

okwudili onyejiaku

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
}
Check if a character is ASCII punctuation character

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.

RELATED TAGS

character
punctuation
rust
ascii
communitycreator
RELATED COURSES

View all Courses

Keep Exploring