Rust by Example中文

13.1 Functions

The same set of rules can be applied to functions: a type T becomes generic when preceded by <T>.

Using generic functions sometimes requires explicitly specifying type parameters. This may be if the function is called where the return type is generic, or if the compiler doesn't have enough information to infer the necessary type parameters.

A function call with explicitly specified type parameters looks like: fun::<A, B, ...>().

struct A; // Concrete type `A`. struct S(A); // Concrete type `S`. struct SGen<T>(T); // Generic type `SGen`. // These functions all take ownership of the variable passed into // them and immediately go out of scope freeing the variable. // Define a function `reg_fn` that takes an argument `s` of type `S`. // This has no `<T>` so this is not a generic function. fn reg_fn(_s: S) {} // Define a function `gen_spec_t` that takes an argument `s` of type `SGen<T>` // that has been explicitly given the type parameter `A`. Because A has not // been specified as a generic type parameter for gen_spec_t, it is not generic. fn gen_spec_t(_s: SGen<A>) {} // Define a function `gen_spec_i32` that takes an argument `s` of type `SGen<i32>` // that has been explicitly given the type parameter `i32`. // This function is also not generic. fn gen_spec_i32(_s: SGen<i32>) {} // Define a function `generic` that takes an argument `s` of type `SGen<T>`. // Because `SGen<T>` is preceded by `<T>`, this function is generic over `T`. fn generic<T>(_s: SGen<T>) {} fn main() { // Using the non-generic functions reg_fn(S(A)); // Concrete type. gen_spec_t(SGen(A)); // Implicitly specified type parameter `A`. gen_spec_i32(SGen(6)); // Implicitly specified type parameter `i32`. // Explicitly specified type parameter `char` to `generic()`. generic::<char>(SGen('a')); // Implicitly specified type parameter `char` to `generic()`. generic(SGen('c')); }

See also:

functions and structs