Why isn’t it possible to compare a borrowed integer to a literal integer?

You interpret the error correctly, and the reason is that it simply isn’t implemented. If the standard library writers wanted to make this work, they’d have to implement PartialEq for &i32 == i32, i32 == &i32, &mut i32 == i32, i32 == &mut i32, &i32 == &mut i32 and &mut i32 == &i32. And then they’d have to do that for all other primitive types (i8, i16, u8, u16, u32, i64, u64, f32, f64, and char).

That’s a lot of PartialEq implementations.

Or instead they can just ask the users of the language to write *c != 0.

(If you’re coming from C++, the key thing to understand is that syntactically, borrows are more like pointers than references. Only method call syntax has the auto-deref feature.)

Leave a Comment