I have the following code which compiles:
pub mod Btree {
pub struct node {
pub id: u32,
pub red: bool,
pub left: Option<Box<node>>,
pub right: Option<Box<node>>,
}
impl<'a> node {
pub fn insert(mut node_: Option<Box<node>>, id: u32) -> Option<Box<node>> {
match node_ {
None => Some(Box::new(node {
id: id,
red: true,
left: None,
right: None,
})),
Some(x) => x.insert_(id),
}
}
pub fn insert_(mut self, id: u32) -> Option<Box<node>> {
self.left = node::insert(self.left, id);
Some(Box::new(self))
}
}
}
When I change insert_()
to work with a Box<node>
instead:
pub mod Btree {
pub struct node {
pub id: u32,
pub red: bool,
pub left: Option<Box<node>>,
pub right: Option<Box<node>>,
}
impl<'a> node {
pub fn insert(mut node_: Option<Box<node>>, id: u32) -> Option<Box<node>> {
match node_ {
None => Some(Box::new(node {
id: id,
red: true,
left: None,
right: None,
})),
Some(x) => node::insert_(x, id),
}
}
pub fn insert_(mut node_: Box<node>, id: u32) -> Option<Box<node>> {
node_.left = node::insert(node_.left, id);
Some(node_)
}
}
}
I get:
error[E0382]: use of partially moved value: `node_`
--> src/main.rs:23:13
|
23 | node_.left = node::insert(node_.left, id);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^----------^^^^^
| | |
| | value moved here
| value used here after move
|
= note: move occurs because `node_.left` has type `std::option::Option<std::boxed::Box<Btree::node>>`, which does not implement the `Copy` trait
I don't understand what that is. The code is very similar and in both cases there is a move.