ASCII stands for American Standard Code for Information Interchange. It was the first encoding among others such as the UTF-8, ISO-8859-1, and so on. It is a way to encode a character set used for communication between computers.
A character is said to be in the range of ASCII if it is a number from 0 to 9 or a letter A to Z, or if it's some special character. We can use the is_ascii()
method to check if a character is within the ASCII range.
character.is_ascii();
character
: This is the character we want to check if it is an ASCII in Rust.
The value returned is a boolean. If the character falls within the ASCII range, a true
is returned, otherwise false
.
fn main(){// create some characterslet char1 = 'E';let char2 = '❤'; // non ASCIIlet char3 = '☀'; // non ASCIIlet char4 = '7';let char5 = '!';// check if characters are asciiprintln!("{} is ASCII? {}", char1, char1.is_ascii()); // falseprintln!("{} is ASCII? {}", char2, char2.is_ascii()); // falseprintln!("{} is ASCII? {}", char3, char3.is_ascii()); // trueprintln!("{} is ASCII? {}", char4, char4.is_ascii()); // trueprintln!("{} is ASCII? {}", char5, char5.is_ascii()); // true}
is_ascii()
method, and then print the result to the console screen.