loops - 当可变引用存在时传递不可变引用
问题描述
我有一个for
循环遍历一片Point
结构。s将Point
在循环中修改一些字段,因此包含循环的函数需要对切片的可变引用。
当我需要将指向切片的(不可变)引用传递给 for 循环中迭代可变引用的函数时,就会出现问题:
#[derive(Debug)]
struct Point {
x: i32,
y: i32,
}
fn main() {
let mut grid = vec![];
grid.push(Point { x: 10, y: 10 });
grid.push(Point { x: -1, y: 7 });
calculate_neighbors(&mut grid);
}
fn calculate_neighbors(grid: &mut [Point]) {
for pt in grid.iter_mut() {
pt.x = nonsense_calc(grid);
}
}
#[allow(unused_variables)]
fn nonsense_calc(grid: &[Point]) -> i32 {
unimplemented!();
}
error[E0502]: cannot borrow `*grid` as immutable because it is also borrowed as mutable
--> src/main.rs:18:30
|
17 | for pt in grid.iter_mut() {
| ---------------
| |
| mutable borrow occurs here
| mutable borrow used here, in later iteration of loop
18 | pt.x = nonsense_calc(grid);
| ^^^^ immutable borrow occurs here
编译器抱怨grid
不能作为不可变借用,因为可变借用已经存在。这是正确的,我可以看到它试图阻止的问题,但是我该如何实现我需要做的事情呢?理想情况下,我不必创建 的副本grid
,因为这可能很昂贵。
解决方案
避免借用数组进行迭代的解决方案是使用索引:
fn calculate_neighbors(grid: &mut [Point]) {
for i in 0..grid.len() {
grid[i].x = nonsense_calc(grid);
}
}
推荐阅读
- amazon-ec2 - create aws_instance without ebs_block_device
- javascript - Webpack: "Cannot read property 'fn' of undefined" when importing owl.carousel
- c# - How to set camera permissions for iOS simulator using Appium Execute Mobile Command (ExecuteScript) - C#
- isabelle - 如何在伊莎贝尔中止证明?
- arangodb - 在 arangodb 中的数组内过滤不起作用
- xamarin.android - C xamarin JsonConvert.DeserializeObject ERREUR
- python - How to login BarChart website with Python?
- r - How can I generate a scatter plot from lmm in R?
- git - How can I find where a git submodule was removed?
- django - 将 Django 应用程序部署到 heroku。调试错误