Skip to content

Some questions about generics #124

Answered by mgeisler
Luosuu asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @Luosuu,

Yeah, the instructor should highlight that get_x can be called on both integers and strings! Let us add that to the speaker notes...

As for the return type, you're right that it's impl Display. The return type is chosen by the person who writes get_x. The caller can only rely on Display being implemented, nothing more.

This is different from the generic in the argument position: the type of name is chosen by the caller of get_x. The person who write the function cannot know anything about the type, except that it implements Display. This allows name to be used with format! as shown in the example.

Does that help a bit? Generics is a complex topic and I need to expand the slide…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@Luosuu
Comment options

Answer selected by Luosuu
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants