gpt4 book ai didi

rust - 如何创建自可变引用的Rc >?

转载 作者:行者123 更新时间:2023-12-03 11:45:28 27 4
gpt4 key购买 nike

我正在实现VirtualTapInterface的特征。此特征的receive函数应创建一个TxToken结构,其中lower属性必须是包含当前Rc<RefCell<VirtualTapInterface>>VirtualTapInterface,即self

impl<'a> Device<'a> for VirtualTapInterface {
type TxToken = TxToken;

fn receive(&'a mut self) -> Option<(Self::RxToken, Self::TxToken)> {
let tx = TxToken { lower: Rc::new(RefCell::new(*self))};

我尝试了这个但是我明白了

cannot move out of *self which is behind a mutable reference

move occurs because *self has type phy::virtual_tun::VirtualTapInterface, which does not implement the Copy traitrustc(E0507)



如何创建一个自可变引用的 Rc<RefCell<>>

最佳答案

我认为您需要将签名更改为fn receive(self) -> ...才能拥有所有权。克隆或装箱也可以。

另一种选择是使用 mem::take mem::replace mem::swap

关于rust - 如何创建自可变引用的Rc <RefCell <>>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62011113/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com