generic
Rust Generic functions
Use a type parameter when the same operation must work with different types. The caller supplies or allows Rust to infer a concrete type. This example returns a package unchanged, transferring ownership for non-Copy inputs. Expected output (not executed): 12 parcel Why: Each call chooses a concrete T. No operation on T beyond moving it is needed. Common mistake: Assuming T supports arithmetic or formatting without a suitable bound.
Formal Syntax & Structure
rust syntaxrust
fn name<T>(value: T) -> TWorking Example
Example Snippetrust
fn keep<T>(value: T) -> T { value }fn main() { println!("{}", keep(12)); println!("{}", keep(String::from("parcel")));}