本文介绍了有没有办法区分具有相同值的不同`Rc`?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是一个例子:
use std::rc::Rc; #[derive(PartialEq, Eq)] struct MyId; pub fn main() { let rc_a_0 = Rc::new(MyId); let rc_a_1 = rc_a_0.clone(); let rc_b_0 = Rc::new(MyId); let rc_b_1 = rc_b_0.clone(); println!("rc_a_0 == rc_a_1: {:?}", rc_a_0 == rc_a_1); println!("rc_a_0 == rc_b_0: {:?}", rc_a_0 == rc_b_0); }两个println!都打印true.有没有办法区分rc_a_*和rc_b_*指针?
Both println!s above print true. Is there a way distinguish between the rc_a_* and rc_b_* pointers?
推荐答案您可以将&*rc强制转换为*const T以获得指向基础数据的指针并比较这些指针的值:
You can cast &*rc to *const T to get a pointer to the underlying data and compare the value of those pointers:
use std::rc::Rc; #[derive(PartialEq, Eq)] struct MyId; pub fn main() { let rc_a_0 = Rc::new(MyId); let rc_a_1 = rc_a_0.clone(); let rc_b_0 = Rc::new(MyId); let rc_b_1 = rc_b_0.clone(); println!( "rc_a_0 == rc_a_1: {:?}", &*rc_a_0 as *const MyId == &*rc_a_1 as *const MyId ); println!( "rc_a_0 == rc_b_0: {:?}", &*rc_a_0 as *const MyId == &*rc_b_0 as *const MyId ); }打印
rc_a_0 == rc_a_1: true rc_a_0 == rc_b_0: false更多推荐
有没有办法区分具有相同值的不同`Rc`?
发布评论