?-6ren"> ?-我想克隆head这是 Box> : let mut node = Some((*head).clone()); 这是完整的代码(playground): use std::boxed::Box; pu-6ren">
gpt4 book ai didi

rust - 当我不断收到 "no method named clone"时如何克隆 Box

转载 作者:行者123 更新时间:2023-11-29 08:28:49 25 4
gpt4 key购买 nike

我想克隆head这是 Box<Node<T>> :

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
}

我该怎么做?

最佳答案

错误重现:

#[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
  • 不需要的unwrap
  • 不需要的取消引用。

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

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)));
}

另见:

关于rust - 当我不断收到 "no method named clone"时如何克隆 Box<CustomStruct> ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56600021/

25 4 0