diff --git a/solutions/ownership/ownership.md b/solutions/ownership/ownership.md index e69de29..7a34d47 100644 --- a/solutions/ownership/ownership.md +++ b/solutions/ownership/ownership.md @@ -0,0 +1,151 @@ +1. +```rust +fn main() { + let x = String::from("hello, world"); + let y = x.clone(); + println!("{},{}",x,y); +} +``` + +```rust +fn main() { + let x = "hello, world"; + let y = x; + println!("{},{}",x,y); +} +``` + +```rust +fn main() { + let x = &String::from("hello, world"); + let y = x; + println!("{},{}",x,y); +} +``` + +```rust +fn main() { + let x = 10; + let y = x; + println!("{},{}",x,y); +} +``` + +2. +```rust +// Don't modify code in main! +fn main() { + let s1 = String::from("hello, world"); + let s2 = take_ownership(s1); + + println!("{}", s2); +} + +// Only modify the code below! +fn take_ownership(s: String) -> String { + println!("{}", s); + s +} +``` + +3. +```rust +fn main() { + let s = give_ownership(); + println!("{}", s); +} + +// Only modify the code below! +fn give_ownership() -> String { + let s = String::from("hello, world"); + // convert String to Vec + let _s = s.as_bytes(); + s +} +``` + +```rust +fn main() { + let s = give_ownership(); + println!("{}", s); +} + +// Only modify the code below! +fn give_ownership() -> String { + let s = String::from("hello, world"); + s +} +``` + +4. +```rust +fn main() { + let s = String::from("hello, world"); + + print_str(s.clone()); + + println!("{}", s); +} + +fn print_str(s: String) { + println!("{}",s) +} +``` + +5. +```rust +fn main() { + let x = (1, 2, (), "hello"); + let y = x; + println!("{:?}, {:?}", x, y); +} +``` + +6. +```rust +fn main() { + let s = String::from("hello, "); + + // modify this line only ! + let mut s1 = s; + + s1.push_str("world") +} +``` + +7. +```rust +fn main() { + let x = Box::new(5); + + let mut y = Box::new(3); // implement this line, dont change other lines! + + *y = 4; + + assert_eq!(*x, 5); +} +``` + +8. +```rust +fn main() { + let t = (String::from("hello"), String::from("world")); + + let _s = t.0; + + // modify this line only, don't use `_s` + println!("{:?}", t.1); + } +``` + +9. +```rust +fn main() { + let t = (String::from("hello"), String::from("world")); + + // fill the blanks + let (s1, s2) = t.clone(); + + println!("{:?}, {:?}, {:?}", s1, s2, t); // -> "hello", "world", ("hello", "world") +} +``` \ No newline at end of file diff --git a/src/ownership/ownership.md b/src/ownership/ownership.md index 0586cb1..af3d881 100644 --- a/src/ownership/ownership.md +++ b/src/ownership/ownership.md @@ -1,17 +1,17 @@ # Ownership -๐ŸŒŸ๐ŸŒŸ +1. ๐ŸŒŸ๐ŸŒŸ ```rust,editable fn main() { - // modify this line only! use as many approaches as you can + // use as many approaches as you can to make it work let x = String::from("hello, world"); let y = x; println!("{},{}",x,y); } ``` -๐ŸŒŸ๐ŸŒŸ +2. ๐ŸŒŸ๐ŸŒŸ ```rust,editable // Don't modify code in main! fn main() { @@ -28,7 +28,7 @@ fn take_ownership(s: String) { ``` -๐ŸŒŸ๐ŸŒŸ +3. ๐ŸŒŸ๐ŸŒŸ ```rust,editable fn main() { @@ -45,9 +45,9 @@ fn give_ownership() -> String { } ``` -๐ŸŒŸ๐ŸŒŸ +4. ๐ŸŒŸ๐ŸŒŸ ```rust,editable -// use clone to fix it +// fix the error without removing code line fn main() { let s = String::from("hello, world"); @@ -61,11 +61,11 @@ fn print_str(s: String) { } ``` -๐ŸŒŸ๐ŸŒŸ +5. ๐ŸŒŸ๐ŸŒŸ ```rust, editable // don't use clone ,use copy instead fn main() { - let x = (1, 2, (), "hello"); + let x = (1, 2, (), "hello".to_string()); let y = x.clone(); println!("{:?}, {:?}", x, y); } @@ -74,7 +74,7 @@ fn main() { #### Mutability Mutability can be changed when ownership is transferred. -๐ŸŒŸ +6. ๐ŸŒŸ ```rust,editable fn main() { @@ -87,7 +87,7 @@ fn main() { } ``` -๐ŸŒŸ๐ŸŒŸ๐ŸŒŸ +7. ๐ŸŒŸ๐ŸŒŸ๐ŸŒŸ ```rust,editable fn main() { @@ -136,7 +136,7 @@ fn main() { #### Exercises -๐ŸŒŸ +8. ๐ŸŒŸ ```rust,editable fn main() { @@ -149,16 +149,16 @@ fn main() { } ``` -๐ŸŒŸ๐ŸŒŸ +9. ๐ŸŒŸ๐ŸŒŸ ```rust,editable fn main() { let t = (String::from("hello"), String::from("world")); - // fill the blanks - let (__, __) = t; + // fill the blanks + let (__, __) = __; - println!("{:?}, {:?}, {:?}", s1, s2, t); + println!("{:?}, {:?}, {:?}", s1, s2, t); // -> "hello", "world", ("hello", "world") } ``` diff --git a/zh-CN/src/ownership/ownership.md b/zh-CN/src/ownership/ownership.md index 22330af..df71e21 100644 --- a/zh-CN/src/ownership/ownership.md +++ b/zh-CN/src/ownership/ownership.md @@ -4,7 +4,7 @@ ```rust,editable fn main() { - // ๅช่ƒฝไฟฎๆ”นไธ‹้ข่ฟ™่กŒไปฃ็ ๏ผ ไฝฟ็”จๅฐฝๅฏ่ƒฝๅคš็š„ๆ–นๆณ•ๆฅ้€š่ฟ‡็ผ–่ฏ‘ + // ไฝฟ็”จๅฐฝๅฏ่ƒฝๅคš็š„ๆ–นๆณ•ๆฅ้€š่ฟ‡็ผ–่ฏ‘ let x = String::from("hello, world"); let y = x; println!("{},{}",x,y); @@ -48,7 +48,7 @@ fn give_ownership() -> String { ๐ŸŒŸ๐ŸŒŸ ```rust,editable -// ไฝฟ็”จ clone ๆฅ่ฎฉไปฃ็ ้€š่ฟ‡็ผ–่ฏ‘ +// ไฟฎๅค้”™่ฏฏ๏ผŒไธ่ฆๅˆ ้™คไปปไฝ•ไปฃ็ ่กŒ fn main() { let s = String::from("hello, world"); @@ -66,7 +66,7 @@ fn print_str(s: String) { ```rust, editable // ไธ่ฆไฝฟ็”จ clone๏ผŒไฝฟ็”จ copy ็š„ๆ–นๅผๆ›ฟไปฃ fn main() { - let x = (1, 2, (), "hello"); + let x = (1, 2, (), "hello".to_string()); let y = x.clone(); println!("{:?}, {:?}", x, y); } @@ -160,9 +160,9 @@ fn main() { let t = (String::from("hello"), String::from("world")); // ๅกซ็ฉบ๏ผŒไธ่ฆไฟฎๆ”นๅ…ถๅฎƒไปฃ็  - let (__, __) = t; + let (__, __) = __; - println!("{:?}, {:?}, {:?}", s1, s2, t); + println!("{:?}, {:?}, {:?}", s1, s2, t); // -> "hello", "world", ("hello", "world") } ```