hd-lang

Tour · 14 of 25

One generic function serves every type its bound allows, and the bound is checked

top_by finds the item with the largest key: the priciest order, the latest date, the longest name. It is written once, for any item type T and any key type K, and the closure you pass picks the key. The bound K < Ord says what top_by needs from a key: that two keys can be compared. The compiler checks the bound at each call, so a key that can't be ordered is rejected where you pass it, not deep inside the function.

Run: Ctrl+Enter · Pages: Alt+← Alt+→