Rust 在Rc中借用T<;RefCell<;T>&燃气轮机;

Rust 在Rc中借用T<;RefCell<;T>&燃气轮机;,rust,borrowing,Rust,Borrowing,我正试着做这样的事情 use std::cell::{RefCell,Ref,RefMut}; use std::rc::Rc; struct Entity; struct Tile { entity: Option<Rc<RefCell<Entity>>> } impl Tile { pub fn try_read_entity<'a>(&'a self) -> Option<Ref<'a, Ent

我正试着做这样的事情

use std::cell::{RefCell,Ref,RefMut};
use std::rc::Rc;

struct Entity;

struct Tile {
    entity: Option<Rc<RefCell<Entity>>>
}

impl Tile {
    pub fn try_read_entity<'a>(&'a self) -> Option<Ref<'a, Entity>> {
        self.entity.map(|e| e.borrow())
    }
}
使用std::cell::{RefCell,Ref,RefMut};
使用std::rc::rc;
结构实体;
结构砖{
实体:期权
}
impl瓷砖{

pub fn try_read_entity Option这是
Option::map()方法的签名:

fn map<U, F>(self, f: F) -> Option<U>
    where F: FnOnce(T) -> U

这是
Option::map()
方法的签名:

fn map<U, F>(self, f: F) -> Option<U>
    where F: FnOnce(T) -> U

这是
Option::map()
方法的签名:

fn map<U, F>(self, f: F) -> Option<U>
    where F: FnOnce(T) -> U

这是
Option::map()
方法的签名:

fn map<U, F>(self, f: F) -> Option<U>
    where F: FnOnce(T) -> U