add solutions for ownership.md

This commit is contained in:
sunface 2022-03-02 17:32:22 +08:00
parent 2759880370
commit 2d7e251e8b
3 changed files with 171 additions and 20 deletions

View File

@ -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")
}
```

View File

@ -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")
}
```

View File

@ -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")
}
```