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 #1761

Closed
estebank opened this issue Jan 11, 2019 · 1 comment · Fixed by #2363
Closed

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

estebank opened this issue Jan 11, 2019 · 1 comment · Fixed by #2363
Labels

Comments

@estebank
Copy link

From rust-lang/rust#55969

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.

@carols10cents
Copy link
Member

Yes, we could add a note here that the usage of the iterator will be explained in chapter 13... that part of the code isn't what we're trying to showcase in this example, so we are deliberately glossing over it but we could be explicitly glossing over it :)

steveklabnik added a commit that referenced this issue Jun 7, 2020
We were cheating due to Copy, and that made this example awkward.
Returning a reference is probably better here anyway, and it makes
everything flow a bit nicer.

FIxes #1761
steveklabnik added a commit that referenced this issue Jun 7, 2020
We were cheating due to Copy, and that made this example awkward.
Returning a reference is probably better here anyway, and it makes
everything flow a bit nicer.

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

Successfully merging a pull request may close this issue.

2 participants