RUST 所有权 技术教程文章

rust所有权和函数【代码】

所有权与函数fn main() {let s = String::from("hello");takes_ownership(s); //s的值移动到函数里let x = 5; //x进入作用域makes_copy(x); //x应该移动到函数里,但是i32是copy,所以后面可以继续使用x。println!("x is {}",x);//下面这句编译会出错, ^ value borrowed here after move//println!("s is {}",s);}fn takes_ownership(some_string:String){println!("{}",some_string); } //some_string离开作用域并调用drop方法。占...