A polymorphic function, or generic function, avoids duplicated implementations by accepting types or other compile-time parameters. It offers a more readable alternative to macros when ordinary function signatures communicate the operation clearly. section 1.3.2
Use generic functions deliberately:
- Prefer them for reducing duplication when a normal function remains easy to understand. section 1.3.2
- Consider generic implementation blocks or blanket implementations when behavior should apply across types satisfying a trait bound. section 1.3.2
- Avoid broad abstractions that make behavior difficult to discover or understand. section 1.3.2
Listing 1.2 The powers and dangers of generics
trait Hello {
fn hello(&self);
}
impl<T:Copy> Hello for T {
fn hello(&self) {
println!("Hello world");
}
}
fn main() {
2.hello();
true.hello();
'c'.hello();
}
A blanket implementation applies one trait method to every type satisfying Copy, demonstrating generic behavior across numbers, characters, and Booleans. section 1.3.2



















