ASCII Case Insensitive Comparison

Compare tokens with eq_ignore_ascii_case to skip allocations, and know when to fall back to full Unicode to_lowercase for non-ASCII text.

RustAuth Jwt

Code

rustsnippet
15 lines
fn main() {
    // eq_ignore_ascii_case:零分配的大小写不敏感比较
    let token = "BEARER abc.def";
    println!("ascii eq: {}", token[..6].eq_ignore_ascii_case("bearer"));

    // 非 ASCII 文本必须走 to_lowercase:ASCII 方法只覆盖 A-Z
    let unicode = "ÄPFEL";
    println!("ascii miss: {}", unicode.eq_ignore_ascii_case("äpfel"));
    println!("unicode ok: {}", unicode.to_lowercase() == "äpfel");

    // make_ascii_lowercase 就地转换:已有 String 时避免新分配
    let mut command = String::from("GET");
    command.make_ascii_lowercase();
    println!("command: {command}");
}

How the snippet works

ASCII Case Insensitive Comparison is built around the function main.

Applying it

Running the file prints labeled results such as ascii eq:, ascii miss:, unicode ok: — each operation’s effect is visible in the output.

Statements that matter

let mut command = String::from("GET");
command.make_ascii_lowercase();
let token = "BEARER abc.def";

Interactive Tools for This Topic

FAQ

What does ASCII Case Insensitive Comparison print when run?
Invoke the file as-is — the `ascii eq:`, `ascii miss:`, `unicode ok:` prefixes identify which step printed each line.