- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的 Graph
对象存在生命周期/借用问题。
fn main() {
let mut g = Graph {
nodePointer: &mut 0,
edgePointer: &mut 0,
nodes: &mut Vec::new(),
edges: &mut Vec::new(),
};
let node1 = g.add_node((1, 1));
let node2 = g.get_node(0);
}
pub struct Graph<'a> {
pub nodePointer: &'a mut usize,
pub edgePointer: &'a mut usize,
pub nodes: &'a mut Vec<Node>,
pub edges: &'a mut Vec<Edge>,
}
impl<'a> Graph<'a> {
pub fn add_node(&'a mut self, data: (u64, u64)) -> usize {
let id: usize = *self.nodePointer;
self.nodes.push(Node {
id: id,
datum: data,
});
*self.nodePointer += 1;
return id;
}
pub fn get_node(&'a mut self, id: usize) -> &'a Node {
return &self.nodes[id];
}
pub fn add_edge(&'a mut self, source: u64, target: u64, weight: u16) -> usize {
let id: usize = *self.nodePointer;
self.edges.push(Edge {
id: id,
source,
target,
weight,
});
*self.edgePointer = *self.edgePointer + 1;
return id;
}
}
pub struct Node {
pub id: usize,
pub datum: (u64, u64),
}
pub struct Edge {
pub id: usize,
pub source: u64,
pub target: u64,
pub weight: u16,
}
error[E0499]: cannot borrow `g` as mutable more than once at a time
--> src/main.rs:9:17
|
8 | let node1 = g.add_node((1, 1));
| - first mutable borrow occurs here
9 | let node2 = g.get_node(0);
| ^ second mutable borrow occurs here
10 | }
| - first borrow ends here
最佳答案
您的问题源于对生命周期的滥用,特别是在您对 add_node
的签名中:
pub fn add_node(&'a mut self, data: (u64, u64)) -> usize
在此签名中,您声明 add_node
需要 &'a mut self
在 Graph<'a>
上;换句话说,你是在告诉 Rust 这个方法需要在图的生命周期结束前不能被删除的可变借用,'a
.但由于它是图本身持有对图的引用,因此唯一会删除该引用的时间是图本身被删除时。
自 add_node
不需要您返回对结构中任何对象的引用,保留该借用是无关紧要的。如果你改变你的 add_node
删除显式生命周期的方法:
pub fn add_node(&mut self, data: (u64, u64)) -> usize
然后您的示例不再引发错误,因为 add_node
现在只借self
直到它完成函数。 (在引擎盖下,这有效地创建了第二个生命周期 'b
并将签名变为 &'b mut self
)
参见 the playground为证。
关于rust - 在调用 &'a mut self 方法后,不能一次多次借用可变变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49928848/
Rust 中 *、&、mut、&mut、ref、ref mut 的用法和区别 在 Rust 中,*、ref、mut、& 和 ref mut 是用于处理引用、解引用和可变性的关
这个问题在这里已经有了答案: How to avoid writing duplicate accessor functions for mutable and immutable referenc
我正在尝试使用指向我的结构的指针调用 pthread_join,以便 C 线程可以将结构填充到我指向它的内存中。 (是的,我知道这是非常不安全的..) pthread_join的函数签名: pub u
这个问题在这里已经有了答案: is it possible to filter on a vector in-place? (4 个答案) Is there a way to drain parts
这个问题在这里已经有了答案: is it possible to filter on a vector in-place? (4 个答案) Is there a way to drain parts
以下代码编译失败,因为 MutRef 不是Copy。无法复制,因为&'a mut i32 不是Copy。有什么方法可以让 MutRef 具有与 &'a mut i32 相似的语义吗? 这样做的动机是能
我想转换数组。 例子: func()-> *mut *mut f32; ... let buffer = func(); for n in 0..48000 { buffer[0][n] =
我想对 &mut [u8] 进行一些操作。 在我的测试代码中我有: #[test] fn test_swap_bytes() { let input: &[u8] = b"abcdef";
首先,我不是在问 &mut 之间有什么区别?和 ref mut本身。 我问是因为我想: let ref mut a = MyStruct 与相同 let a = &mut MyStruct 考虑从函数
这个问题在这里已经有了答案: What's the difference between placing "mut" before a variable name and after the ":"
我正在从书中学习 Rust,并且我正在处理第 8 章末尾的练习,但是关于将单词转换为 Pig Latin 的练习我遇到了困难。我想具体看看我是否可以将 &mut String 传递给一个接受 &mut
这段代码: let mut a2 = 99; let b: *mut i32 = &mut a2; *b = 11; // does not compile , even after unsafe {
我正在尝试了解借用检查器。我有一个带有签名的函数 fn SerializeChar(&mut self, value: &mut u8) 我想从 u8 和 i8 获取数据,因为我不关心符号: let
在下面的示例中,t1 编译但 t2 不编译。 &mut &stream有什么特别之处吗?我认为 Deref 不会起作用。 use std::net::TcpStream; fn t1() {
我正在围绕 C 库编写一个 Rust 包装器,同时我正在尝试利用 The Book 中提到的“可空指针优化” , 但我找不到转换 Option 的好方法至 *const T和 Option至 *mut
我试图在 Rust 中实现一个链表,但我在理解这两个函数之间的区别时遇到了一些困难: enum List { Nil, Cons(T, Box>) } fn foo(list: &mu
根据文档,ManuallyDrop是一个零成本包装器。这是否意味着我可以取消引用指向 ManuallyDrop 的原始指针从原始指针转换为 T ? 最佳答案 ManuallyDrop 是 declar
我一直在尝试用 Rust 编写 Redis 模块。这是我第一次尝试使用 Rust FFI 和绑定(bind)。如何在不破坏 Redis 指针的情况下调用此方法并在 Rust 中得到一个数据值? ext
我很难理解以下代码为何具有2种不同的行为: pub fn get(&self, idx: usize) -> &T { let arr = unsafe { core::slice::from
在 documentation为 Unwindsafe我们有: Types such as &mut T and &RefCell are examples which are not unwind
我是一名优秀的程序员,十分优秀!