- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图从结构中使用BTreeMap(或HashMap),但我不能,因为它一直在提示所有权问题。
cannot move out of `self.vertices` which is behind a shared reference
move occurs because `self.vertices` has type `std::collections::BTreeMap<u32, std::vec::Vec<u32>>`, which does not implement the `Copy` trait
help: consider borrowing here: `&self.vertices`rustc(E0507)
digraph.rs(13, 17): move occurs because `self.vertices` has type `std::collections::BTreeMap<u32, std::vec::Vec<u32>>`, which does not implement the `Copy`
在这个阶段,我完全感到困惑。
use std::collections::BTreeMap;
pub struct DirectedGraph {
vertices: BTreeMap<u32, Vec<u32>>
}
impl DirectedGraph {
pub fn new() -> DirectedGraph {
DirectedGraph { vertices: BTreeMap::new() }
}
pub fn add_edge(&self, vertex: u32, edge: u32) {
let v = &self.vertices;
v.entry(vertex).or_insert(vec!()).push(edge);
}
pub fn num_vertices(&self) -> usize {
self.vertices.len()
}
}
error[E0596]: cannot borrow `*v` as mutable, as it is behind a `&` reference
--> src\digraph.rs:14:9
|
13 | let v =&self.vertices;
| -------------- help: consider changing this to be a mutable reference: `&mut self.vertices`
14 | v.entry(vertex).or_insert(vec!()).push(edge);
| ^ `v` is a `&` reference, so the data it refers to cannot be borrowed as mutable
error: aborting due to previous error
For more information about this error, try `rustc --explain E0596`.
最佳答案
问题似乎是您正在尝试对尚未标记为可变的内容进行突变。 add_edge
方法显然必须对结构进行突变,但是您有一个&self
接收器而不是&mut self
。进行更改后,代码将编译:
use std::collections::BTreeMap;
pub struct DirectedGraph {
vertices: BTreeMap<u32, Vec<u32>>
}
impl DirectedGraph {
pub fn new() -> DirectedGraph {
DirectedGraph { vertices: BTreeMap::new() }
}
pub fn add_edge(&mut self, vertex: u32, edge: u32) {
self.vertices
.entry(vertex)
.or_insert(Vec::new())
.push(edge);
}
pub fn num_vertices(&self) -> usize {
self.vertices.len()
}
}
playground
关于rust - 结构的可变性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65162695/
我遇到了 python 问题..我有一个二叉树节点类型: class NODE: element = 0 leftchild = None rightc
这个问题已经有答案了: Local (?) variable referenced before assignment [duplicate] (3 个回答) 已关闭 4 年前。 我有一些来自初学者编
class TestClass(object): def __init__(self): self.value = 100 self.x = lambda: s
谁能解释为什么下面的代码会编译,但如果我注释掉一行,那么它不会,即使代码本质上在做同样的事情? struct OtherStruct { x: i32, } struct Inner { bl
我有一个在同一主机上运行的 TCP 客户端和服务器。客户端发送消息,服务器确认该消息,然后客户端发送下一条消息。在任何时刻,都只有一条未处理的未确认消息。消息大小为 1KB。家庭计算机运行CentOs
我是 Panda 的 Dataframe 的新手,如果有人可以通过以下示例向我简要讨论 DataFrame 的可变性,我将不胜感激: d1=pd.date_range('1/1/2016',perio
在 C++ 编程语言第 4 版的第 16.2.9.4 节“通过间接实现的可变性”中,有一个使用间接代替 mutable 关键字进行惰性求值的示例的草图。 struct cache { bool
我对 Haskell 了解不多,但从我读到的关于计算的可变性(例如:函数返回函数、复杂的 monad 和函数等)的内容来看,你似乎可以做很多元编程,即使在运行时。 如果像函数和 monad 这样的一切
在 Python 中,globals() 返回全局符号表的表示,而 locals() 返回本地状态的表示。虽然两者都返回字典,但对 globals() 的更改会在全局符号表中生效,而对 locals(
我正在关注斯坦福算法 MOOC 并尝试使用 Haskell 解决问题。许多算法需要大量的数据处理,纯解决方案的运行速度比人们为命令式语言引用的基准要慢得多。所以我觉得我需要使用可变数据结构。 大多数
在 react 井字游戏中 tutorial ,为什么他们必须使用 Array.slice()? handleClick(i) { const squares = this.state.square
在 react 井字游戏中 tutorial ,为什么他们必须使用 Array.slice()? handleClick(i) { const squares = this.state.square
在 C# 中,我想制作“智能”枚举,这在 Java 中是可能的,其中有更多信息附加到枚举值,而不仅仅是底层 int。我偶然发现了一个创建类(而不是枚举)的方案,如以下简单示例所示: public se
我是一名优秀的程序员,十分优秀!