小编Ser*_*mov的帖子

在从引用中分配变量时,ref和&之间的区别是什么?

这段代码有什么问题?

fn example() {
    let vec = vec![1, 2, 3];
    let &_y = &vec;
}
Run Code Online (Sandbox Code Playgroud)
error[E0507]: cannot move out of borrowed content
 --> src/lib.rs:3:15
  |
3 |     let &_y = &vec;
  |         ---   ^^^^ cannot move out of borrowed content
  |         ||
  |         |data moved here
  |         help: consider removing the `&`: `_y`
  |
note: move occurs because `_y` has type `std::vec::Vec<i32>`, which does not implement the `Copy` trait
 --> src/lib.rs:3:10
  |
3 |     let &_y = &vec;
  |          ^^ …
Run Code Online (Sandbox Code Playgroud)

syntax reference pattern-matching rust

5
推荐指数
2
解决办法
625
查看次数

标签 统计

pattern-matching ×1

reference ×1

rust ×1

syntax ×1