1. 59
    Rustlings generics2: Converting concrete types to generic implementations
    1m 1s

Rustlings generics2: Converting concrete types to generic implementations

Chris Biscardi
InstructorChris Biscardi
Share this video with your friends

Social Share Links

Send Tweet
Published 4 years ago
Updated 3 years ago

README for this exercise.

Chris Biscardi: [0:00] In generics2, we have a concrete struct Wrapper that works on a concrete type u32, and we have impl Wrapper for u32. Other than that, we have a couple of tests that use the struct in the implementation. We have to rewrite it using generics to let us support wrapping any type.

[0:15] Note that in the first test, we're using integers. In the second test, we're using strings. To convert this to operating on a generic type, we need to replace the concrete type u32 with the generic type <T>.

<[0:27] T> doesn't mean anything. It's just a letter. We can say that the Wrapper struct operates on a type <T> where the value is a T. The implementation on <T> for wrapper allows us to pass a value of type <T> into new and set the value on the struct. This has allowed us to pass the first test.

[0:45] Our second test is still failing because we have an assert that we no longer need. If we enable the second test, we can see that our Wrapper type now operates on integers and on strings using the same APIs, just like the vector type that we used in generics1 as well as a number of other exercises.

egghead
egghead
~ 7 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today