问题描述
我在 Rust 中有以下代码:
pub struct RegExpFilter {
...
regexp_data: RefCell<Option<RegexpData>>,...
}
struct RegexpData {
regexp: regex::Regex,string: String
}
...
pub fn is_regexp_compiled(&self) -> bool {
self.regexp_data.borrow().is_some()
}
pub fn compile_regexp(&self) -> RegexpData {
...
}
fn regexp(&self) -> ®ex::Regex {
if !self.is_regexp_compiled() { // lazy computation that mutates the struct
self.regexp_data.replace(Some(self.compile_regexp()));
}
&self.regexp_data.borrow().as_ref().unwrap().regexp
}
pub fn matches(&self,location: &str) -> bool {
self.regexp().find(location)
}
regexp 是惰性计算的,捕获 &mut self
我不需要所以使用 RefCell
。
我收到以下消息:
&self.regexp_data.borrow().as_ref().unwrap().regexp
| ^-------------------------^^^^^^^^^^^^^^^^^^^^^^^^^
| ||
| |temporary value created here
| returns a value referencing data owned by the current function
编译器消息似乎很明确:Ref
是由 borrow()
临时创建并返回到外部。但是我相信 Option
(self.regexp_data
) 由 RefCell
拥有,它由结构本身拥有,所以在内部使用它应该没问题(因为函数不是 {{1} }).
我也尝试了以下操作(但失败并显示相同的消息)
pub
我该如何解决?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)