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 4 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.