You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The Rust book (2018 edition) uses the following example in chapter 10 (comments added by me)
fnlargest(list:&[i32]) -> i32{letmut largest = list[0];// i32for&item in list.iter(){// item is &i32if item > largest {
largest = item;}}
largest
}
This was and still is puzzling to me for several reasons:
list is &[i32], so I'd expect list[0] to be &i32. Instead, list[0] is i32...
which is extra weird because list.iter() seems to yield &i32's.
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.
The text was updated successfully, but these errors were encountered:
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 :)
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
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
From rust-lang/rust#55969
The text was updated successfully, but these errors were encountered: