Curriculum Traits & generics Generics
Generics
Rust generics are monomorphized and usually constrained with traits. TypeScript's generic syntax transfers conceptually, but Rust wants bounds where behavior is used. Planned exercises: 1. choose generic function syntax. 2. recognise Vec<T>. 3. choose a trait bound for formatting or comparison. 4. fill <T>. 5. fill T: Clone. 6. write a generic identity function. 7. write a generic container struct. 8. translate a TS generic helper. 9. fix a generic function by adding the smallest required bound.
Exercises
9 ready
01
pick one
Declare a function identity that takes a value of any type
02
pick one
Pick the type name that means "vector of T" where T is a
03
pick one
A generic function prints its argument with {}. Pick the
04
fill blanks
Fill the generic parameter declaration so this function takes
05
fill blanks
Fill the trait bound. The function needs to CLONE its input
06
write a program
Write fn identity<T>(x: T) -> T returning its input
07
write a program
Define a generic container struct Pair<T> { first: T, second: T }
08
write a program
Translate the TS generic helper to Rust. pair(a, b) returns
09
write a program
This program fails with error[E0277]: T` doesn't implement