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

Dereferencing seems to be used in the book before it's explained #55969

Closed
DanielSank opened this issue Nov 15, 2018 · 1 comment
Closed

Dereferencing seems to be used in the book before it's explained #55969

DanielSank opened this issue Nov 15, 2018 · 1 comment

Comments

@DanielSank
Copy link

The Rust book (2018 edition) uses the following example in chapter 10 (comments added by me)

fn largest(list: &[i32]) -> i32 {
    let mut largest = list[0];  // i32

    for &item in list.iter() {  // item is &i32
        if item > largest {
            largest = item;
        }
    }

    largest
}

This was and still is puzzling to me for several reasons:

  1. list is &[i32], so I'd expect list[0] to be &i32. Instead, list[0] is i32...
  2. which is extra weird because list.iter() seems to yield &i32's.
  3. Finally, what the heck is &item? I thought & made references, but here it seems to do the opposite.

I gather that there's some kind of implicit dereferencing going on when we do list[0], and I guess &item is also a dereference. It would be nice if this were explained in the book before it shows up in the example.

@estebank
Copy link
Contributor

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

No branches or pull requests

2 participants