Skip to content
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

Explanation for listing 15-7 has strange phrasing #2274

Closed
nahla-nee opened this issue Feb 22, 2020 · 5 comments · Fixed by #2332
Closed

Explanation for listing 15-7 has strange phrasing #2274

nahla-nee opened this issue Feb 22, 2020 · 5 comments · Fixed by #2332

Comments

@nahla-nee
Copy link

list 15-7 has the following code snippet:

fn main() {
    let x = 5;
    let y = Box::new(x);

    assert_eq!(5, x);
    assert_eq!(5, *y);
}

With the following explanation

The only difference between Listing 15-7 and Listing 15-6 is that here we set y to be an instance of a box pointing to the value in x rather than a reference pointing to the value of x.

This is confusing because y is not pointing to the value of x, it created a copy of x's value and stored that in the heap. The explanation suggests (to me at least) that *y and x refer to the same location in memory

@sl4m
Copy link
Contributor

sl4m commented May 5, 2020

I noticed this as well. What do you think would be a better explanation? I thought about modifying the listing example to something like this, but it does stray slightly away from the previous listing (15-6)

fn main() {
    let mut x = 5;
    let y = Box::new(x);
    
    x += 1;
    assert_eq!(6, x);
    assert_eq!(5, *y);
}

@nahla-nee
Copy link
Author

maybe something like this?

The only difference between Listing 15-7 and Listing 15-6 is that here we set y to be an instance of a box pointing to a new integer with the same value as x, rather than a reference pointing to the value of x.

@sl4m
Copy link
Contributor

sl4m commented May 7, 2020

maybe something like this?

The only difference between Listing 15-7 and Listing 15-6 is that here we set y to be an instance of a box pointing to a new integer with the same value as x, rather than a reference pointing to the value of x.

I think that reads a lot better. 👍 Shall it mention copy at all? Otherwise, maybe one of us can create a PR?

@nahla-nee
Copy link
Author

you can feel free to create the PR if you want. As far as copying I feel that is implied since you can't get a new value that is the same as the one x had without copying the value of x, but it maybe better to be verbose about it, i'll leave that up to you.

@nahla-nee nahla-nee reopened this May 12, 2020
@nahla-nee
Copy link
Author

oop wrong button

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 a pull request may close this issue.

2 participants