Skip to content

Edit Chapter 09 #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

edilmedeiros
Copy link
Contributor

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.

What's happening here is that in the `read_version` function the local variable is being changed without modifying the slice in `main`.
The `transaction_bytes` variable is being treated as a mutable *copy* of the pointer to the underlying data.

Let me be more verbose since this can be pretty confusing for newcomers.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm given this long paragraph, might make sense to add another diagram in here. Let me look into creating one that could help.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was uncertain if you would approve this, but since you said there was some people with difficulty here I added anyway.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah i'm wondering if it makes more sense to replace all the extra text with a helpful diagram instead. So it doesn't feel too overwhelming to read.

@sb1752
Copy link
Contributor

sb1752 commented May 23, 2024

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 sb1752 marked this pull request as draft May 23, 2024 19:01
@sb1752
Copy link
Contributor

sb1752 commented May 23, 2024

Here is a PR I made to correct that explanation. Included the diagrams as well: #26

@edilmedeiros
Copy link
Contributor Author

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
Copy link
Contributor

sb1752 commented May 26, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants