djole
August 27, 2020, 2:03pm
1
I am a beginner and rust and I can't fix the issue no matter what I try.
This is the error:
use of moved value: `current_node`
value used here after moverustc(E0382)
closest_value_bst.rs(24, 28): value moved here
closest_value_bst.rs(15, 9): move occurs because `current_node` has type `std::option::Option<std::boxed::Box<basic::closest_value_bst::BST>>`,
which does not implement the `Copy` trait
I tried references as well but I end up having even more errors..
What would be a standard way of solving these issues?
alice
August 27, 2020, 2:05pm
2
current_node.as_ref().unwrap()
or current_node.as_mut().unwrap()
tesuji
August 27, 2020, 2:44pm
4
Could you give us some code in play.rust-lang.org/ to play around ?
djole
August 27, 2020, 2:50pm
5
Yes.
This is my first week of rust pretty much.. Try not to judge the code too much
trentj
August 27, 2020, 3:01pm
7
No need for take
in this particular example: since current_node
is not used again before reassigning it, you can just move out of it.
pub fn algo<'a>(tree: Option<Box<BST>>, target: i32, mut closest: i32) -> i32 {
let mut current_node = tree;
while let Some(node) = current_node {
if (target - closest).abs() > (target - node.value).abs() {
closest = node.value;
}
if target < node.value {
current_node = node.left;
} else if target > node.value {
current_node = node.right;
} else {
break;
}
}
closest
}
2 Likes
djole
August 27, 2020, 3:05pm
8
Can't believe that was only mistake to the algorithm... Thank you everyone
tesuji
August 27, 2020, 3:14pm
9
Actually it is an ownership problem.
system
Closed
November 25, 2020, 3:14pm
10
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.