Skip to content

Commit fe1dd37

Browse files
committed
Break long paragraph lines
1 parent 8f0c2c8 commit fe1dd37

File tree

1 file changed

+37
-12
lines changed

1 file changed

+37
-12
lines changed

13_structs.md

Lines changed: 37 additions & 12 deletions
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 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,8 @@ 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? One neat modification we can make here is remove the key/value pair and just put a comma separated list of field names.
45+
We can do this anytime the field names match the variable names:
3946

4047
```rust
4148
...
@@ -55,7 +62,8 @@ Pretty simple right? One neat modification we can make here is remove the key/va
5562
...
5663
```
5764

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()`.
65+
Let's change this up so that we have an `inputs` vector and we'll `push` each decoded input into that.
66+
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()`.
5967

6068
```rust
6169
...
@@ -86,7 +94,8 @@ Now that we have a collection of `inputs`, let's try printing out the debug outp
8694
...
8795
```
8896

89-
Try running `cargo run` and see what happens. We'll get a compiler error:
97+
Try running `cargo run` and see what happens.
98+
We'll get a compiler error:
9099
```console
91100
error[E0277]: `Input` doesn't implement `Debug`
92101
--> src/main.rs:89:30
@@ -105,9 +114,12 @@ help: consider annotating `Input` with `#[derive(Debug)]`
105114
|
106115
```
107116

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.
117+
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.
118+
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.
109119

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

112124
```rust
113125
impl fmt::Debug for Input {
@@ -122,18 +134,30 @@ impl fmt::Debug for Input {
122134
}
123135
```
124136

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

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`.
142+
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.
143+
The field method takes in a field name to display along with the value.
144+
The value itself must be something that implements the `Debug` trait.
145+
Luckily, all of our fields do.
146+
At the end, we call `finish` which returns the required `fmt::Result`.
128147

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

131151
```shell
132152
Version: 1
133153
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 }]
134154
```
135155

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.
156+
We'll talk more about returning a prettier hex encoded display for the end user in the next lesson.
157+
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.
158+
Instead of doing all that, we can actually just add an attribute to our struct.
159+
This is basically metadata that will tell the compiler to auto-generate the implementation for a particular **derivable** trait.
160+
As long as all the fields of the struct implement the `Debug` trait, this will work.
137161

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

@@ -149,7 +173,8 @@ struct Input {
149173

150174
And voila! That works now and is much cleaner isn't it?
151175

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.
176+
Now that we understand a bit more about structs and trait implementations, let's work on displaying this information in a more readable way.
177+
Rather than print out the debugging output, we'll instead **serialize** the data according to a standard JSON format and display that.
153178

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

0 commit comments

Comments
 (0)