You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

31 lines
1.2 KiB

fn main() {
1 month ago
let s1 = gives_ownership(); // gives_ownership moves its return
// value into s1
1 month ago
let s2 = String::from("hello"); // s2 comes into scope
1 month ago
let s3 = takes_and_gives_back(s2); // s2 is moved into
// takes_and_gives_back, which also
// moves its return value into s3
} // Here, s3 goes out of scope and is dropped. s2 was moved, so nothing
// happens. s1 goes out of scope and is dropped.
1 month ago
fn gives_ownership() -> String { // gives_ownership will move its
// return value into the function
// that calls it
1 month ago
let some_string = String::from("yours"); // some_string comes into scope
1 month ago
some_string // some_string is returned and
// moves out to the calling
// function
}
1 month ago
// 该函数将传入字符串并返回该值
fn takes_and_gives_back(a_string: String) -> String {
// a_string comes into
// scope
a_string // 返回 a_string 并移出给调用的函数
}