使用方括号时会出现隐藏的取消引用。这个
Index
trait documentation
说:
container[index]
实际上是的句法糖
*container.index(index)
如果添加
*
到第一行,则会得到相同的错误消息:
error[E0507]: cannot move out of a shared reference
--> src/lib.rs:4:13
|
4 | let t = *v.index(0);
| ^^^^^^^^^^^ move occurs because value has type `Vec<i32>`, which does not implement the `Copy` trait
|
help: consider removing the dereference here
|
4 - let t = *v.index(0);
4 + let t = v.index(0);
|
Playground