struct - 谁拥有采用 &self 的函数中结构成员的所有权?

标签 struct rust ownership

我正在尝试围绕 VecDeque 编写一个小包装器。

特别是我有代码(playground):

use std::collections::VecDeque;

trait VecCircleTraits<T: Eq> {
    fn new() -> VecCircle<T>;
    fn find_and_remove(&self, _: T) -> Option<T>;
}

#[derive(Debug)]
struct VecCircle<T: Eq>(VecDeque<T>);

impl<T: Eq> VecCircleTraits<T> for VecCircle<T> {
    fn new() -> VecCircle<T> {
        return VecCircle(VecDeque::<T>::new());
    }

    fn find_and_remove(&self, key: T) -> Option<T> {
        let search_index: Option<usize> = self.0.into_iter().position(|x| x == key); //error 1
        if let Some(index) = search_index {
            return self.0.remove(index); // error 2
        } else {
            return None;
        }
    }
}

这给了我以下错误:

    error: cannot borrow immutable anonymous field `self.0` as mutable
  --> <anon>:20:20
   |>
20 |>             return self.0.remove(index); // error 2
   |>                    ^^^^^^

error: cannot move out of borrowed content [--explain E0507]
  --> <anon>:18:44
   |>
18 |>         let search_index: Option<usize> =  self.0.into_iter().position(|x| x == key); //error 1
   |>                                            ^^^^ cannot move out of borrowed content

但是,我不太清楚谁拥有 self.0 的所有权?如果我正确地理解了文档,内存区域不会被绑定(bind)到 self.0 并因此赋予它所有权吗?很抱歉那里的逻辑很肤浅,但我仍在努力理解所有权系统。

最佳答案

find_and_remove ,您指定了 &self在参数列表中。这意味着该方法将收到一个指向 self 的借用指针。 ;即 self 的类型是&VecCircle<T> .因此,该方法不拥有 VecCircle<T> 的所有权。 .

find_and_remove尝试调用 into_iter VecDeque 上, 和 into_iter按值接收其参数( self 而不是 &self&mut self )。因此,Rust 解释 self.0试图移动 VecDeque来自 VecCircle .但是,这是不允许的,因为您不能从借用的内容中移动任何内容,因为从某个位置移动会使该位置无效。但我们不能只告诉调用者“嘿,我刚刚使 self 失效,停止使用它!”;如果我们想这样做,我们必须指定 self在参数列表中,而不是 &self .

但这不是您要在此处尝试执行的操作。 into_iter将取得 VecDeque 的所有权因此摧毁它。还有其他方法可以获得 VecDeque 的迭代器不破坏它。在这里,我们应该使用 iter ,这需要 &self .

然后,find_and_remove尝试调用 remove . remove需要 &mut self ,即对 VecDeque 的可变引用.但是,我们不能借self.0作为可变的,因为 self本身不是可变借用。我们不能只将不可变借用升级为可变借用:同时使用不可变借用和可变借用是无效的。这里的解决办法是改成&self&mut self在参数列表中。

use std::collections::VecDeque;

trait VecCircleTraits<T: Eq> {
    fn new() -> VecCircle<T>;
    fn find_and_remove(&mut self, _: &T) -> Option<T>;
}

#[derive(Debug)]
struct VecCircle<T: Eq>(VecDeque<T>);

impl<T: Eq> VecCircleTraits<T> for VecCircle<T> {
    fn new() -> VecCircle<T> {
        return VecCircle(VecDeque::<T>::new());
    }

    fn find_and_remove(&mut self, key: &T) -> Option<T> {
        let search_index: Option<usize> =  self.0.iter().position(|x| x == key);
        if let Some(index) =  search_index {
            self.0.remove(index)
        } else {
            None
        }
    }
}

注意:我还更改了 key参数 &T解决另一个错误,这次在闭包中传递给 position .自 iter迭代对 VecDeque 中项目的引用, position传递对闭包的引用。自 find_and_remove实际上并不需要获得 key 的所有权,它应该只接收一个不可变的借用,这样 xkey类型为 &T因此我们可以申请==给他们。

关于struct - 谁拥有采用 &self 的函数中结构成员的所有权?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39693063/

相关文章:

c++ - 级联结构指针测试代码中的段错误

struct - 比较结构数组和字符串中的元素

c - 仅给出成员位置时结构的位置

rust - 在 Rust 中开发基板运行时是否有必要使用宏?

for-loop - 在 Rust 中的 if block 内创建一个向量。 if作用域结束后如何使用?

c++ - C++结构到Delphi Record dll调用

struct - '&self' 和 '&' a self' 有什么区别?

linux - mac OS X 和 linux 之间的 rsync 所有权

rust - 为什么 Rust 不允许在一种类型上复制和删除特征?

unit-testing - 如何在 ink 中设置来电者!契约(Contract)单元测试功能?