Pybites Logo Rust Platform

Generic Functions

Medium +3 pts

🎯 In Python, functions work with any type by default — duck typing:

def middle(items): if not items: return None return items[len(items) // 2] middle([10, 20, 30]) # 20 middle(["a", "b", "c", "d"]) # "b"

No type annotations needed. If it has __getitem__, it works. If it doesn't — runtime error.

Rust's approach is different: generic functions work with many types, but the compiler verifies at compile time that each type supports the operations you use. …

Login to see the full exercise.