【发布时间】:2021-07-27 23:40:16
【问题描述】:
我很好奇这里的这个例子https://doc.rust-lang.org/book/ch08-01-vectors.html
$ cargo run
Compiling collections v0.1.0 (file:///projects/collections)
error[E0502]: cannot borrow `v` as mutable because it is also borrowed as immutable
--> src/main.rs:6:5
|
4 | let first = &v[0];
| - immutable borrow occurs here
5 |
6 | v.push(6);
| ^^^^^^^^^ mutable borrow occurs here
7 |
8 | println!("The first element is: {}", first);
| ----- immutable borrow later used here
error: aborting due to previous error
For more information about this error, try `rustc --explain E0502`.
error: could not compile `collections`
To learn more, run the command again with --verbose.
在高层次上,对v 中的项目的引用与对v 本身的引用有何相同之处。从语法上讲,这是如何执行的? 如何简单地实现带有示例类型的东西来举例说明这一点?到目前为止,我所能找到的只是所有权阻止了这一点,但我找不到任何解释它的东西。
感谢任何帮助!
【问题讨论】:
标签: rust