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
### 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
// 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 donโ€™t 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 donโ€™t have any data that you want to store in the type itself.
```rust,editable
struct Unit;
@ -40,7 +40,7 @@ fn main() {
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
@ -62,7 +62,7 @@ fn check_color(p: Color) {
### 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
@ -79,14 +79,14 @@ fn main() {
};
// how can you believe sunface is only 18?
p.age = 30
p.age = 30;
// fill the lank
__ = String::from("sunfei");
}
```
๐ŸŒŸ Using *field init shorthand syntax* to reduct repetitions.
5. ๐ŸŒŸ Using *field init shorthand syntax* to reduct repetitions.
```rust,editable
// 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
// fill the blank to make the code work
@ -134,7 +134,7 @@ fn set_email(u: User) -> User {
```
### 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
@ -194,7 +194,7 @@ fn main() {
#### Exercises
๐ŸŒŸ๐ŸŒŸ
8. ๐ŸŒŸ๐ŸŒŸ
```rust,editable
// fix errors to make it work
@ -211,6 +211,7 @@ fn main() {
let _name = f.name;
// ONLY modify this line
println!("{}, {}, {:?}",f.name, f.data, f);
}
```

View File

@ -78,7 +78,7 @@ fn main() {
};
// how can you believe sunface is only 18?
p.age = 30
p.age = 30;
// ๅกซ็ฉบ
__ = String::from("sunfei");
@ -212,6 +212,7 @@ fn main() {
let _name = f.name;
// ๅช่ƒฝไฟฎๆ”น่ฟ™ไธ€่กŒ
println!("{}, {}, {:?}",f.name, f.data, f);
}
```