๐Ÿ˜ตFunction Generics

Function generics are special functions that can operate with generic types. This allows us to create a function template whose functionality can be adapted to multiple types or classes without repeating the entire code for each type.

A generic parameter is a special parameter that can be used to pass a type as an argument: just like regular function parameters can be used to pass values to a function, template parameters allow to pass also types to a function. These function templates can use these parameters like any other regular type.

func add<T: std::ops::Add<T>>(a: T, b: T) {
    return a + b;
}

add(1,2) // 3
add("hello-", "world") // hello-world

Generics 101

๐Ÿ˜ดpageGenerics

Last updated