Skip to content

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

@tal-zvon

Description

@tal-zvon

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions