May 2019
Intermediate to advanced
698 pages
17h 21m
English
We can also write impl blocks for our generic types too, but it gets verbose here because of the extra generic type parameters, as we'll see. Let's implement a new() method on our Container<T> struct:
// generic_struct_impl.rsstruct Container<T> { item: T}impl Container<T> { fn new(item: T) -> Self { Container { item } }}fn main() { // stuff}
Let's compile this:

The error message cannot find our generic type T. When writing an impl block for any generic type, we need to declare the generic type parameter before using it within our type. T is just like a variable—a type variable—and we need to declare it. Therefore, ...
Read now
Unlock full access