Pular para o conteúdo
TE822 · Tipos · error

Trait não existe

Um bound na assinatura da função nomeia um trait que o compilador não conhece.

Why this fires

A bound in a function signature names a trait that the compiler doesn't recognise.

fn format_all<T: Printable>(items: [T]) {
    //            ^^^^^^^^^ error[TE822]: unknown trait `Printable`
}

Common causes:

  • A typo (e.g. Disply instead of Display, Ordd instead of Ord).
  • Using a trait from another language ecosystem (e.g. Rust's Clone, Haskell's Functor).
  • Referring to a user-defined trait that hasn't been declared yet.

Fix it

1. Correct the spelling

Check the std-trait catalogue below and use the exact name.

2. Use the right std trait

The built-in trait names are: Eq, Ord, Add, Sub, Mul, Div, Mod, Neg, Display, Hash.

// ❌ Unknown trait
fn show<T: Printable>(x: T) { ... }

// ✅ Correct
fn show<T: Display>(x: T) { ... }

3. Declare the trait first

If you need a custom trait, declare it before using it as a bound:

trait Serializable {
    fn serialize(self) -> str
}

fn save<T: Serializable>(value: T) {
    let data = value.serialize()
    // ...
}

See also

  • TE820 — the concrete type doesn't implement a (valid) required trait.
  • TE821 — operator used on an unbounded generic param.

Veja também

Buscar no Zolo

9 resultados

en