January 2019
Beginner to intermediate
554 pages
13h 31m
English
To create a generic function, we place the generic type parameter immediately after the function name and before the parenthesis, like so:
// generic_function.rsfn give_me<T>(value: T) { let _ = value;}fn main() { let a = "generics"; let b = 1024; give_me(a); give_me(b);}
In the preceding code, give_me is a generic function with <T> after its name, and the value parameter is of type T. In main, we can call this function with any argument. During compilation, our compiled object file will contain two specialized copies of this function. We can confirm this in our generated binary object file by using the nm command, like so:
nm is a utility from the GNU binutils package for viewing symbols from compiled object files. By ...
Read now
Unlock full access