In Rust, we can check if two ASCII values are a case-insensitive match. This means that without checking their casing, we check if they are ASCII values and if they match. This can be found by using the method eq_ignore_ascii_case()
.
A character is said to be an ASCII value if it is a number from 0 to 9 or a letter A to Z, or if it's some special character.
character1.eq_ignore_ascii_case(&character2)
character1
: This is a character we want to compare with another character2
.
The value returned is a boolean value. If the characters match, then a true
is returned. Otherwise, a false
is returned.
fn main(){ // create some characters let char1 = 'i'; let char2 = 'I'; let char3 = '☀'; let char4 = 'M'; let char5 = 'm'; let char6 = 'k'; // check if they match without minding their case println!("{}", char1.eq_ignore_ascii_case(&char2)); // true println!("{}", char2.eq_ignore_ascii_case(&char3)); // false println!("{}", char5.eq_ignore_ascii_case(&char4)); // true println!("{}", char6.eq_ignore_ascii_case(&char5)); // false }
RELATED TAGS
CONTRIBUTOR
View all Courses