What is the character.is_ascii_graphic() method in Rust?

Overview

We use the is_ascii_graphic() method to check if a character is an ASCII graphic character. A graphic character is a character that is to be written, printed, shown, etc., in a way that humans can understand. ASCII is a popular encoding for information and communication among computers.

Syntax

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

Parameter

character: This is the character we want to check to see if it is an ASCII graphic character.

Return value

The value returned is a Boolean value. If the character is an ASCII graphic character, then a true will be returned. Otherwise, a false is returned.

Example

fn main(){
// create some characters
let char1 = 't';
let char2 = '❤'; // non ASCII
let char3 = '☀'; // non ASCII
let char4 = 'w';
let char5 = 'n';
// check if characters are ascii graphic characters
println!("{} is ASCII? {}", char1, char1.is_ascii_graphic()); // true
println!("{} is ASCII? {}", char2, char2.is_ascii_graphic()); // false
println!("{} is ASCII? {}", char3, char3.is_ascii_graphic()); // false
println!("{} is ASCII? {}", char4, char4.is_ascii_graphic()); // true
println!("{} is ASCII? {}", char5, char5.is_ascii_graphic()); // true
}

Explanation

  • Lines 3–7: We create some characters (char1 , char2 , char3 , char4 , char5).
  • Lines 10–14: We check if each of the characters we created is an ASCII graphic character.

Free Resources