2

変更可能なベクトルから最後の要素を複製し、値を Rust のベクトルにプッシュする方法は?

fn ElementsFromPoint(&self, ...) -> Vec<Root<Element>> {

    let mut elements: Vec<Root<Element>> = self.elements_from_point(point).iter()
        .map(|&untrusted_node_addr| {...}).collect();     

    let last_element = elements.last().clone().unwrap(); // elements.last() is Option<&Root<Element>>
    if let Some(root_element) = self.GetDocumentElement() { //self.GetDocumentElement() is Option<Root<Element>>
        if *last_element != root_element {
            elements.push(root_element);
        }
    }


    elements
}

エラーは

2620:17: 2620:25 error: cannot borrow `elements` as mutable because it is also borrowed as immutable [E0502]
2620                 elements.push(root_element);
                     ^~~~~~~~
2617:28: 2617:36 note: previous borrow of `elements` occurs here; the immutable borrow prevents subsequent moves or mutable borrows of `elements` until the borrow ends
2617         let last_element = elements.last().clone().unwrap();
                                ^~~~~~~~
2626:6: 2626:6 note: previous borrow ends here
2590     fn ElementsFromPoint(&self, x: Finite<f64>, y: Finite<f64>) -> Vec<Root<Element>> {
                                                         ...
2626     }
                                                             ^
2625:9: 2625:17 error: cannot move out of `elements` because it is borrowed [E0505]
2625         elements
             ^~~~~~~~
2617:28: 2617:36 note: borrow of `elements` occurs here
2617         let last_element = elements.last().clone().unwrap();

私はこれを読ん試しました

let last_element = elements.last().unwrap().clone();

そしてまた

let last_element = elements.last().map(|t| t.clone()).unwrap();

それでも失敗しました。

また、まだ.cloned()実装されてRoot<T>いません。

変更可能なベクトルから最後の要素を複製し、値を Rust のベクトルにプッシュする方法はありますか? Clonedまたは、最初に特性を実装する必要がありますか?

4

1 に答える 1