4

绑定到 var 后分配和直接分配 a 有什么区别&Vec

let mut v2 = &vec![1,2,3];

let tv = &vec![2,3,4];
v2 = tv;

// different from
// v2 = &vec![2, 3, 4];  // uncomment this will error

println!("{:?}", v2);

借用检查器:

error[E0716]: temporary value dropped while borrowed
  --> examples\demo.rs:27:11
   |
27 |     v2 = &vec![2, 3, 4];
   |           ^^^^^^^^^^^^^- temporary value is freed at the end of this statement
   |           |
   |           creates a temporary which is freed while still in use
...
30 |     println!("{:?}", v2);
   |                      -- borrow later used here
4

0 回答 0