Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

A generic type is a type that is defined using one or more parametric types or values. It is not, however, a real type, as it not compiled as-is.

For it to be compiled, we need to realize a version of it. This means we need to fix the parameters.

In Rust, the generic notation uses the bracket characters <> and can be applied to various objects: enum, struct, trait, function, method.

Here is an example of a generic function which turns each integer value of an array into its negative counterpart. It takes a generic integer parameter. The parameter is used inside the function’s argument to accept an array of signed integers:

[-15, -9, -15000]
[-320, -4]

Here is another example of a function that just prints its argument. Since we print using the debug formatter (?), we have to declare that our generic parameter T needs to implement the Debug trait:

15
[15, 4]
true
"Hello"