quest

Sign in to start training

quest keeps your progress, streaks, and scores tied to your account — so you can pick up on any device.

Synced everywhere. Progress follows you across laptop and phone.
Streaks & stats. See your daily run and every score over time.
Your profile. A name and avatar for the work you do here.

We only use Google to sign you in and sync progress. No posting, no contacts — ever.

Core Intermediate

10  Traits & generics

Traits are Rust's interfaces; generics let one function serve many types. Give a trait a default method, then write a generic 'largest'.

Step 2 / 2

Write a generic largest over a non-empty slice.

Hints
  • Start with the first element as the best, then scan the rest and replace it when you find bigger.
  • The PartialOrd bound is what lets you write item > best for any T.
10_traits_generics.rs
Reset to starterCompiled in a sandbox; hidden checks must pass.
Reveal solution
fn largest<T: PartialOrd + Copy>(items: &[T]) -> T {
    let mut best = items[0];
    for &item in &items[1..] {
        if item > best {
            best = item;
        }
    }
    best
}