亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在迭代遞歸結構時無法獲得可變引用:一次不能以可變的形式多次借用

在迭代遞歸結構時無法獲得可變引用:一次不能以可變的形式多次借用

在迭代遞歸結構時無法獲得可變引用:一次不能以可變的形式多次借用我試圖迭代地導航遞歸數據結構,以便在某個位置插入元素。據我有限的理解,這意味著對結構的根進行可變的引用,并將其依次替換為對其追隨者的引用:type Link = Option<Box<Node>>;struct Node {     next: Link}struct Recursive {     root: Link}impl Recursive {     fn back(&mut self) -> &mut Link {         let mut anchor = &mut self.root;         while let Some(ref mut node) = *anchor {             anchor = &mut node.next;         }         anchor    }}(生銹操場鏈接)然而,這是失敗的:error[E0499]: cannot borrow `anchor.0` as mutable more than once at a time   --> src/main.rs:14:24    | 14 |         while let Some(ref mut node) = *anchor {    |                        ^^^^^^^^^^^^    |                        |    |                        second mutable borrow occurs here    |                        first mutable borrow occurs here ... 18 |     }    |     - first borrow ends here error[E0506]: cannot assign to `anchor` because it is borrowed   --> src/main.rs:15:13    | 14 |         while let Some(ref mut node) = *anchor {    |                        ------------ borrow of `anchor` occurs here 15 |             anchor = &mut node.next;    |             ^^^^^^^^^^^^^^^^^^^^^^^ assignment to borrowed `anchor` occurs here error[E0499]: cannot borrow `*anchor` as mutable more than once at a time   --> src/main.rs:17:9    | 14 |         while let Some(ref mut node) = *anchor {    |                        ------------ first mutable borrow occurs here ... 17 |         anchor    |         ^^^^^^ second mutable borrow occurs here 18 |     }    |     - first borrow ends here這是合情合理的,因為兩者anchor和node引用相同的結構,但實際上我并不關心anchor在摧毀它之后。怎么能back()是否正確地使用安全銹蝕來實現?
查看完整描述

3 回答

?
慕運維8079593

TA貢獻1876條經驗 獲得超5個贊

有可能.。但我希望我有一個更優雅的解決方案。

訣竅是不要向anchor,因此在兩個累加器之間進行操作:

  • 保存對當前節點的引用的
  • 另一個被分配給下一個節點的引用。

這使我想到:

impl Recursive {
    fn back(&mut self) -> &mut Link {
        let mut anchor = &mut self.root;

        loop {
            let tmp = anchor;
            if let Some(ref mut node) = *tmp {
                anchor = &mut node.next;
            } else {
                anchor = tmp;
                break;
            }
        }

        anchor    }}

不是很漂亮,但這是借閱檢查器可以得到的東西,所以wu\_(ツ)_/mr。

@ker通過創建一個未命名的臨時名稱,改進了這一點:

impl Recursive {
    fn back(&mut self) -> &mut Link {
        let mut anchor = &mut self.root;

        loop {
            match {anchor} {
                &mut Some(ref mut node) => anchor = &mut node.next,
                other => return other,
            }
        }
    }}

這里的訣竅是{anchor} 移動.的內容anchor進入執行匹配的未命名臨時表中。因此,在match我們不能從anchor但是從暫時的,讓我們自由修改anchor..參見相關的博客文章填充身份函數所做的(在銹病中).


查看完整回答
反對 回復 2019-07-03
?
富國滬深

TA貢獻1790條經驗 獲得超9個贊

原始代碼的工作方式是-是一次。非詞匯壽命啟用:

#![feature(nll)]type Link = Option<Box<Node>>;struct Node {
    next: Link}struct Recursive {
    root: Link}impl Recursive {
    fn back(&mut self) -> &mut Link {
        let mut anchor = &mut self.root;
        while let Some(node) = anchor {
            anchor = &mut node.next;
        }
        anchor    }}fn main() {}

非詞法生命周期提高了編譯器的借入檢查器的精度,使它能夠看到anchor不再使用。我們還可以簡化if let由于最近語言的變化。


查看完整回答
反對 回復 2019-07-03
  • 3 回答
  • 0 關注
  • 570 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號