← All Exercises

Rust Trait Bounds

intermediaterust~20min

Background

Trait bounds constrain generics to types with specific behavior, enabling type-safe generic algorithms.

Problem

Write a generic Rust function that finds the largest element in a slice, using a PartialOrd trait bound.

Input

A slice of elements implementing PartialOrd and Copy.

Output

An Option containing the largest element, or None for empty slices.

Sample tests

Sample 1

Input: [3, 1, 4, 1, 5, 9, 2, 6]
Expected: Some(9)

Your solution

Learning objective

Learn trait bounds, compound bounds with + , and the relationship between traits and generics.