有没有办法区分具有相同值的不同`Rc`?

编程入门 行业动态 更新时间:2024-10-27 17:19:13
本文介绍了有没有办法区分具有相同值的不同`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`?

本文发布于:2023-10-31 01:27:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1544589.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:没有办法   Rc

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!