rust - 如何返回对 optional 结构字段内值的引用?

标签 rust reference optional

这个问题在这里已经有了答案:





Cannot move out of value which is behind a shared reference when unwrapping

(2 个回答)



How to get an Option's value or set it if it's empty?

(1 个回答)


11 个月前关闭。




我正在努力改进 Cacher The Rust Programming Language 中描述的类型.建议的改进之一指出 Cacher应该可以用于多种类型。为此,我编写了以下代码:

struct Cacher<T, U>
where
    T: Fn(&U) -> U,
{
    calculation: T,
    value: Option<U>,
}

impl<T, U> Cacher<T, U>
where
    T: Fn(&U) -> U,
{
    fn new(calculation: T) -> Cacher<T, U> {
        Cacher {
            calculation,
            value: None,
        }
    }

    fn value(&mut self, arg: &U) -> &U {
        match self.value {
            Some(v) => &v,
            None => {
                let v = (self.calculation)(arg);
                self.value = Some(v);
                &v
            }
        }
    }
}
编译器提示:
error[E0507]: cannot move out of `self.value.0` which is behind a mutable reference
  --> src/lib.rs:21:15
   |
21 |         match self.value {
   |               ^^^^^^^^^^ help: consider borrowing here: `&self.value`
22 |             Some(v) => &v,
   |                  -
   |                  |
   |                  data moved here
   |                  move occurs because `v` has type `U`, which does not implement the `Copy` trait

error[E0515]: cannot return reference to local variable `v`
  --> src/lib.rs:22:24
   |
22 |             Some(v) => &v,
   |                        ^^ returns a reference to data owned by the current function

error[E0515]: cannot return reference to local variable `v`
  --> src/lib.rs:26:17
   |
26 |                 &v
   |                 ^^ returns a reference to data owned by the current function

error[E0382]: borrow of moved value: `v`
  --> src/lib.rs:26:17
   |
24 |                 let v = (self.calculation)(arg);
   |                     - move occurs because `v` has type `U`, which does not implement the `Copy` trait
25 |                 self.value = Some(v);
   |                                   - value moved here
26 |                 &v
   |                 ^^ value borrowed here after move
我想这是因为 v是函数的局部。但是,鉴于实际数据存在于结构中,该结构存在于 value 之外。方法,是否不可能以任何方式返回对此数据的引用?如果不是这样,那么我需要做什么才能获得 Cacher 的功能拥有计算数据并在需要时返回引用?

最佳答案

你几乎明白了。您只需要返回对对象的引用 Option :

struct Cacher<T, U>
where
    T: Fn(&U) -> U
{
    calculation: T,
    value: Option<U>,
}

impl<T, U> Cacher<T, U>
where
    T: Fn(&U) -> U
{
    fn new(calculation: T) -> Cacher<T, U> {
        Cacher {
            calculation,
            value: None,
        }
    }

    fn value(&mut self, arg: &U) -> &U {
        if self.value.is_none() {
            let v = (self.calculation)(arg);
            self.value = Some(v);
        }
        self.value.as_ref().unwrap()
    }
}
Playground

关于rust - 如何返回对 optional 结构字段内值的引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63980290/

相关文章:

c# - 未设置对象引用 c#

c++ - map 通过引用

c++ - std::bind 不适用于引用?

types - 期望一个切片,在断言它们相等时找到一个数组

data-structures - 如何在特征的函数中发送不同的结构?

java - 我们是否需要 Optional ifNotPresent 在链中间使用?

scala - 在 Scala 中转换多个 optional 值

java - 如何使用 java 对 spark 数据集中的 optional 字段进行编码?

rust - 我如何在 Rust 中定义一个泛型结构,而其中一个字段不是泛型类型?

generics - trait 不能做成一个对象