diff --git a/src/main.rs b/src/main.rs index 9f4922c..d4f0b22 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,4 @@ -mod ownership_1; +mod ownership; mod sub_mod; mod tests; mod user; @@ -51,7 +51,8 @@ fn main() { // 在特定作用域中的特定数据有且只有一个可变引用 // let s2 = &mut s; - ownership_1::test_ownership1(); + ownership::test_ownership1(); + ownership::test_ownership2(); change(s1); println!("{}", s1); diff --git a/src/ownership.rs b/src/ownership.rs new file mode 100644 index 0000000..8d5e508 --- /dev/null +++ b/src/ownership.rs @@ -0,0 +1,46 @@ +pub fn test_ownership1() { + let s = String::from("hello"); // s 进入作用域 + + takes_ownership(s); // s 的值移动到函数里 ... + + // ... 所以到这里不再有效 + + let x = 5; // x 进入作用域 + + makes_copy(x); // x 应该移动函数里, + // 但 i32 是 Copy 的,所以在后面可继续使用 x +} // 这里, x 先移出了作用域,然后是 s。但因为 s 的值已被移走,所以不会有特殊操作 + +fn takes_ownership(some_string: String) { + // some_string 进入作用域 + println!("{}", some_string); +} // 这里,some_string 移出作用域并调用 `drop` 方法。占用的内存被释放 + +fn makes_copy(some_integer: i32) { + // some_integer 进入作用域 + println!("{}", some_integer); +} // 这里,some_integer 移出作用域。不会有特殊操作 + +pub fn test_ownership2() { + let s1 = gives_ownership(); // gives_ownership 将返回值 移给 s1 + println!("{}", s1); + + let s2 = String::from("hello"); // s2 进入作用域 + // println!("{}", s2); + + let s3 = takes_and_gives_back(s2); // s2 被移动到 takes_and_gives_back 中, 它也将返回值移给 s3 + println!("{}", s3); +} // 这里, s3 移出作用域并被丢弃。s2 也移出作用域,但已被移走,所以什么也不会发生。s1 移出作用域并被丢弃 + +fn gives_ownership() -> String { + // gives_ownership 将返回值移动给调用它的函数 + let some_string = String::from("hello"); // some_string 进入作用域. + some_string // 返回 some_string 并移出给调用的函数 +} + +// takes_and_gives_back 将传入字符串并返回该值 +fn takes_and_gives_back(a_string: String) -> String { + // a_string 进入作用域 + a_string + // 返回 a_string 并移出给调用的函数 +} diff --git a/src/ownership_1.rs b/src/ownership_1.rs deleted file mode 100644 index 4b8acf3..0000000 --- a/src/ownership_1.rs +++ /dev/null @@ -1,22 +0,0 @@ -pub fn test_ownership1() { - let s = String::from("hello"); // s 进入作用域 - - takes_ownership(s); // s 的值移动到函数里 ... - - // ... 所以到这里不再有效 - - let x = 5; // x 进入作用域 - - makes_copy(x); // x 应该移动函数里, - // 但 i32 是 Copy 的,所以在后面可继续使用 x -} // 这里, x 先移出了作用域,然后是 s。但因为 s 的值已被移走,所以不会有特殊操作 - -fn takes_ownership(some_string: String) { - // some_string 进入作用域 - println!("{}", some_string); -} // 这里,some_string 移出作用域并调用 `drop` 方法。占用的内存被释放 - -fn makes_copy(some_integer: i32) { - // some_integer 进入作用域 - println!("{}", some_integer); -} // 这里,some_integer 移出作用域。不会有特殊操作 diff --git a/src/ownership_2.rs b/src/ownership_2.rs deleted file mode 100644 index a703df2..0000000 --- a/src/ownership_2.rs +++ /dev/null @@ -1,21 +0,0 @@ -fn main() { - let s1 = gives_ownership(); // gives_ownership 将返回值 移给 s1 - println!("{}", s1); - - let s2 = String::from("hello"); // s2 进入作用域 - // println!("{}", s2); - - let s3 = takes_and_gives_back(s2); // s2 被移动到 takes_and_gives_back 中, 它也将返回值移给 s3 - println!("{}", s3); -} // 这里, s3 移出作用域并被丢弃。s2 也移出作用域,但已被移走,所以什么也不会发生。s1 移出作用域并被丢弃 - -fn gives_ownership() -> String { // gives_ownership 将返回值移动给调用它的函数 - let some_string = String::from("hello"); // some_string 进入作用域. - some_string // 返回 some_string 并移出给调用的函数 -} - -// takes_and_gives_back 将传入字符串并返回该值 -fn takes_and_gives_back(a_string: String) -> String { // a_string 进入作用域 - a_string - // 返回 a_string 并移出给调用的函数 -} \ No newline at end of file