如何比较两个 HashMap 的对象标识,而不是值标识?

问题描述 投票:0回答:1

我想比较两个不同的哈希图,尽管它们包含的对象相同。我希望能够分辨出它们何时不同。我如何测试它们的底层对象身份是否不同?


fn main(){
    let h1 = HashMap::<i64,i64>::new();
    let h2 = HashMap::<i64,i64>::new();
    
    assert!(h1 != h2);  // this doesn't work, the maps have identical collections.
}

rust hashmap comparison
1个回答
0
投票

由于 Rust 只允许每个值有一个所有者,因此这种比较将始终是不平等的。

如果要比较两个对 HashMap 的引用,可以使用

std::ptr::eq
:

assert!(!std::ptr::eq(&h1, &h2));

另外查看相关功能

addr_eq
,或者
Arc
Rc
,有
ptr_eq

© www.soinside.com 2019 - 2024. All rights reserved.