What is string.eq_ignore_ascii_case() in Rust programming?

Overview

We can check if two strings are the case-sensitive match using the eq_ignore_ascii_case() method. The method also lets us check if they are ASCII strings.

Syntax

string1.eq_ignore_ascii_case(&string2)
The syntax for checking if two strings are case-insensitive

Parameters

string1: This is the string we want to compare with another string2.

Return value

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

Example

fn main() {
// create some strings
let str1 = "Welcome";
let str2 = "WELCOME";
let str3 = "tO";
let str4 = "tO";
let str5 = "EDPREsso";
let str6 = "edpresso";
let str7 = "Rust";
let str8 = "Solidity";
// check if they match without minding their case
println!("{}", str1.eq_ignore_ascii_case(&str2)); // true
println!("{}", str3.eq_ignore_ascii_case(&str4)); // true
println!("{}", str5.eq_ignore_ascii_case(&str6)); // true
println!("{}", str7.eq_ignore_ascii_case(&str8)); // false
}

Explanation

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