无法将`* x`借为可变的,因为它也被借为不可变的 [英] Cannot borrow `*x` as mutable because it is also borrowed as immutable
问题描述
我正在做一个组合优化项目来学习Rust,但遇到一个我无法解决自己的问题...
我有2个功能:
pub fn get_pareto_front_offline<'a>(scheduling_jobs: &'a Vec<Vec<u32>>, costs_vector: &'a Vec<(u32, u32)>) -> Vec<(&'a Vec<u32>, &'a (u32, u32))> {
// ...
}
和
pub fn pareto_approach_offline<'a>(list_of_jobs: &'a mut Vec<Vec<u32>>, neighborhood: &'a mut Vec<Vec<u32>>, costs: &'a Vec<(u32, u32)>) -> Vec<(&'a Vec<u32>, &'a (u32, u32))> {
let pareto_front = get_pareto_front_offline(neighborhood, costs);
loop {
if pareto_front == vec![] {
break;
}
neighborhood.clear();
for front in pareto_front.iter() {
neighborhood.push((front.0).clone());
}
}
pareto_front
}
我遇到了一个问题,因为编译器告诉我:
cannot borrow '*neighborhood' as mutable because it is also borrowed as immutableat line 15 col 9
cannot borrow '*neighborhood' as mutable because it is also borrowed as immutableat line 19 col 13
您正在尝试做根本上不可能的事情.
调用get_pareto_front_offline
时,会将neighborhood
的重新借用传递给该函数.必须重新维护此 ,以使pareto_front
保持有效.换句话说,只要pareto_front
存在,编译器将 not 不允许您以任何方式访问neighborhood
.
这是一件好事,因为您随后尝试尝试清除我们的neighborhood
,这肯定会肯定使pareto_front
无效,可能导致使用后使用和破坏程序的状态.
目前尚不清楚您要尝试做什么;但是您不能用这种方式.
顺便说一句,即使编译了该循环,该循环也可能永远不会结束运行:因为您永远不会修改pareto_front
,所以永远不会满足您的终止条件(pareto_front == vec![]
);它会立即停止,或者永远运行.
摆脱借款问题的最简单方法是制作事物的副本,这样您就不需要长期的借款.如果get_pareto_front_offline
返回了Vec<(Vec<u32>, (u32, u32))>
,则不会出现此问题.或者,将其修改为代码,以在调用get_pareto_front_offline
时不触碰neighborhood
.
I'm making a Combinatory Optimization project to learn Rust and I've got a problem I cannot resolve myself...
I've got 2 functions :
pub fn get_pareto_front_offline<'a>(scheduling_jobs: &'a Vec<Vec<u32>>, costs_vector: &'a Vec<(u32, u32)>) -> Vec<(&'a Vec<u32>, &'a (u32, u32))> {
// ...
}
and
pub fn pareto_approach_offline<'a>(list_of_jobs: &'a mut Vec<Vec<u32>>, neighborhood: &'a mut Vec<Vec<u32>>, costs: &'a Vec<(u32, u32)>) -> Vec<(&'a Vec<u32>, &'a (u32, u32))> {
let pareto_front = get_pareto_front_offline(neighborhood, costs);
loop {
if pareto_front == vec![] {
break;
}
neighborhood.clear();
for front in pareto_front.iter() {
neighborhood.push((front.0).clone());
}
}
pareto_front
}
I've got a problem because the compiler tells me:
cannot borrow '*neighborhood' as mutable because it is also borrowed as immutableat line 15 col 9
cannot borrow '*neighborhood' as mutable because it is also borrowed as immutableat line 19 col 13
You're trying to do something fundamentally impossible.
When you call get_pareto_front_offline
, you pass a re-borrowing of neighborhood
into that function. This re-borrow must be maintained in order for pareto_front
to remain valid. In other words, as long as pareto_front
exists, the compiler will not allow you to access neighborhood
in any fashion whatsoever.
This is a good thing, because you then proceed to try and clear our neighborhood
, which would almost certainly invalidate pareto_front
, likely leading to use-after-free and corrupting your program's state.
It's not clear what it is you're attempting to do; but you cannot do it this way.
As an aside, even if it compiled, that loop would probably never finish running: your termination condition (pareto_front == vec![]
) will never be satisfied because you never modify pareto_front
; it'll either stop immediately, or run forever.
The simplest way to get out from under borrowing problems is to make copies of things, so that you don't need a long-lived borrow; if get_pareto_front_offline
returned a Vec<(Vec<u32>, (u32, u32))>
instead, you wouldn't have this issue. That, or modify to code to not touch neighborhood
once you call get_pareto_front_offline
.
这篇关于无法将`* x`借为可变的,因为它也被借为不可变的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!