对 Vec<_>的可变引用在 while 循环中活得不够长
这是到目前为止的代码,相关行是 27 和 28: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=37bba701ad2e9d47741da1149881ddd1
错误消息:
error[E0597]: `neighs` does not live long enough
--> src/lib.rs:28:25
|
17 | while let Some(Reverse(current)) = open.pop() {
| ---------- borrow later used here
...
28 | for neighbor in neighs.iter_mut() {
| ^^^^^^^^^^^^^^^^^ borrowed value does not live long enough
...
38 | }
| - `neighs` dropped here while still borrowed
我过去使用过 Rust 几次,我我完全意识到这个问题应该是多么基本。我还花了几个小时尝试不同的事情并搜索类似的问题。我似乎无法找到解决方案。如何使 neighs
与函数 a_star
一样长?
Here's the code so far, the relevant lines are 27 and 28: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=37bba701ad2e9d47741da1149881ddd1
Error message:
error[E0597]: `neighs` does not live long enough
--> src/lib.rs:28:25
|
17 | while let Some(Reverse(current)) = open.pop() {
| ---------- borrow later used here
...
28 | for neighbor in neighs.iter_mut() {
| ^^^^^^^^^^^^^^^^^ borrowed value does not live long enough
...
38 | }
| - `neighs` dropped here while still borrowed
I used Rust a few times in the past, and I'm fully aware of how basic this problem should be. I've also spent hours trying different things and googling similar problems. I just can't seem to find a solution. How do I make neighs
live as long as function a_star
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

我建议:
这种方式 open 将不会引用任何 neighs 元素或 current 元素。这将避免终身问题。
I would suggest:
This way open will not reference any of neighs elements or current. which will avoid lifetime issues.