Avatar ·

Difference between == and === in Rust

Hi everyone! I recently started learning Rust and encountered an unexpected problem when comparing values. In some languages (e.g., JavaScript), there are two comparison operators: == (with type coercion) and === (strict comparison without coercion).

\n\n

I tried using === in Rust to compare strings, but the compiler gave an error:

\n\n
fn main() {\n    let a = String::from("hello");\n    let b = String::from("hello");\n    \n    if a === b {\n        println!("Equal");\n    }\n}\n
\n\n

Error: expected one of ".", ";", "?", "}", or an operator, found "=="

\n\n

I expected === to work as a strict comparison without type coercion, but apparently there is no such operator in Rust. At the same time, bo