add solutions for struct.md
This commit is contained in:
parent
616153c17c
commit
02375b3b16
|
@ -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);
|
||||
}
|
||||
```
|
|
@ -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);
|
||||
}
|
||||
```
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
```
|
||||
|
|
Loadingโฆ
Reference in New Issue