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

Chapter 10.1 - Generic Data Types - Improve impl section #2679

Closed
tal-zvon opened this issue Apr 2, 2021 · 2 comments · Fixed by rust-lang/rust#91143
Closed

Chapter 10.1 - Generic Data Types - Improve impl section #2679

tal-zvon opened this issue Apr 2, 2021 · 2 comments · Fixed by rust-lang/rust#91143
Milestone

Comments

@tal-zvon
Copy link

tal-zvon commented Apr 2, 2021

Chapter 10.1 starts off very clear when it comes to generics on functions, structs, and enums. Then, in the In Method Definitions section, things get a lot more confusing.

It would be nice if there was a longer explanation about the difference between: impl<T> Point<T>, and impl Point<T>. This is briefly explained as:

By declaring T as a generic type after impl, Rust can identify that the type in the angle brackets in Point is a generic type rather than a concrete type.

But for beginners who miss this very important point, like I did, the rest stops making sense.

One of the reasons this point is easy to miss is because the example using impl Point<f32> (a concrete type) comes after the above sentence, so when you read that sentence, you haven't come across a clear example about using impl with a concrete type. I think that the whole "In Method Definitions" section can either be expanded, or placed onto its own page (Chapter 10.2?).

The other thing that never made sense to me that could be clarified here is if the generic type names in the impl block need to match the generic type names in the struct definition.

Ex:

struct Point<T> {
    x: T,
    y: T,
}

impl<T> Point<T> {
    fn x(&self) -> &T {
        &self.x
    }
}

Here, as well as all the examples in that chapter, the type name T matches between the struct definition and the impl definition. I was never clear just from reading this book if this was a requirement or not, and how T from the struct definition relates to T from the impl definition. Examples like this one, where the name is different, but related, made it a lot more obvious. I would go so far as to suggest that the example from that link, where they use impl<V> GenericVal<V, V>, be included in the book.

@tal-zvon
Copy link
Author

tal-zvon commented Apr 3, 2021

It would also help to explain that the number of type args in the impl definition after Point MUST be the same as the number of type args in the struct definition, because that's what it is matching against.

@carols10cents
Copy link
Member

Thank you for the suggestions!

It would be nice if there was a longer explanation about the difference between: impl<T> Point<T>, and impl Point<T>.

Well one difference is that impl <T> Point<T> will compile and impl Point<T> won't compile, so I'm not sure explaining invalid code would help here since it doesn't have a meaning to explain...

This is briefly explained as:

By declaring T as a generic type after impl, Rust can identify that the type in the angle brackets in Point is a generic type rather than a concrete type.

But for beginners who miss this very important point, like I did, the rest stops making sense.

One of the reasons this point is easy to miss is because the example using impl Point<f32> (a concrete type) comes after the above sentence

I've added a few sentences to hopefully clarify between the sentence you've quoted and the impl Point<f32> example, and a sentence explaining this as well:

The other thing that never made sense to me that could be clarified here is if the generic type names in the impl block need to match the generic type names in the struct definition.

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Nov 22, 2021
Update books

## reference

3 commits in a01d151a7250a540a9cb7ccce5956f020c677c21..c0f222da23568477155991d391c9ce918e381351
2021-10-22 22:34:13 +0900 to 2021-11-22 10:30:57 -0800
- Byte literal and string syntax: add missing quote escapes (rust-lang/reference#1101)
- fix crate_name attribute description (rust-lang/reference#1109)
- Update list of types with magic Copy impls (rust-lang/reference#1104)

## book

19 commits in 5c5dbc5b196c9564422b3193264f3288d2a051ce..a5e0c5b2c5f9054be3b961aea2c7edfeea591de8
2021-11-09 19:30:43 -0500 to 2021-11-19 17:06:19 -0500
- Propagate edits to chapter 4 back
- Edits to the edits of chapter 4
- ch 4 from nostarch
- Fix install links
- Snapshot of chapter 10
- Fix quotes
- Add more explanation of Self in chapter 10. Fixes rust-lang/book#2222.
- We've defined the signatures, not behaviors. Fixes rust-lang/book#2917
- Remove 'most distinctive' claim. Fixes rust-lang/book#2861.
- Clarify that traits must be brought into scope to use their methods
- Explain why lifetimes are part of the function contract better
- Further edits to rust-lang/book#2895
- Clarify generic types in mixin
- Change 'either' to 'at least one of'. See rust-lang/book#2885
- Clarify explanation of lifetime annotations a bit
- Further edits to clarify code explanation
- Clarify the meaning of a sentence in ch10-03
- Clarify this code is demonstrating usage of the library from a binary. Fixes rust-lang/book#1445.
- Clarify generics in method definitions and impls. Fixes rust-lang/book#2679.

## rust-by-example

1 commits in e9d45342d7a6c1def4731f1782d87ea317ba30c3..43f82530210b83cf888282b207ed13d5893da9b2
2021-11-02 13:33:03 -0500 to 2021-11-21 22:31:50 -0300
- Update `Development dependencies` page. (rust-lang/rust-by-example#1478)

## rustc-dev-guide

7 commits in 196ef69aa68f2cef44f37566ee7db37daf00301b..a2fc9635029c04e692474965a6606f8e286d539a
2021-11-07 07:48:47 -0600 to 2021-11-18 13:31:13 -0500
- Describe drop elaboration (rust-lang/rustc-dev-guide#1240)
- Fix an invalid link on Diagnostic Items (rust-lang/rustc-dev-guide#1261)
- Fix broken links related to `rustc_borrowck` (rust-lang/rustc-dev-guide#1259)
- Unify `x.py` usage (rust-lang/rustc-dev-guide#1258)
- Spelling fixes
- Fix compare-mode documentation
- Fix broken link in "Bootstrapping"

## edition-guide

1 commits in 27f4a84d3852e9416cae5861254fa53a825c56bd..8e0ec8c77d8b28b86159fdee9d33a758225ecf9c
2021-11-08 10:13:20 -0500 to 2021-11-12 06:30:23 -0800
- Fix a broken link in the Disjoint Capture in Closure chapter (rust-lang/edition-guide#273)

## embedded-book

1 commits in 51739471276b1776dea27cf562b974ef07e24685..8c395bdd8073deb20ca67e1ed4b14a3a7e315a37
2021-10-17 16:48:42 +0000 to 2021-11-14 11:38:31 +0000
- added math chapter  (rust-embedded/book#308)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants