Skip to content

Commit 838d8c2

Browse files
authored
Merge pull request #35 from edilmedeiros/edilmedeiros-chapt13
Edit Chapter 13
2 parents 659e63c + 2ce2469 commit 838d8c2

File tree

1 file changed

+42
-14
lines changed

1 file changed

+42
-14
lines changed

13_structs.md

+42-14
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
# Structs
22

3-
A struct is a fairly straightforward type. It's similar to classes in OOP languages, but there's no concept of inheritance or polymorphism here. It simply allows you to group together related pieces of data - which can all be of different types - into a single name. At a high level, we first define the struct by providing it's name, the names of the different fields and their associated types. We then create an instance of the struct by specifying the the values for each field. We can also add methods to a struct by defining functions within an `impl` block.
3+
A struct is a fairly straightforward type.
4+
It's similar to classes in OOP languages, but there's no concept of inheritance or polymorphism here.
5+
It simply allows you to group together related pieces of data - which can all be of different types - into a single name.
6+
At a high level, we first define the struct by providing it's name, the names of the different fields and their associated types.
7+
We then create an instance of the struct by specifying the values for each field.
8+
We can also add methods to a struct by defining functions within an `impl` block.
49

5-
Let's create an input struct to group together our related input fields. We typically place this at the top of our file or we include it from another file:
10+
Let's create an input struct to group together our related input fields.
11+
We typically place this at the top of our file or we include it from another file:
612

713
```rust
814
use std::io::Read;
@@ -35,7 +41,9 @@ And all we have to do is create an instance of it in our `main` function:
3541
...
3642
```
3743

38-
Pretty simple right? One neat modification we can make here is remove the key/value pair and just put a comma separated list of field names. We can do this anytime the field names match the variable names:
44+
Pretty simple right?
45+
One neat modification we can make here is remove the key/value pair and just put a comma separated list of field names.
46+
We can do this anytime the field names match the variable names:
3947

4048
```rust
4149
...
@@ -55,11 +63,12 @@ Pretty simple right? One neat modification we can make here is remove the key/va
5563
...
5664
```
5765

58-
Let's change this up so that we have an `inputs` vector and we'll `push` each decoded input into that. Remember, we have to declare it as *mutable*! We can declare a new vec in two ways, using a macro `vec![]` or calling the new method, `Vec::new()`.
66+
Let's change this up so that we have an `inputs` vector and we'll `push` each decoded input into that.
67+
Remember, we have to declare it as *mutable*! We can declare a new vec in two ways, using a macro `vec![]` or calling the new method, `Vec::new()`.
5968

6069
```rust
6170
...
62-
let inputs = vec![];
71+
let mut inputs = vec![];
6372

6473
for _ in 0..input_length {
6574
let txid = read_txid(&mut bytes_slice);
@@ -86,7 +95,8 @@ Now that we have a collection of `inputs`, let's try printing out the debug outp
8695
...
8796
```
8897

89-
Try running `cargo run` and see what happens. We'll get a compiler error:
98+
Try running `cargo run` and see what happens.
99+
We'll get a compiler error:
90100
```console
91101
error[E0277]: `Input` doesn't implement `Debug`
92102
--> src/main.rs:89:30
@@ -105,9 +115,12 @@ help: consider annotating `Input` with `#[derive(Debug)]`
105115
|
106116
```
107117

108-
We're trying to print out a collection of `Input` structs, but that struct does not implement the `Debug` trait, so we can't get a printout. In fact, it doesn't implement the `Debug` or the `Display` traits so how do we print it out? One way would be to manually implement the `Debug` trait ourselves.
118+
We're trying to print out a collection of `Input` structs, but that struct does not implement the `Debug` trait, so we can't get a printout.
119+
In fact, it doesn't implement the `Debug` or the `Display` traits so how do we print it out? One way would be to manually implement the `Debug` trait ourselves.
109120

110-
Let's try that first. We'll follow the example from the docs here for manual implementation: https://doc.rust-lang.org/std/fmt/trait.Debug.html#examples. Don't forget to add `use std::fmt` at the top of the file to bring the `fmt` module into scope.
121+
Let's try that first.
122+
We'll follow the example from the docs here for manual implementation: https://doc.rust-lang.org/std/fmt/trait.Debug.html#examples.
123+
Don't forget to add `use std::fmt` at the top of the file to bring the `fmt` module into scope.
111124

112125
```rust
113126
impl fmt::Debug for Input {
@@ -122,22 +135,35 @@ impl fmt::Debug for Input {
122135
}
123136
```
124137

125-
Some of this syntax might feel a bit unfamiliar, so let's take a moment to go through it. The key thing to understand is that we have to provide the `fmt` function implementation. This is the function that will get called when we try to print the debugging printout. It will be called with an argument type that is a `Formatter` (we'll ignore the `<'_>` syntax for now).
138+
Some of this syntax might feel a bit unfamiliar, so let's take a moment to go through it.
139+
The key thing to understand is that we have to provide the `fmt` function implementation.
140+
This is the function that will get called when we try to print the debugging printout.
141+
It will be called with an argument type that is a `Formatter` (we'll ignore the `<'_>` syntax for now).
126142

127-
The `Formatter` has a method called [`debug_struct`](https://doc.rust-lang.org/std/fmt/struct.Formatter.html#method.debug_struct) that we can use and pass in the various fields that we want to display for debugging purposes. The field method takes in a field name to display along with the value. The value itself must be something that implements the `Debug` trait. Luckily, all of our fields do. At the end, we call `finish` which returns the required `fmt::Result`.
143+
The `Formatter` has a method called [`debug_struct`](https://doc.rust-lang.org/std/fmt/struct.Formatter.html#method.debug_struct) that we can use and pass in the various fields that we want to display for debugging purposes.
144+
The field method takes in a field name to display along with the value.
145+
The value itself must be something that implements the `Debug` trait.
146+
Luckily, all of our fields do.
147+
At the end, we call `finish` which returns the required `fmt::Result`.
128148

129-
If you try running `cargo run` now, this should work. It won't be very pretty or easy to read though.
149+
If you try running `cargo run` now, this should work.
150+
It won't be very pretty or easy to read though.
130151

131152
```shell
132153
Version: 1
133154
Inputs: [Input { txid: [248, 198, 147, 119, 27, 41, 146, 161, 27, 83, 192, 69, 54, 154, 179, 26, 146, 13, 225, 217, 33, 255, 60, 20, 138, 157, 4, 135, 200, 249, 11, 175], output_index: 16, script: [72, 48, 69, 2, 33, 0, 144, 74, 46, 14, 143, 89, 127, 193, 204, 39, 27, 98, 148, 176, 151, 166, 237, 201, 82, 227, 12, 69, 62, 53, 48, 249, 36, 146, 116, 151, 105, 168, 2, 32, 24, 70, 76, 34, 91, 3, 194, 135, 145, 175, 6, 188, 127, 237, 18, 157, 202, 174, 255, 158, 200, 19, 90, 218, 31, 177, 23, 98, 206, 8, 30, 169, 1, 65, 4, 218, 40, 145, 146, 176, 132, 93, 91, 137, 206, 130, 102, 93, 136, 172, 137, 215, 87, 207, 197, 253, 153, 123, 29, 232, 174, 71, 247, 120, 12, 230, 163, 34, 7, 88, 59, 116, 88, 209, 210, 243, 253, 107, 58, 59, 132, 42, 234, 158, 183, 137, 226, 190, 165, 123, 3, 212, 14, 104, 77, 142, 30, 5, 105], sequence: 4294967295 }, Input { txid: [229, 29, 33, 119, 51, 43, 175, 249, 207, 187, 192, 132, 39, 207, 13, 133, 210, 138, 253, 200, 20, 17, 205, 187, 132, 244, 12, 149, 133, 139, 8, 13], output_index: 1, script: [72, 48, 69, 2, 32, 54, 157, 247, 212, 39, 149, 35, 158, 171, 249, 212, 26, 238, 117, 227, 255, 32, 82, 23, 84, 82, 43, 208, 103, 137, 15, 142, 237, 246, 4, 76, 109, 2, 33, 0, 154, 207, 189, 136, 213, 29, 132, 45, 184, 122, 185, 144, 164, 139, 237, 18, 177, 248, 22, 233, 85, 2, 208, 25, 142, 208, 128, 222, 69, 106, 152, 141, 1, 65, 4, 224, 236, 152, 138, 103, 153, 54, 206, 168, 10, 136, 230, 6, 61, 98, 220, 133, 24, 46, 84, 138, 83, 95, 174, 205, 110, 86, 159, 181, 101, 99, 61, 229, 180, 232, 61, 90, 17, 251, 173, 139, 1, 144, 140, 231, 30, 3, 116, 176, 6, 216, 70, 148, 176, 111, 16, 189, 193, 83, 202, 88, 165, 63, 135], sequence: 4294967295 }]
134155
```
135156

136-
We'll talk more about returning a prettier hex encoded display for the end user in the next lesson. For now, we can make a nice change to our code so that we don't have to write out the `impl Debug` for our struct. Instead of doing all that, we can actually just add an attribute to our struct. This is basically metadata that will tell the compiler to auto-generate the implementation for a particular **derivable** trait. As long as all the fields of the struct implement the `Debug` trait, this will work.
157+
We'll talk more about returning a prettier hex encoded display for the end user in the next lesson.
158+
For now, we can make a nice change to our code so that we don't have to write out the `impl Debug` for our struct.
159+
Instead of doing all that, we can actually just add an attribute to our struct.
160+
This is basically metadata that will tell the compiler to auto-generate the implementation for a particular **derivable** trait.
161+
As long as all the fields of the struct implement the `Debug` trait, this will work.
137162

138163
Let's remove the `impl` block and the `use std::fmt` statement and just add this attribute to our struct:
139164

140165
```rust
166+
#[allow(dead_code)]
141167
#[derive(Debug)]
142168
struct Input {
143169
txid: [u8; 32],
@@ -147,9 +173,11 @@ struct Input {
147173
}
148174
```
149175

150-
And voila! That works now and is much cleaner isn't it?
176+
And voila! That works now and is much cleaner isn't it?
177+
The attribute `#[allow(dead_code)]` is a hint to the compiler that we know we are not using the struct fields right now, but we will in the next section.
151178

152-
Now that we understand a bit more about structs and trait implementations, let's work on displaying this information in a more readable way. Rather than print out the debugging output, we'll instead **serialize** the data according to a standard JSON format and display that.
179+
Now that we understand a bit more about structs and trait implementations, let's work on displaying this information in a more readable way.
180+
Rather than print out the debugging output, we'll instead **serialize** the data according to a standard JSON format and display that.
153181

154182
### Additional Reading
155183
* Defining Structs: https://doc.rust-lang.org/book/ch05-01-defining-structs.html

0 commit comments

Comments
 (0)