add solutions for struct.md

This commit is contained in:
sunface 2022-03-02 21:25:55 +08:00
parent 616153c17c
commit 02375b3b16
3 changed files with 167 additions and 10 deletions

View File

@ -0,0 +1,155 @@
1.
```rust
struct Person {
name: String,
age: u8,
hobby: String
}
fn main() {
let age = 30;
let p = Person {
name: String::from("sunface"),
age,
hobby: "coding".to_string()
};
}
```
2.
```rust
struct Unit;
trait SomeTrait {
// ...Some behavours defines here
}
// We don't care the the fields are in Unit, but we care its behaviors.
// So we use a struct with no fields and implement some behaviors for it
impl SomeTrait for Unit { }
fn main() {
let u = Unit;
do_something_with_unit(u);
}
// fill the blank to make the code work
fn do_something_with_unit(u: Unit) { }
```
3.
```rust
struct Color(i32, i32, i32);
struct Point(i32, i32, i32);
fn main() {
let v = Point(0, 127, 255);
check_color(v);
}
fn check_color(p: Point) {
let Point(x, _, _) = p;
assert_eq!(x, 0);
assert_eq!(p.1, 127);
assert_eq!(p.2, 255);
}
```
4.
```rust
struct Person {
name: String,
age: u8,
}
fn main() {
let age = 18;
let mut p = Person {
name: String::from("sunface"),
age,
};
// how can you believe sunface is only 18?
p.age = 30;
p.name = String::from("sunfei");
}
```
5.
```rust
struct Person {
name: String,
age: u8,
}
fn main() {}
fn build_person(name: String, age: u8) -> Person {
Person {
age,
name
}
}
```
6.
```rust
struct User {
active: bool,
username: String,
email: String,
sign_in_count: u64,
}
fn main() {
let u1 = User {
email: String::from("someone@example.com"),
username: String::from("sunface"),
active: true,
sign_in_count: 1,
};
let u2 = set_email(u1);
}
fn set_email(u: User) -> User {
User {
email: String::from("contact@im.dev"),
..u
}
}
```
7.
```rust
#[derive(Debug)]
struct Rectangle {
width: u32,
height: u32,
}
fn main() {
let scale = 2;
let rect1 = Rectangle {
width: dbg!(30 * scale), // print debug info to stderr and assign the value of `30 * scale` to `width`
height: 50,
};
dbg!(&rect1); // print debug info to stderr
println!("{:?}", rect1); // print debug info to stdout
}
```
8.
```rust
#[derive(Debug)]
struct File {
name: String,
data: String,
}
fn main() {
let f = File {
name: String::from("readme.md"),
data: "Rust By Practice".to_string()
};
let _name = f.name;
println!("{}", f.data);
}
```

View File

@ -1,7 +1,7 @@
# Struct # Struct
### There types of structs ### There types of structs
🌟 We must specify concrete values for each of the fields in struct. 1. 🌟 We must specify concrete values for each of the fields in struct.
```rust,editable ```rust,editable
// fix the error // fix the error
@ -20,7 +20,7 @@ fn main() {
``` ```
🌟 Unit struct don't have any fields. It can be useful when you need to implement a trait on some type but dont have any data that you want to store in the type itself. 2. 🌟 Unit struct don't have any fields. It can be useful when you need to implement a trait on some type but dont have any data that you want to store in the type itself.
```rust,editable ```rust,editable
struct Unit; struct Unit;
@ -40,7 +40,7 @@ fn main() {
fn do_something_with_unit(u: __) { } fn do_something_with_unit(u: __) { }
``` ```
🌟🌟🌟 Tuple struct looks similar to tuples, it has added meaning the struct name provides but has no named fields. It's useful when you want give the whole tuple a name, but don't care the fields's names. 3. 🌟🌟🌟 Tuple struct looks similar to tuples, it has added meaning the struct name provides but has no named fields. It's useful when you want give the whole tuple a name, but don't care the fields's names.
```rust,editable ```rust,editable
@ -62,7 +62,7 @@ fn check_color(p: Color) {
### Operate on structs ### Operate on structs
🌟 You can make a whole struct mutable when instantiate it, but Rust doesn't allow us to mark only certain fields as mutable. 4. 🌟 You can make a whole struct mutable when instantiate it, but Rust doesn't allow us to mark only certain fields as mutable.
```rust,editable ```rust,editable
@ -79,14 +79,14 @@ fn main() {
}; };
// how can you believe sunface is only 18? // how can you believe sunface is only 18?
p.age = 30 p.age = 30;
// fill the lank // fill the lank
__ = String::from("sunfei"); __ = String::from("sunfei");
} }
``` ```
🌟 Using *field init shorthand syntax* to reduct repetitions. 5. 🌟 Using *field init shorthand syntax* to reduct repetitions.
```rust,editable ```rust,editable
// fill the blank // fill the blank
@ -104,7 +104,7 @@ fn build_person(name: String, age: u8) -> Person {
} }
``` ```
🌟 You can create instance from other instance with *struct update syntax* 6. 🌟 You can create instance from other instance with *struct update syntax*
```rust,editable ```rust,editable
// fill the blank to make the code work // fill the blank to make the code work
@ -134,7 +134,7 @@ fn set_email(u: User) -> User {
``` ```
### Print the structs ### Print the structs
🌟🌟 We can use `#[derive(Debug)]` to [make a struct prinable](https://doc.rust-lang.org/book/ch05-02-example-structs.html?highlight=%23%5Bderive(Debug)%5D#adding-useful-functionality-with-derived-traits). 7. 🌟🌟 We can use `#[derive(Debug)]` to [make a struct prinable](https://doc.rust-lang.org/book/ch05-02-example-structs.html?highlight=%23%5Bderive(Debug)%5D#adding-useful-functionality-with-derived-traits).
```rust,editable ```rust,editable
@ -194,7 +194,7 @@ fn main() {
#### Exercises #### Exercises
🌟🌟 8. 🌟🌟
```rust,editable ```rust,editable
// fix errors to make it work // fix errors to make it work
@ -211,6 +211,7 @@ fn main() {
let _name = f.name; let _name = f.name;
// ONLY modify this line
println!("{}, {}, {:?}",f.name, f.data, f); println!("{}, {}, {:?}",f.name, f.data, f);
} }
``` ```

View File

@ -78,7 +78,7 @@ fn main() {
}; };
// how can you believe sunface is only 18? // how can you believe sunface is only 18?
p.age = 30 p.age = 30;
// 填空 // 填空
__ = String::from("sunfei"); __ = String::from("sunfei");
@ -212,6 +212,7 @@ fn main() {
let _name = f.name; let _name = f.name;
// 只能修改这一行
println!("{}, {}, {:?}",f.name, f.data, f); println!("{}, {}, {:?}",f.name, f.data, f);
} }
``` ```