【发布时间】:2017-10-05 01:33:21
【问题描述】:
我正在尝试在 Rust 中实现循环链接数据结构。我的Nodes 定义为:
#[derive(Debug)]
enum Node<'a> {
Link(&'a Node<'a>),
Leaf,
}
我正在尝试构建一个像这样的最小结构(额外的括号以获得更好的生命周期可见性):
fn main() {
let placeholder = Node::Leaf;
{
let link1 = Node::Link(&placeholder);
{
let link2 = Node::Link(&link1);
println!("{:?}", link2);
} // link2 gets dropped
} // link1 gets dropped
}
这可行,但现在我不知道如何将占位符替换为对 link2 的引用以“关闭循环”。我试过这个,但它不起作用,因为我不能分配给link1,这是借用了上面的行,并且因为link2 会超出范围,而它仍然被link1 引用:
let placeholder = Node::Leaf;
{
let mut link1 = Node::Link(&placeholder);
{
let link2 = Node::Link(&link1);
link1 = Node::Link(&link2);
println!("{:?}", link2);
} // link2 gets dropped
} // link1 gets dropped
error: `link2` does not live long enough
--> src/main.rs:15:9
|
13 | link1 = Node::Link(&link2);
| ----- borrow occurs here
14 | println!("{:?}", link2);
15 | } // link2 gets dropped
| ^ `link2` dropped here while still borrowed
16 | } // link1 gets dropped
| - borrowed value needs to live until here
error[E0506]: cannot assign to `link1` because it is borrowed
--> src/main.rs:13:13
|
12 | let link2 = Node::Link(&link1);
| ----- borrow of `link1` occurs here
13 | link1 = Node::Link(&link2);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ assignment to borrowed `link1` occurs here
我可以尝试通过使用 Rcs 来避免这些生命周期问题,但这听起来会破坏 Rust 的 0-runtime-cost 生命周期管理的目的。
将所有节点放入 Vec 并仅直接引用它的另一种尝试也不起作用:
use std::ops::IndexMut;
let mut store = Vec::new();
store.push(Node::Leaf);
store.push(Node::Link(&store[0]));
store.push(Node::Link(&store[1]));
*store.index_mut(1) = Node::Link(&store[2]);
因为我不能更改任何节点而不可变地借用它们,但它们都已经被不可变地借用了。
error[E0502]: cannot borrow `store` as immutable because it is also borrowed as mutable
--> src/main.rs:12:28
|
12 | store.push(Node::Link(&store[0]));
| ----- ^^^^^ - mutable borrow ends here
| | |
| | immutable borrow occurs here
| mutable borrow occurs here
error[E0502]: cannot borrow `store` as mutable because it is also borrowed as immutable
--> src/main.rs:13:5
|
12 | store.push(Node::Link(&store[0]));
| ----- immutable borrow occurs here
13 | store.push(Node::Link(&store[1]));
| ^^^^^ mutable borrow occurs here
14 | *store.index_mut(1) = Node::Link(&store[2]);
15 | }
| - immutable borrow ends here
error[E0502]: cannot borrow `store` as immutable because it is also borrowed as mutable
--> src/main.rs:13:28
|
13 | store.push(Node::Link(&store[1]));
| ----- ^^^^^ - mutable borrow ends here
| | |
| | immutable borrow occurs here
| mutable borrow occurs here
error[E0502]: cannot borrow `store` as mutable because it is also borrowed as immutable
--> src/main.rs:14:6
|
12 | store.push(Node::Link(&store[0]));
| ----- immutable borrow occurs here
13 | store.push(Node::Link(&store[1]));
14 | *store.index_mut(1) = Node::Link(&store[2]);
| ^^^^^ mutable borrow occurs here
15 | }
| - immutable borrow ends here
有没有办法在没有运行时开销的情况下使用具有循环链接的结构,例如像我尝试过的纯引用?我可以接受额外的内存成本,例如支持 Vec 持有所有节点的所有权。
【问题讨论】:
-
你打算如何使用这个数据结构?节点将持有什么样的数据?你打算如何执行 Rust 的 Aliasing XOR Mutability 原则?如果您可以勾勒出您希望获得的内容、您希望如何避免内存泄漏以及您希望如何解决 Aliasing XOR Mutability,那么我们可以帮助您在 Rust 中实现这一点。否则?您会收到大量不同的建议(有许多不同的方法,每种方法都有其权衡),它们可能有用,也可能没用。
-
但这听起来会破坏 Rust 的 0-runtime-cost 生命周期管理的目的 => Rust 是 0-cost 的,否则成本最低;
Rc和Weak,RefCell是完全有效的工具......尽管它们混淆了代码并将数据使用的验证推到运行时而不是编译时(从而增加了错误的可能性) . -
我的目标是描述一个 BNF 语法。这些问题只发生在节点“结在一起”期间。我的计划是仅使用不可变引用以某种方式将这些节点结合在一起,引用节点枚举由具有相同生命周期的(私有)vec 拥有一次:这两个可能捆绑在一个结构中,所以当结构被删除时,两者向量和循环结构同时被删除,没有问题。
-
好吧,听起来很简单;请将此编辑为您的问题:)
标签: tree rust cyclic-reference cyclic-graph