【问题标题】:How to clone the last element from a mutable vector and then push a value to the vector in Rust?如何从可变向量中克隆最后一个元素,然后将值推送到 Rust 中的向量?
【发布时间】:2016-03-16 05:07:51
【问题描述】:

如何从 mutable 向量中克隆最后一个元素,然后将值推送到 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();

我已阅读this 并尝试过

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

还有

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

但还是失败了。

还没有为Root&lt;T&gt; 实现.cloned()

有没有办法从 mutable 向量中克隆最后一个元素,然后将值推送到 Rust 中的向量?还是应该先实现Cloned trait?

【问题讨论】:

    标签: vector rust borrowing


    【解决方案1】:

    试试这个

    fn ElementsFromPoint(&self, ...) -> Vec<Root<Element>> {
    
        let mut elements: Vec<Root<Element>> = self.elements_from_point(point).iter()
            .map(|&untrusted_node_addr| {...}).collect();
    
    
        if let Some(root_element) = self.GetDocumentElement() {
            if {
                match elements.last() {
                    Some(last_element) => *last_element != root_element,
                    None => true
                }                
            } {
                elements.push(root_element);
            }
        }
    
        elements
    }
    

    【讨论】:

    • if 条件中的块的好技巧。实际上clone() 在这里什么都不做,unwrap() 会在elements 为空的情况下引起恐慌。我建议使用:if elements.last().map(|x| *x!=root_element).unwrap_or(true) {elements.push(root_element)}
    • @aSpex 我只是从问题中复制了代码,没有进行额外的更改或优化,但你是绝对正确的。
    猜你喜欢
    • 2015-11-01
    • 2016-10-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多