Skip to content

Commit 8f0c2c8

Browse files
authored
Merge pull request #31 from BBlackwo/misc-updates
Update 08 quiz solution and some typo fixes
2 parents 9167723 + a98abaf commit 8f0c2c8

File tree

4 files changed

+10
-7
lines changed

4 files changed

+10
-7
lines changed

Diff for: 05_vectors_and_result_enum.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ Crashing your program is probably not the best way to handle an error, unless yo
102102
For now we'll use the `unwrap` method.
103103
We'll explore different ways of handling a `Result` and doing proper error handling later on in this course.
104104

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:
105+
For now, let's update our function so that we are actually working with the underlying vector of bytes and not the wrapped `Result` type:
106106

107107
```rust
108108
fn read_version(transaction_hex: &str) -> u32 {

Diff for: 08_traits_and_reading_bytes.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ pub trait Read {
9191

9292
You'll notice there's no function body, just the signature.
9393
It means the `read` method itself is not actually implemented with any logic in the trait declaration.
94-
We expect the types that "implement" this trait to aactually provide the function logic for any *required* method, or trait methods, that have no implementation.
94+
We expect the types that "implement" this trait to actually provide the function logic for any *required* method, or trait methods, that have no implementation.
9595

9696
A trait can also provide other methods that a type can get access to once it has implemented the trait.
9797
These are known as *provided* methods and are considered *default* implementations since they can also be overwritten.

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,4 @@ By the end of this project, you will have understood the fundamental concepts of
4343
* [20.0 Command Line Arguments](20_command_line_arguments.md)
4444
* [21.0 Refactoring and the Rust-Bitcoin Library](21_refactoring_and_rust_bitcoin.md)
4545
* [22.0 Decoding Segwit](22_decoding_segwit.md)
46+
* [Quiz Solutions](quiz_solutions/)

Diff for: quiz_solutions/08_solution.md

+7-5
Original file line numberDiff line numberDiff line change
@@ -44,24 +44,26 @@ For now, if we wanted to print a `vec` we would have to use `{:?}` inside the pr
4444

4545
Let's attempt to implement the `Display` trait for a `vec` by implementing the required method `fmt`:
4646
```rust
47-
use std::fmt;
47+
use std::fmt::{self, Display, Formatter};
4848

49-
impl Display for Vec<u8> {
49+
struct CustomVec<T>(Vec<T>);
50+
51+
impl Display for CustomVec<u8> {
5052
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
5153
write!(f, "Values:\n")?;
52-
for v in &self {
54+
for v in &self.0 {
5355
write!(f, "\t{}", v)?;
5456
}
5557
Ok(())
5658
}
5759
}
5860

5961
fn main() {
60-
let vec: Vec::<u8> = vec![0, 0, 0, 0, 0];
62+
let vec: CustomVec<u8> = CustomVec(vec![0, 0, 0, 0, 0]);
6163
println!("Vec: {}", vec);
6264
}
6365
```
6466

65-
The basic idea is that we leverage the `write!` macro which takes the `Formatter` instance and writes some information to it. If any step fails, an error will be returned (we'll talk more about error handling and the `?` operator in chapter 19). If we iterate through the vector and are able to write all values successfully we can simply return the `Ok(())` result, which matches the the expected result type `fmt::Result`.
67+
The basic idea is that we leverage the `write!` macro which takes the `Formatter` instance and writes some information to it. If any step fails, an error will be returned (we'll talk more about error handling and the `?` operator in chapter 19). If we iterate through the vector and are able to write all values successfully we can simply return the `Ok(())` result, which matches the the expected result type `fmt::Result`. We need to create a `CustomVec<T>` using the [Newtype Pattern](https://doc.rust-lang.org/book/ch19-03-advanced-traits.html#using-the-newtype-pattern-to-implement-external-traits-on-external-types) to implement external traits on external types.
6668

6769
This might still be a bit confusing at this stage, so consider coming back to revisit this solution after you've gone through the course.

0 commit comments

Comments
 (0)