- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在实现深度优先搜索。
它的数据结构是用HashMap
实现的,如“当前节点”->“下一个节点”。
为了避免循环图中的循环,我的程序在标记时尝试从 HashMap
的值(Vec
of next depth Vertices)中删除一个节点。
当通过 get_mut
操作 HashMap
对象的值时,我注意到它的整个 HashMap
对象的所有权以后不能移动。
#[derive(Debug, PartialEq, Eq, Hash, Copy, Clone)]
pub enum Vertex<A> {
Start,
Goal,
Vertex(A),
}
pub fn search(
curr: &Vertex<i32>,
mut acc: Vec<Vertex<i32>>,
mut field: HashMap<Vertex<i32>, Vec<Vertex<i32>>>,
goal: &Vertex<i32>,
) -> Vec<Vertex<i32>> {
match field.get_mut(&curr) {
// when reached goal
_ if *curr == *goal => {
acc.push(*curr);
acc
}
// when vertices found
Some(ns) => {
if let Some(next) = ns.pop() {
// go to next depth
acc.push(*curr);
// trying to move "field"'s ownership to next recursive call here but it fails because "field.get_mut(&curr)" is done at match expression
search(&next, acc, field, goal)
} else if let Some(prev) = acc.pop() {
// backtrack
search(&prev, acc, field, goal) // ditto
} else {
// no answer
vec![]
}
}
// when next is not registered
None => vec![],
}
}
如评论中所写,递归调用中存在非法移动。
因此我在编译时收到以下消息。
18 | let result: Vec<Vertex<i32>> = match field.get_mut(&curr) {
| ----- borrow of `field` occurs here
...
29 | _search(&next, acc, field, goal) // to be fixed
| ^^^^^ move out of `field` occurs here
error[E0505]: cannot move out of `field` because it is borrowed
--> src/algorithm/search/graph/depth_first.rs:31:37
|
18 | let result: Vec<Vertex<i32>> = match field.get_mut(&curr) {
| ----- borrow of `field` occurs here
...
31 | _search(&prev, acc, field, goal) // to be fixed
| ^^^^^ move out of `field` occurs here
您能否提出解决此问题或重新设计整个代码的好方法?
最佳答案
您的代码编译为稳定的 Rust 2018(或带有 #![feature(nll)]
的 nightly Rust 2015)。
要使其在稳定的 Rust 2015 上运行,您可以将递归调用移到借用 field
的范围之外。一种方法如下:
pub fn _search(
curr: &Vertex<i32>,
mut acc: Vec<Vertex<i32>>,
mut field: HashMap<Vertex<i32>, Vec<Vertex<i32>>>,
goal: &Vertex<i32>,
) -> Vec<Vertex<i32>> {
let v = match field.get_mut(&curr) {
// when reached goal
_ if *curr == *goal => {
acc.push(*curr);
return acc;
}
// when vertices found
Some(ns) => {
if let Some(next) = ns.pop() {
// go to next depth
acc.push(*curr);
next
} else if let Some(prev) = acc.pop() {
// backtrack
prev // ditto
} else {
// no answer
return vec![];
}
}
// when next is not registered
None => return vec![],
};
_search(&v, acc, field, goal)
}
关于rust - 修改 HashMap 对象中的特定值,以防以后可以移动其对象的所有权,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54036921/
我一直在此处和互联网上寻找一种解决方案,当查询未从数据库返回任何内容时,在 highcharts 的 DIV 上显示无数据图片。 mysql查询 $GetData= "SELECT COUNT( *
我正在用 C++ 编写一个记录器,为了简化输入行,我使用 vsnprintf 函数来构建日志行 void CLogger::RegManLog(const LogLevel & logLevelMas
我想创建一个输入 int 值的优先级列表。我知道该列表通常具有升序的自然顺序(如果我弄错了,请纠正我)。但作为新手,我想做的就是对相同的值进行排序。 假设我输入了值 3、6、4、1、2、4、7、4,我
我希望这张图片在左侧。 有什么帮助吗?? 最佳答案 @安尼施·米塔尔 我在这里描述您在两种布局中的问题解决方案: 1> 线性布局 这段代码放在主线性布局中
我正在开发一个网站,本质上需要大量的动态数据。我的服务器上无法承受如此庞大的数据,但有很多网站使用它。让我用一个例子来澄清。 假设网站 hotel.com 拥有所有酒店的列表。它允许用户使用ajax查
我需要编写一个 node.js 代码来启动 bitcoind 守护进程命令并继续监视它,如果它崩溃,进程应该重新启动。 我知道有命令行 npm 模块,例如 forever、forver-monitor
我想声明如下: var block = blocksById.FirstOrDefault(X => X.Value == tracResult.ID).Key 我的问题是如果我有 FirstOrDe
第一件事。希望我的标题没有误导。我尽力表达它。 现在,请看下面的代码。案例 1 非常简单。这两种情况都按预期工作。我的问题是为什么编译器允许案例 2?是否有需要的特定场景。我想不出一个。 interf
我是一名优秀的程序员,十分优秀!