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

Overview

ASCII stands for American Standard Code for Information Interchange. It is a way of encoding a character set used for communication between computers. ASCII is the first among many encodings, which also include such encodings as the UTF-8, ISO-8859-1, etc.

A character is said to be ASCII if it is a number from 0 to 9, a letter from A to Z, or some special character.

We can use the is_ascii_alphabetic() method to check if a character is both an ASCII and a letter of the alphabet. It returns true if the character is an ASCII and a letter. Otherwise, it returns false.

Syntax

character.is_ascii_alphabetic()
Syntax for is_ascii_alphabetic() method

Parameters

character: This is the character that we are checking to see if it is both an ASCII and an alphabetic letter.

Return value

This method returns a boolean value. It returns true if the character is both an ASCII and a letter. Otherwise, it returns false.

Code example

fn main(){
// create some characters
let char1 = 'R';
let char2 = '❤'; // non ASCII
let char3 = 'l';
let char4 = 'a';
let char5 = '3';
// check if ASCII and alphabetic too
println!("{}", char1.is_ascii_alphabetic());
println!("{}", char2.is_ascii_alphabetic());
println!("{}", char3.is_ascii_alphabetic());
println!("{}", char4.is_ascii_alphabetic());
println!("{}", char5.is_ascii_alphabetic());
}

Explanation

  • Lines 3–7: We create some ASCII and non-ASCII characters.
  • Lines 10–14: We check each character using the is_ascii_alphabetic() method to see if they are ASCII and alphabetic. Then we print the result to the console screen.

Free Resources