相关疑难解决方法(0)

Rust的ref关键字是否可以避免?

为什么Rust有ref关键字?可以

match value.try_thing() {
    &Some(ref e) => do_stuff(e),
    // ...
}
Run Code Online (Sandbox Code Playgroud)

不能由

match value.try_thing() {
    &Some(e) => do_stuff(&e),
    // ...
}
Run Code Online (Sandbox Code Playgroud)

language-design pattern-matching rust

2
推荐指数
1
解决办法
120
查看次数

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

Rust的书ref关键词为“传统”。当我想遵循隐式建议避免时ref,如何在以下玩具示例中做到这一点?您也可以在操场上找到代码。

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
        // …
Run Code Online (Sandbox Code Playgroud)

pattern-matching rust

1
推荐指数
1
解决办法
249
查看次数

标签 统计

pattern-matching ×2

rust ×2

language-design ×1