What are you trying to do? Since you cannot have several references to the same “object” in the same way you can in many other languages such as Java, it usually doesn't make sense to perform this operation.
If you are using a smart pointer like Rc that allows you to have several references to the same object, they typically have a dedicated method (e.g. ptr_eq) for doing an equality check. In the specific case of an Rc, you can call into_raw to obtain a pointer which you can cast to usize and then hash.
If you must, you can use a similar method on Vec, which returns a pointer you can cast to usize and hash. However if the vec runs out of capacity, it will reallocate which results in a new hash.
Thanks everyone, std::ptr::hash seems like what I need.
As a quick follow-up question - when does std::ptr::hash change for a given slice. When the slice gets reallocated? If so, when does the slice get reallocated? only when its capacity changes? will this happen randomly at some other point?
The capacity changes once you give the global allocator (Rust uses jemalloc now) a *mut u8 followed-by the new Layout. Re-allocation never happens "randomly" for raw slice types. If you use a std::vec::Vec, then re-alloc occurs once you push an item such that its new implied length is greater than the capacity. Re-allocation is done automatically for you with vectors (so long as you use the safe functions provided in the API), but with raw slices, this needs to be done manually