Skip to content

Explanation for listing 15-7 has strange phrasing #2274

@nahla-nee

Description

@nahla-nee

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions