首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在函数&self或&mut self中进行模式匹配时,如何避免ref关键字?

在函数&self或&mut self中进行模式匹配时,如何避免ref关键字?
EN

Stack Overflow用户
提问于 2019-03-13 05:31:00
回答 1查看 629关注 0票数 2

“铁锈”一书calls the ref keyword "legacy"。因为我想遵循隐含的建议来避免ref,我该如何在下面的玩具示例中做到这一点呢?你也可以在playground上找到代码。

代码语言:javascript
运行
复制
struct OwnBox(i32);

impl OwnBox {
    fn ref_mut(&mut self) -> &mut i32 {
        match *self {
            OwnBox(ref mut i) => i,
        }

        // This doesn't work. -- Even not, if the signature of the signature of the function is
        // adapted to take an explcit lifetime 'a and use it here like `&'a mut i`.
        // match *self {
        //     OwnBox(mut i) => &mut i,
        // }

        // This doesn't work
        // match self {
        //     &mut OwnBox(mut i) => &mut i,
        // }
    }
}
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55130932

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档