I'm working on a binary search tree implementation in Rust and I'm trying to implement a function that swaps child nodes.
#[derive(Default)]
pub struct Node {
data: i32,
left: Option<Box<Node>>,
right: Option<Box<Node>>
}
impl Node {
fn print_data(&self) -> i32 {
self.data
}
fn default() -> Node { //function to help when instantiate node with no children
Node {
data: 0,
left: None,
right: None
}
}
fn set_left(&mut self, new_left: Option<Box<Node>>) -> () {
// If self.left was already a pointer, now we're losing it
// (memory leak).
self.left = new_left
// Node {data: self.data, left: new_left, right: &self.right}
}
fn set_right(&mut self, new_right: Option<Box<Node>>) -> () {
self.right = new_right
}
fn swap_childs(&mut self) -> () {
let tmpr = Some(self.right.unwrap());
let tmpl = Some(self.left.unwrap());
let nilr = self.right.take();
let nill = self.left.take();
self.right = tmpl;
self.left = tmpr;
}
}
Here's the error I'm getting:
error[E0507]: cannot move out of `self.right` which is behind a mutable reference
--> src/main.rs:43:19
|
43 | let tmpr = Some(self.right.unwrap());
| ^^^^^^^^^^
| |
| move occurs because `self.right` has type `std::option::Option<std::boxed::Box<Node>>`, which does not implement the `Copy` trait
| help: consider borrowing the `Option`'s content: `self.right.as_ref()`
error[E0507]: cannot move out of `self.left` which is behind a mutable reference
--> src/main.rs:44:19
|
44 | let tmpl = Some(self.left.unwrap());
| ^^^^^^^^^
| |
| move occurs because `self.left` has type `std::option::Option<std::boxed::Box<Node>>`, which does not implement the `Copy` trait
| help: consider borrowing the `Option`'s content: `self.left.as_ref()`
Through a lot of trial and error, I'm aware that my struct can't implement the Copy
trait since I'm using Option
. This is why I assume that the best way to make this change is within the struct, hence the impl function swap_childs
. Is there a better way to do this? Is this function even possible using just Option
and Box
without using other types like Rc
?