尝试遍历网格结构会导致“借入的值寿命不足”

时间:2018-07-06 05:25:48

标签: iterator rust borrowing

我正在尝试创建一个可以存储监视器位置数据的数据结构。

我采用的方法是每个屏幕都引用存储在HashMap<Direction, Rc<RefCell<Screen>>>中的4个邻居。

我正在努力的是如何在一个方向上“移动” /迭代直到到达网格的边缘:

use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;

#[derive(Debug, Copy, Clone, Default)]
pub struct ScreenCoord {
    left: i32,
    right: i32,
    bottom: i32,
    top: i32,
}

impl ScreenCoord {
    fn new_primary_from_dimensions(width: i32, height: i32) -> ScreenCoord {
        ScreenCoord {
            left: 0,
            right: width,
            top: 0,
            bottom: height,
        }
    }
}

pub struct ScreenNetwork {
    primary_screen: Rc<RefCell<Screen>>,
}

impl ScreenNetwork {
    pub fn new(width: i32, height: i32) -> ScreenNetwork {
        ScreenNetwork {
            primary_screen: Rc::new(RefCell::new(Screen::new(
                ScreenCoord::new_primary_from_dimensions(width, height),
            ))),
        }
    }

    pub fn add_screen(&mut self, new_width: i32, new_height: i32, direction: Direction) {
        let mut new_scrn = Screen::new(ScreenCoord::new_primary_from_dimensions(
            new_width, new_height,
        ));
        let mut original_screen = &self.primary_screen;

        while let Some(next) = original_screen.borrow().neighbours.get(&direction) {
            original_screen = next;
        }

        // Do stuff with original_screen

        // new_scrn.neighbours.insert(Direction::from_u8((direction.clone() as u8) ^ 0b11).unwrap(), original_screen.clone());
        // original_screen.borrow_mut().neighbours.insert(direction, Rc::new(RefCell::new(new_scrn)));
    }
}

/// Screen with information about adjacent screens
#[derive(Default, Debug)]
pub struct Screen {
    neighbours: HashMap<Direction, Rc<RefCell<Screen>>>,
    coordinates: ScreenCoord,
}

impl Screen {
    pub fn new(coord: ScreenCoord) -> Screen {
        Screen {
            coordinates: coord,
            neighbours: HashMap::new(),
        }
    }
}

#[derive(Debug, PartialEq, Hash, Eq, Clone)]
pub enum Direction {
    Left = 0,
    Top,
    Bottom,
    Right,
}

fn main() {}

playground

error[E0597]: borrowed value does not live long enough
  --> src/main.rs:43:32
   |
43 |         while let Some(next) = original_screen.borrow().neighbours.get(&direction) {
   |                                ^^^^^^^^^^^^^^^^^^^^^^^^                          - temporary value dropped here while still borrowed
   |                                |
   |                                temporary value does not live long enough
...
51 |     }
   |     - temporary value needs to live until here

我了解为什么会发生错误,但是我不知道如何解决该错误。我试图克隆original_screennext,但是编译器抱怨说我在尝试借用original_screen的同时仍然借用它。

1 个答案:

答案 0 :(得分:3)

您正在使用Rc,因此请充分利用它。一般来说,引用Rc就是问题。

首先尝试:在需要时克隆Rc

let mut original_screen = self.primary_screen.clone();

while let Some(next) = original_screen.borrow().neighbours.get(&direction) {
    original_screen = next.clone();
}

失败,并显示以下消息:

  

无法借用original_screen,因为它是借来的

足够正确:在while条件下借阅不会让您对其进行更新,但可以轻松地对其进行重写:

loop {
    if let Some(next) = original_screen.borrow().neighbours.get(&direction) {
         original_screen = next.clone()
    } else {
        break;
    }
}

该代码是等效的,实际上,它失败并显示相同的错误消息!但是现在我们更接近解决方案了:

loop {
    let next = if let Some(next) = original_screen.borrow().neighbours.get(&direction) {
         next.clone()
    } else {
        break;
    };
    original_screen = next;
}

现在可以编译,因为分配是在释放借项之后完成的。