【问题标题】:How to clone a Box<CustomStruct> when I keep getting "no method named clone"?当我不断收到“没有名为克隆的方法”时如何克隆 Box<CustomStruct>?
【发布时间】:2019-06-14 14:18:00
【问题描述】:

我想克隆head,这是一个Box&lt;Node&lt;T&gt;&gt;

let mut node = Some((*head).clone());

这里是完整代码(playground):

use std::boxed::Box;

pub struct List<T> {
    pub head: Option<Box<Node<T>>>,
    pub size: u64,
}

impl<T> List<T> {
    pub fn new() -> List<T> {
        return List {
            head: Option::None,
            size: 0,
        };
    }

    pub fn push_back(&mut self, data: T) {
        match &self.head {
            Some(head) => {
                let mut node = Some((*head).clone());
                while (*(node).unwrap()).next.is_some() {
                    node = (*node.unwrap()).next;
                }
                node.unwrap().next = Some(Box::new(Node::new(data)));
            }
            None => {
                self.head = Some(Box::new(Node::new(data)));
            }
        }
    }
}

#[derive(Clone)]
pub struct Node<T> {
    pub next: Option<Box<Node<T>>>,
    pub value: T,
}

impl<T> Node<T> {
    pub fn new(v: T) -> Node<T> {
        Node {
            next: Option::None,
            value: v,
        }
    }
}

编译器一直说方法clone 存在,但不满足以下特征界限:

error[E0599]: no method named `clone` found for type `std::boxed::Box<Node<T>>` in the current scope
  --> src/lib.rs:19:45
   |
19 |                 let mut node = Some((*head).clone());
   |                                             ^^^^^
   |
   = note: the method `clone` exists but the following trait bounds were not satisfied:
           `Node<T> : std::clone::Clone`
           `std::boxed::Box<Node<T>> : std::clone::Clone`
   = help: items from traits can only be used if the trait is implemented and in scope
   = note: the following trait defines an item `clone`, perhaps you need to implement it:
           candidate #1: `std::clone::Clone`

我尝试添加#[derive(Clone)],但还是不行:

#[derive(Clone)]
pub struct Node<T> {
    pub next: Option<Box<Node<T>>>,
    pub value: T
}

我该怎么做?

【问题讨论】:

    标签: rust


    【解决方案1】:

    错误的再现:

    #[derive(Clone)]
    pub struct Node<T> {
        pub next: Option<Box<Node<T>>>,
        pub value: T,
    }
    
    fn thing<T>(node: Node<T>) {
        node.clone();
    }
    
    error[E0599]: no method named `clone` found for type `Node<T>` in the current scope
     --> src/lib.rs:8:10
      |
    2 | pub struct Node<T> {
      | ------------------ method `clone` not found for this
    ...
    8 |     node.clone();
      |          ^^^^^
      |
      = note: the method `clone` exists but the following trait bounds were not satisfied:
              `Node<T> : std::clone::Clone`
      = help: items from traits can only be used if the trait is implemented and in scope
      = note: the following trait defines an item `clone`, perhaps you need to implement it:
              candidate #1: `std::clone::Clone`
    

    您需要添加一个 trait bound,表明 T 实现 Clone

    fn thing<T>(node: Node<T>)
    where
        T: Clone,
    {
        node.clone();
    }
    

    另见:


    您的代码有许多不习惯的方面:

    • 不需要导入std::boxed::Box
    • 不需要unwraps
    • 不需要的取消引用。

    这里甚至不需要克隆,使用它可能是不正确的。我会写:

    pub fn push_back(&mut self, data: T)
    where
        T: Clone,
    {
        let spot = match &mut self.head {
            Some(head) => {
                let mut node = &mut head.next;
                while let Some(n) = node {
                    node = &mut n.next;
                }
                node
            }
            None => &mut self.head,
        };
    
        *spot = Some(Box::new(Node::new(data)));
    }
    

    另见:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-17
      • 1970-01-01
      • 1970-01-01
      • 2011-12-22
      • 1970-01-01
      • 1970-01-01
      • 2010-10-19
      相关资源
      最近更新 更多