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

后端 未结 1 739
野趣味
野趣味 2020-12-31 05:36

I want to get the elements in an array where a condition is true. For example. I would like all indices where the array elements are 0:

fn main() {
    let l         


        
相关标签:
1条回答
  • 2020-12-31 05:53

    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.)

    0 讨论(0)
提交回复
热议问题