fn main() { // 栈分配的整型 let a = 88; // 将 `a` *复制*到 `b`——不存在资源移动 let b = a; // 两个值各自都可以使用 println!("a {}, and b {}", a, b);
let v1 = vec!["Go语言极简一本通","Go语言微服务架构核心22讲","从0到Go语言微服务架构师"]; let v2 =v1; println!("{:?}",v1); }
报错如下: error[E0382]: borrow of moved value: `v1` let v1 = vec!["Go语言极简一本通","Go语言微服务架构核心22讲","从0到Go语言微服务架构师"]; | -- move occurs because `v1` has type `Vec<&str>`, which does not implement the `Copy` trait 9 | let v2 =v1; | -- value moved here 10 | println!("{:?}",v1); | ^^ value borrowed here after move |
error[E0382]: borrow of moved value: `studyList2` | let studyList2 = studyList; // studyList 将所有权转义给了 studyList2 | ---------- move occurs because `studyList2` has type `Vec<&str>`, which does not implement the `Copy` trait | show(studyList2); // studyList2 将所有权转让给参数 v,studyList2 不再可用。 | ---------- value moved here | println!("studyList2 {:?}",studyList2);//studyList2 已经不可用。 | ^^^^^^^^^^ value borrowed here after move
fn main() { let studyList3 = vec!["Go语言极简一本通","Go语言微服务架构核心22讲","从0到Go语言微服务架构师"]; let studyList4 = studyList3; let result = show2(studyList4); println!("result {:?}",result); //输出 result ["Go语言极简一本通", "Go语言微服务架构核心22讲", "从0到Go语言微服务架构师"] }