You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There are two different ways we can approach this:
12
12
1. Read the first 4 pairs of characters and do some base math to determine the u32 integer.
13
-
2. Convert the transaction into a collection bytes first and then just work with the methods available for that type.
13
+
2. Convert the transaction into a collection of bytes first and then work with the methods available for that type.
14
14
15
15
Now that we have a basic understanding of hexadecimal format and bytes, we're going to take approach #2 and leverage an external library.
16
-
It will be easier and more efficient to work with an array of `u8` integers to traverse the byte data, rather than read from a string and do unnecessary math.
16
+
It will be easier and more efficient to work with an array of `u8` integers to traverse the byte sequence, rather than read from a string and do unnecessary math.
17
17
The library can handle the conversion for us.
18
18
It will also be easier to work with as we decode the rest of the transaction.
19
19
20
-
So the first thing we want to do is convert our hexadecimal string into a collection of bytes.
20
+
The first thing we want to do is convert our hexadecimal string into a collection of bytes.
21
21
We'll use the popular [`hex` crate](https://docs.rs/hex/latest/hex/).
error[E0608]: cannot index into a value of type `Result<Vec<u8>, FromHexError>`
83
84
```
@@ -87,22 +88,21 @@ Let's examine what `hex::decode` returns.
87
88
Remember, we want to work with a Vector of bytes so a `vec<u8>` is the data type we're looking for.
88
89
However, if we look at the return type of the `decode` function we see that the data structure we want is wrapped *inside* of a `Result` type.
89
90
90
-
91
91
The `Result` type is a common `enum` that you will see in Rust code.
92
-
Enums are a way to describe a mutually exclusive set of options for a particular variable.
93
-
If you think about it, it's possible that the `hex::decode` function fails to return a proper collection of bytes.
92
+
Enums are a way to describe a mutually exclusive set of values for a particular variable.
93
+
If you think about it, the `hex::decode` function can fail to return a proper collection of bytes.
94
94
For example, what if one of the characters is not a hex character?
95
95
So we get two possibilities from a `Result`, an `Ok` response or an `Err` response.
96
+
The former represents a successful computation whereas the latter indicates an error occurred.
96
97
97
-
So how should we work with this?
98
98
There are a few different ways to work with an enum.
99
-
We'll explore different ways of handling a `Result` later on in this course, but for now we'll use the `unwrap` method.
100
99
Every `Result` enum has an [`unwrap`](https://doc.rust-lang.org/std/result/enum.Result.html#method.unwrap) method that you can call.
101
100
This will return the underlying data type if the result is an `Ok` type or it will `panic` and the program will crash if the result is an `Err` type.
102
101
Crashing your program is probably not the best way to handle an error, unless you're confident that an `Err` result *should* not be possible.
103
-
But we'll look into better ways of error handling later on.
102
+
For now we'll use the `unwrap` method.
103
+
We'll explore different ways of handling a `Result` and doing proper error handling later on in this course.
104
104
105
-
For now, let's update this so that we are actually working with the underlying vector of bytes and not the wrapped `Result` type:
105
+
For now, let's our function so that we are actually working with the underlying vector of bytes and not the wrapped `Result` type:
106
106
107
107
```rust
108
108
fnread_version(transaction_hex:&str) ->u32 {
@@ -117,10 +117,13 @@ How does the program run now?
117
117
Let's see by running `cargo run`.
118
118
119
119
We're going to get another compile error and it's going to be a confusing one:
120
-
`error[E0277]: the size for values of type [u8] cannot be known at compilation time`
120
+
121
+
```console
122
+
error[E0277]: the size for values of type [u8] cannot be known at compilation time
123
+
```
121
124
122
125
This will make more sense as we develop a better understanding of the difference between arrays, slices and vectors in Rust as well as the difference between the stack and the heap.
123
-
We'll get a better handle on these concepts as we continue on in the course, but let's get a brief overview of these concepts in the next lesson.
126
+
Let's get a brief overview of these concepts in the next lesson.
0 commit comments