如何以高效的方式绕过 "cannot borrow `*self` as mutable more than once at a time" 以进行基于代理的模拟?
How to circumvent "cannot borrow `*self` as mutable more than once at a time" in a performant way for agent based simulation?
我想在 Rust 中实现基于代理的模拟,但我 运行 进入了借用检查器。
代理应该生活在一个可变的网格中,在每个单元格中携带一个状态。
每个代理都带有一些可变状态。
通常,我会实现一组代理,例如作为从网格位置到代理的 HashMap
。
在模拟步骤中,我将遍历所有代理,然后根据代理自身的状态、该位置的网格状态以及附近其他代理的状态更新代理的状态。
虚构的示例可能如下所示:
use std::collections::HashMap;
struct Agent { // each agent carries some state
id: i64,
state: i32,
}
struct CellState { // some state of a grid cell
state: i64,
}
struct Chart {
agents: HashMap<usize, Agent>,
grid: Vec<CellState>,
}
impl Chart {
fn new(size: usize) -> Chart {
let mut agents = HashMap::new(); // generate hash and populate with 2 agents
agents.insert(10, Agent { id: 1, state: 1 });
agents.insert(11, Agent { id: 2, state: 0 });
let mut grid: Vec<CellState> = Vec::with_capacity(size);
Chart {
agents: agents,
grid: grid,
}
}
fn do_stuff(&mut self, agent: &mut Agent) {
// here we want to update the state of agent,
// based on the state of other agents in the grid
}
fn step_agents(&mut self) {
for (_, agent) in &mut self.agents {
self.do_stuff(agent);
}
}
}
fn main() {
let mut ch = Chart::new(128);
ch.step_agents();
}
此代码产生错误
error[E0499]: cannot borrow `*self` as mutable more than once at a time
--> src/main.rs:37:13
|
36 | for (_, agent) in &mut self.agents {
| ----------------
| |
| first mutable borrow occurs here
| first borrow later used here
37 | self.do_stuff(agent);
| ^^^^ second mutable borrow occurs here
我了解错误以及 Rust 编译器出现问题的原因。我不明白的是如何以高效的方式规避这个问题。
如果我不可变地借用对代理的引用,我就不能更新它的状态。在一个真实的例子中,代理会携带相当多的状态,因此克隆并不便宜。
实现这个的惯用 Rust 方法是什么?
拥有多个独占引用,在本例中指向同一个 HashMap
,确实违反了借用检查器的约束。
鉴于 Agent
显然复制起来并不便宜,我想你可能想研究一下用 std::cell::RefCell
包装 Agent
以动态借用这些值。
这是一个简单的例子:
use std::cell::RefCell;
use std::collections::HashMap;
#[derive(Debug, PartialEq)]
struct Agent {
id: i64,
state: i32,
}
struct CellState {
state: i64,
}
struct Chart {
agents: HashMap<usize, RefCell<Agent>>,
grid: Vec<CellState>,
}
impl Chart {
fn new(size: usize) -> Self {
let mut agents = HashMap::new();
agents.insert(1, RefCell::new(Agent { id: 1, state: 0 }));
agents.insert(2, RefCell::new(Agent { id: 2, state: 1 }));
let mut grid: Vec<CellState> = Vec::with_capacity(size);
Self { agents, grid }
}
fn do_stuff(&self, agent: &RefCell<Agent>) {
for other in self.agents.values().filter(|&other| agent != other) {
if other.borrow().state == 1 {
agent.borrow_mut().state += 1;
}
}
}
fn step_agents(&self) {
for agent in self.agents.values() {
self.do_stuff(agent);
}
}
}
fn main() {
let mut chart = Chart::new(128);
chart.step_agents();
for agent in chart.agents.values() {
println!("{:?}", agent);
}
}
由于 HashMap
以任意顺序访问,上面的 可以 然后 return:
RefCell { value: Agent { id: 1, state: 1 } }
RefCell { value: Agent { id: 2, state: 1 } }
我想在 Rust 中实现基于代理的模拟,但我 运行 进入了借用检查器。
代理应该生活在一个可变的网格中,在每个单元格中携带一个状态。
每个代理都带有一些可变状态。
通常,我会实现一组代理,例如作为从网格位置到代理的 HashMap
。
在模拟步骤中,我将遍历所有代理,然后根据代理自身的状态、该位置的网格状态以及附近其他代理的状态更新代理的状态。
虚构的示例可能如下所示:
use std::collections::HashMap;
struct Agent { // each agent carries some state
id: i64,
state: i32,
}
struct CellState { // some state of a grid cell
state: i64,
}
struct Chart {
agents: HashMap<usize, Agent>,
grid: Vec<CellState>,
}
impl Chart {
fn new(size: usize) -> Chart {
let mut agents = HashMap::new(); // generate hash and populate with 2 agents
agents.insert(10, Agent { id: 1, state: 1 });
agents.insert(11, Agent { id: 2, state: 0 });
let mut grid: Vec<CellState> = Vec::with_capacity(size);
Chart {
agents: agents,
grid: grid,
}
}
fn do_stuff(&mut self, agent: &mut Agent) {
// here we want to update the state of agent,
// based on the state of other agents in the grid
}
fn step_agents(&mut self) {
for (_, agent) in &mut self.agents {
self.do_stuff(agent);
}
}
}
fn main() {
let mut ch = Chart::new(128);
ch.step_agents();
}
此代码产生错误
error[E0499]: cannot borrow `*self` as mutable more than once at a time
--> src/main.rs:37:13
|
36 | for (_, agent) in &mut self.agents {
| ----------------
| |
| first mutable borrow occurs here
| first borrow later used here
37 | self.do_stuff(agent);
| ^^^^ second mutable borrow occurs here
我了解错误以及 Rust 编译器出现问题的原因。我不明白的是如何以高效的方式规避这个问题。
如果我不可变地借用对代理的引用,我就不能更新它的状态。在一个真实的例子中,代理会携带相当多的状态,因此克隆并不便宜。
实现这个的惯用 Rust 方法是什么?
拥有多个独占引用,在本例中指向同一个 HashMap
,确实违反了借用检查器的约束。
鉴于 Agent
显然复制起来并不便宜,我想你可能想研究一下用 std::cell::RefCell
包装 Agent
以动态借用这些值。
这是一个简单的例子:
use std::cell::RefCell;
use std::collections::HashMap;
#[derive(Debug, PartialEq)]
struct Agent {
id: i64,
state: i32,
}
struct CellState {
state: i64,
}
struct Chart {
agents: HashMap<usize, RefCell<Agent>>,
grid: Vec<CellState>,
}
impl Chart {
fn new(size: usize) -> Self {
let mut agents = HashMap::new();
agents.insert(1, RefCell::new(Agent { id: 1, state: 0 }));
agents.insert(2, RefCell::new(Agent { id: 2, state: 1 }));
let mut grid: Vec<CellState> = Vec::with_capacity(size);
Self { agents, grid }
}
fn do_stuff(&self, agent: &RefCell<Agent>) {
for other in self.agents.values().filter(|&other| agent != other) {
if other.borrow().state == 1 {
agent.borrow_mut().state += 1;
}
}
}
fn step_agents(&self) {
for agent in self.agents.values() {
self.do_stuff(agent);
}
}
}
fn main() {
let mut chart = Chart::new(128);
chart.step_agents();
for agent in chart.agents.values() {
println!("{:?}", agent);
}
}
由于 HashMap
以任意顺序访问,上面的 可以 然后 return:
RefCell { value: Agent { id: 1, state: 1 } }
RefCell { value: Agent { id: 2, state: 1 } }