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

Tweak example in chapter 10 #2363

Merged
merged 1 commit into from
Jun 7, 2020
Merged

Tweak example in chapter 10 #2363

merged 1 commit into from
Jun 7, 2020

Conversation

steveklabnik
Copy link
Member

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
@steveklabnik steveklabnik merged commit 48b9625 into master Jun 7, 2020
@steveklabnik steveklabnik deleted the gh1761 branch June 7, 2020 13:35
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request Jun 8, 2020
Update books

## nomicon

3 commits in d1517d4e3f29264c5c67bce2658516bb5202c800..bfe1ab96d717d1dda50e499b360f2e2f57e1750a
2020-05-12 13:47:00 -0400 to 2020-06-05 13:19:42 -0400
- Clarify that str data must still be initialized
- Remove language-level UB for non-UTF-8 str
- fix Nomicon transmute UB

## reference

5 commits in becdca9477c9eafa96a4eea5156fe7a2730d9dd2..5d40ba5c2515caffa7790cda621239dc21ef5a72
2020-05-21 21:08:02 +0100 to 2020-06-06 20:25:36 -0700
- Add some links to Disambiguating Function Calls. (rust-lang/reference#829)
- change bash to sh as shell code blocks language indentifier (rust-lang/reference#827)
- Fix sentence mistake in array-expr.md (rust-lang/reference#826)
- removed the word "Second" form the beginning of the 2nd list item and  labelled it as `2` (rust-lang/reference#822)
- Update fn-like proc-macro invocation restrictions. (rust-lang/reference#816)

## book

14 commits in e8a4714a9d8a6136a59b8e63544e149683876e36..30cd9dfe71c446de63826bb4472627af45acc9db
2020-05-25 10:29:27 -0500 to 2020-06-07 23:07:19 -0500
- Unnecessarily long type name in Ch 13 (rust-lang/book#2362)
- Tweak example in chapter 10 (rust-lang/book#2363)
- Mention that to_lowercase isn't perfect (rust-lang/book#2364)
- fix typo in CONTRIBUTING.md (rust-lang/book#2360)
- Link German translation in appendix F (rust-lang/book#2347)
- Updates wording on Box example (rust-lang/book#2332)
- fix: match 15-24 with 15-18 (rust-lang/book#2324)
- Reword ch01-03 recap paragraph (rust-lang/book#2305)
- Remove some confusing wording. (rust-lang/book#2358)
- Clarify some wording a bit (rust-lang/book#2357)
- Update ch12-05 PowerShell note (rust-lang/book#2348)
- text -> console (rust-lang/book#2352)
- Improve wording around drop (rust-lang/book#2350)
- Make some statements about crates more correct (rust-lang/book#2349)

## edition-guide

1 commits in 0a8ab5046829733eb03df0738c4fafaa9b36b348..82bec5877c77cfad530ca11095db4456d757f668
2020-05-18 08:34:23 -0500 to 2020-06-03 08:56:02 -0500
- Add stuff for Rust 1.33 (rust-lang/edition-guide#214)
@dalrrard
Copy link
Contributor

dalrrard commented Sep 5, 2020

The changes merged in this PR seem to have affected the continuity of this example into Chapter 10.2. None of the error codes associated with these two line up and the borrowing flips throughout the two chapters without rewriting the example code.

Chapter 10.2

fn largest<T: PartialOrd + Copy>(list: &[T]) -> T {
    let mut largest = list[0];

    for &item in list {
        if item > largest {
            largest = item;
        }
    }

    largest
}

Chapter 10.1

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

    for item in list {
        if item > largest {
            largest = item;
        }
    }

    largest
}

Written error

$ cargo run
   Compiling chapter10 v0.1.0 (file:///projects/chapter10)
error[E0369]: binary operation `>` cannot be applied to type `T`
 --> src/main.rs:5:17
  |
5 |         if item > largest {
  |            ---- ^ ------- T
  |            |
  |            T
  |
  = note: `T` might need a bound for `std::cmp::PartialOrd`

Actual error

binary operation `>` cannot be applied to type `&T`
 --> generics/src/main.rs:5:17
  |
5 |         if item > largest {
  |            ---- ^ ------- T
  |            |
  |            &T
  |
help: consider restricting type parameter `T`
  |
1 | fn largest<T: std::cmp::PartialOrd>(list: &[T]) -> &T {
  |             ^^^^^^^^^^^^^^^^^^^^^^

error[E0308]: mismatched types
 --> generics/src/main.rs:6:23
  |
1 | fn largest<T>(list: &[T]) -> &T {
  |            - this type parameter
...
6 |             largest = item;
  |                       ^^^^ expected type parameter `T`, found `&T`
  |
  = note: expected type parameter `T`
                  found reference `&T`

error[E0308]: mismatched types
  --> generics/src/main.rs:10:5
   |
1  | fn largest<T>(list: &[T]) -> &T {
   |            - this type parameter
...
10 |     largest
   |     ^^^^^^^
   |     |
   |     expected `&T`, found type parameter `T`
   |     help: consider borrowing here: `&largest`
   |
   = note:   expected reference `&T`
           found type parameter `T`

error: aborting due to 3 previous errors

It seems like there was a PR that fixed most of the issues with Chapter 10.1 by adding let mut largest = &list[0];, but the compiler still throws a different error than written.

error[E0369]: binary operation `>` cannot be applied to type `&T`
 --> generics/src/main.rs:5:17
  |
5 |         if item > largest {
  |            ---- ^ ------- &T
  |            |
  |            &T
  |
help: consider restricting type parameter `T`
  |
1 | fn largest<T: std::cmp::PartialOrd>(list: &[T]) -> &T {
  |             ^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error

(https://doc.rust-lang.org/book/ch10-02-traits.html#fixing-the-largest-function-with-trait-bounds)

listings/ch10-generic-types-traits-and-lifetimes/listing-10-05/src/main.rs

src/ch10-01-syntax.md

@steveklabnik
Copy link
Member Author

Can you open a new issue for this please?

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 this pull request may close these issues.

Dereferencing seems to be used in the book before it's explained
2 participants