- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
改变结构)并将其缓存在 Rust 中返回。 我收到以下错误消息: error[E0499]: -6ren">
我必须迭代键,通过键在 HashMap 中找到值,可能在找到的结构中作为值进行一些繁重的计算(惰性 => 改变结构)并将其缓存在 Rust 中返回。
我收到以下错误消息:
error[E0499]: cannot borrow `*self` as mutable more than once at a time
--> src/main.rs:25:26
|
23 | fn it(&mut self) -> Option<&Box<Calculation>> {
| - let's call the lifetime of this reference `'1`
24 | for key in vec!["1","2","3"] {
25 | let result = self.find(&key.to_owned());
| ^^^^ `*self` was mutably borrowed here in the previous iteration of the loop
...
28 | return result
| ------ returning this value requires that `*self` is borrowed for `'1`
这是
code in playground 。
use std::collections::HashMap;
struct Calculation {
value: Option<i32>
}
struct Struct {
items: HashMap<String, Box<Calculation>> // cache
}
impl Struct {
fn find(&mut self, key: &String) -> Option<&Box<Calculation>> {
None // find, create, and/or calculate items
}
fn it(&mut self) -> Option<&Box<Calculation>> {
for key in vec!["1","2","3"] {
let result = self.find(&key.to_owned());
if result.is_some() {
return result
}
}
None
}
}
self
和结构),因为可能的计算会改变它最佳答案
您不能使用独占访问进行缓存。您不能将 Rust 引用视为通用指针(顺便说一句:&String
和 &Box<T>
是双重间接,在 Rust 中非常不习惯。使用 &str
或 &T
进行临时借用)。&mut self
意味着不只是可变的,而是排他的和可变的,所以你的缓存支持只返回一项,因为它返回的引用必须保持 self
只要它存在就“锁定”。
您需要让借用检查员相信 find
下次调用时返回不会突然消失。目前没有这样的保证,因为接口(interface)不会阻止你调用例如items.clear()
(借用检查器检查函数的接口(interface)允许什么,而不是函数实际执行什么)。
您可以使用 Rc
来做到这一点。 ,或使用实现 a memory pool/arena 的 crate .
struct Struct {
items: HashMap<String, Rc<Calculation>>,
}
fn find(&mut self, key: &str) -> Rc<Calculation>
这样,如果您克隆
Rc
,它会在需要的时间内存活,与缓存无关。
struct Struct {
items: RefCell<HashMap<…
}
这将允许您内存
find
使用共享借用而不是独占借用的方法:
fn find(&self, key: &str) -> …
对于方法的调用者来说,这更容易使用。
关于rust - 如何在 Rust 中修复 ".. was mutably borrowed here in the previous iteration of the loop"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66167634/
我正在研究 clojure 库,当时我注意到一个可变字段被注释为 ^:unsynchronized-mutable 。可变是可变的,但我不知道不同步部分意味着什么,所以我读了 docs ,其中包含:
对于 put和 get操作 OpenHashMap跑赢大盘HashMap约5次:https://gist.github.com/1423303 HashMap时是否有任何情况应该优先于 OpenHas
下面的代码打开一个 .txt 文件并计算词频。我正在看一本书,但我感到困惑: 我的问题在这里: filename := os.Args[1] frequencyForWord := map[strin
我正在尝试添加两个 BitSet对象在一起(改变其中之一)。这应该是位集的有效操作。但似乎唯一能做到这一点的操作是 ++= .查看源代码,这似乎并没有区别对待添加位集。 在 Scala 2.9.1 中
trait Output { fn write(&mut self, text: &str); } struct DummyOutput {} impl Output for DummyOut
我正在用函数改变字符串值(我知道这是非常不安全和危险的): public static void reverse(String s) { try { Field val = S
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: C++ 'mutable' keyword class student { mutable int r
在《Java并发实践》一书中,在谈到“锁定和可见性”时,作者说道: We can now give the other reason for the rule requiring all thread
我想知道如何在 C99 中基本上声明 (a const pointer to (a mutable pointer to (a const type)))。 假设我有这个调用站点: const uin
我在考c++,下面有一个奇怪的代码块,我看不懂。这里,i 是一个 int 而 code 是一个 char: [=,&i]()mutable { i++; code = 'b'; std::
编译: []{}; 这也是: []() mutable {}; 但是对于这段代码,编译器会向我抛出错误信息: [] mutable {}; ^~~~~~~ error: lambda requi
我编写了如下所示的映射,即我的 mutable.HashMap 实现。 class SampleMap() extends mutable.HashMap[String, (Any, BigInt)]
我理解事物的方式是“变量”一词指的是重新分配引用的能力。 “常量”意味着不能重新分配引用。本质上是 Java 中 final 与 not 的区别。 var something = new obj()
是否有一种优雅的方式来更新 Map 中已经存在的值? 这看起来太可怕了: val a = map.get ( something ) if ( a != null ) // case .. excus
我正在尝试从向量中删除元素(如果存在): use std::collections::HashMap; fn test(map: HashMap>, department: String, emplo
在尝试克隆可变集合时,我最初的方法是对 mutable.Cloneable 特征使用 clone() 方法。但是,这取决于创建引用副本的 java.Object.clone 实现,而不是深拷贝。通过测
在尝试克隆可变集合时,我最初的方法是对 mutable.Cloneable 特征使用 clone() 方法。但是,这取决于创建引用副本的 java.Object.clone 实现,而不是深拷贝。通过测
MultiMap 的 addBinding 似乎不保留绑定(bind)到同一键的值的插入顺序,因为它使用的底层机制是 HashSet。使用 MultiMap 保留插入顺序的惯用方法是什么? 最佳答案
当我尝试在可变 Map 中插入一个元素时,我希望这个元素插入到我的 Map 而不是返回 Map(如 PF,不可变对象(immutable对象) ecc ...)出于这个原因,我使用了可变集合,但在我的
我意识到结构是不可变的,改变结构是邪恶的,改变结构中值的正确方法是创建新实例。但是,我不清楚新实例与允许结构可变的内存和性能方面/问题。 假设我有结构, struct Vehicle { pu
我是一名优秀的程序员,十分优秀!