Rust Generics Generic functions


Example

Generic functions allow some or all of their arguments to be parameterised.

fn convert_values<T, U>(input_value: T) -> Result<U, String> {
  // Try and convert the value.
  // Actual code will require bounds on the types T, U to be able to do something with them.
}

If the compiler can't infer the type parameter then it can be supplied manually upon call:

let result: Result<u32, String> = convert_value::<f64, u32>(13.5);