bitcoin-dev-project / bitcoiner-intro-to-rust

The Bitcoiner's Introduction to Rust
68 stars 18 forks source link

Edit Chapter 09 #25

Closed edilmedeiros closed 3 months ago

edilmedeiros commented 4 months ago

I can see people getting confused with this, but I believe you did a great job explaining the copy of references. I tried to expand a bit and explain step by step what is happening. Please see the individual commits, in particular the top one.

sb1752 commented 4 months ago

So I did a little investigation here and I believe I'm explaining this slightly incorrectly, though the general idea is still the same.

This line for example: println!("transaction_bytes memory address before reading {:p}", transaction_bytes); will print the memory address of the internal buffer (the heap location), not the structure itself. If we want the structure itself, we'd have to add the & in front and print the memory address of &transaction_bytes.

This makes sense since when this memory address updates, it's consistently 4 greater than the previous memory address.

So I updated the two diagrams that we can use to show what's going on here. I don't think the transaction_bytes actually gets updated to point to the new bytes_slice. That bytes_slice structure remains the same and simply points to a new location on the heap.

These are the two diagrams I have. Let me know what you think.

Mutable Copy Memory Diagram Updated

Mutable Reference Memory Diagram Updated

sb1752 commented 4 months ago

Here is a PR I made to correct that explanation. Included the diagrams as well: https://github.com/sb1752/bitcoiner-intro-to-rust/pull/26

edilmedeiros commented 3 months ago

The new diagrams are a huge improvement. I tend to be more verbose and explain the figures I add in my texts anyhow, but I don't want to disturb your style. Based on people's difficulties and feedback, do you believe the diagrams are enough or there are more concept they are finding hard to grasp?

sb1752 commented 3 months ago

Yeah I'm not sure. I think the diagrams might do a lot of heavy lifting though. They def helped me quite a bit when reading the Programming Rust book.