Find any issues in my Rust code based on the error message and the current code.
[Error message]
Line 40: Char 13: error: cannot assign to tmp because it is borrowed (solution.rs)
|
40 | tmp = curr.as_mut().unwrap().next.take();
| ^^^ tmp is assigned to here but it was already borrowed
41 | curr = &mut tmp;
| -------- tmp is borrowed here
42 | results.push(result.clone());
| ------ borrow later used here
Line 40: Char 19: error: borrow of moved value: curr (solution.rs)
|
28 | let mut curr = &mut head;
| -------- move occurs because curr has type &mut Option<Box<list_node::ListNode>>, which does not implement the Copy trait
29 | let mut tmp;
30 | for i in 0..k {
| ------------- inside of this loop
...
35 | let mut result = curr;
| ---- value moved here
...
40 | tmp = curr.as_mut().unwrap().next.take();
| ^^^^ value borrowed here after move
|
help: consider cloning the value if the performance cost is acceptable
|
35 | let mut result = curr.clone();
| ++++++++
Some errors have detailed explanations: E0382, E0506.
For more information about an error, try rustc --explain E0382.
error: could not compile prog (bin "prog") due to 2 previous errors
[Code]
impl Solution {
pub fn split_list_to_parts(mut head: Option<Box>, k: i32) -> Vec<Option<Box>> {
let count = 0;
let mut node = &head;
while let Some(nd) = node {
node = &nd.next;
}
let small_size = count / k;
let large_size = small_size + 1;
let large_no = count % k;
let mut results = Vec::new();
let mut curr = &mut head;
let mut tmp;
for i in 0..k {
if curr.is_none() {
results.push(None);
continue;
}
let mut result = curr;
let sz = if i < large_no {large_size} else {small_size};
for j in 0..sz-1 {
curr = &mut curr.as_mut().unwrap().next;
}
tmp = curr.as_mut().unwrap().next.take();
curr = &mut tmp;
results.push(result.clone());
}
results
}
}