1

In JavaScript, one can compare/find objects like this:

let foo = {}
let bar = foo
[foo].includes(bar) // true

It's a fast operation, because as far as I know, JavaSscript stores some hidden identifier (memory address?) for every object and just compares them. Rust, on the other hand seems to require implementing the Eq trait which compares every single field including those of every underlying data structure. I can imagine that this may be very, very slow, for such a performant language as Rust, so I guess I am missing something. Where am I wrong? Also, can Rust compare objects in a safe manner by their memory address?

To rephrase my question, I want to know if two references point to the same object.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Nurbol Alpysbayev
  • 19,522
  • 3
  • 54
  • 89
  • JS works by comparing references not the objects right? I don't think Rust lets you do that. Is this what you really want to do though? You could have two distinct but equal objects in your array. – Brady Dean Nov 13 '19 at 17:58
  • 1
    Checking whether two references refer to the same object, and whether two different objects have the same value, are two things which are related but very different. Comparing addresses is *not* a performance optimization over comparing values, even in JS. – hegel5000 Nov 13 '19 at 18:00
  • @hegel5000 I know, to rephrase my question, I want to know if two references point to the same object. – Nurbol Alpysbayev Nov 13 '19 at 18:02
  • @BradyDean please see the update – Nurbol Alpysbayev Nov 13 '19 at 18:05

0 Answers0