What is character.eq_ignore_ascii_case() in Rust?

Overview

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.

Syntax

character1.eq_ignore_ascii_case(&character2)
Syntax for eq_ignore_ascii_case() method in Rust

Parameters

character1: This is a character we want to compare with another character2.

Return value

The value returned is a boolean value. If the characters match, then a true is returned. Otherwise, a false is returned.

Example

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
}

Explanation

  • Lines 3–8: We declare some characters.
  • Lines 11–14: We check if the characters are ASCII values and that they match with their cases ignored. Then we print the result to the console.

Free Resources