Skip to content

Guide 25 of 53

Trait aliases

Name a group of trait requirements and reuse it in bounds.

trait alias PrintableOrder = Ord + Display groups requirements. Use the alias in an inline bound, a where clause, an associated-type bound or a let value: impl Alias binding. Nested aliases expand transitively; every member trait is checked. Aliases do not create a runtime trait, and cannot be implemented. Implement their members individually. Cycles are errors even when the alias is unused.

trait alias PrintableOrder = Ord + Display
fn maximum<T: PrintableOrder>(a: T, b: T) -> T {
    if a > b { a } else { b }
}
print(maximum(12, 7))

Effect aliases continue to use type Alias = EffectA + EffectB; their namespace and meaning remain separate.

DOCS / FEEDBACK

Did this page leave a question?

Tell us where the explanation lost you. Documentation is part of the language experience.

Global index

Find your way through Zolo

Try an idea

Start here

9 results

9 results

enespt-br