The is_ascii()
method in Rust is used to check if the characters of a particular string are ASCII or within the ASCII range. If at least one of the characters is not ASCII, then a Boolean false
is returned. Otherwise, a true
is returned.
A character is said to be ASCII if it is a number from 0 to 9, a letter from A to Z, or a special character.
string.is_ascii()
string
: This is the string we are checking to see if it is an ASCII.
The value returned is a boolean. If all the characters of the string are ASCII, a true
is returned. Otherwise, false
is returned.
fn main(){// create some stringslet str1 = "Edpresso is the best";let str2 = "I love ❤ Educative.io"; // non ASCIIlet str3 = "I shine like a star ☀"; // non ASCIIlet str4 = "Rust is the best!";let str5 = "Coding is fun";// check if stracters are asciiprintln!("'{}' is ASCII? {}", str1, str1.is_ascii()); // trueprintln!("'{}' is ASCII? {}", str2, str2.is_ascii()); // falseprintln!("'{}' is ASCII? {}", str3, str3.is_ascii()); // falseprintln!("'{}' is ASCII? {}", str4, str4.is_ascii()); // trueprintln!("'{}' is ASCII? {}", str5, str5.is_ascii()); // true}
is_ascii()
method and then print the result to the console screen.