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.

Here is an example of a generic function. It takes a single parameter T and uses it to define the type of its single argument:

3

The previous function does not do anything useful, and thus does not put any constraint on the type T. Usually, we use T inside the function’s code, like calling one of its methods or using an operator on it. This implies that T must provide some abilities. The following example needs the T type to be compared using the > operator. This implies that T must implement the PartialOrd trait. The function returns the highest element in an array of any type T:

We apply the function first on a list of integers:

The largest number is 100
()

Now we apply it on a list of characters:

The largest char is y
()

see solution