Pular para o conteúdo
TE823 · Tipos · error

Operador exige que o tipo concreto implemente um trait

Um operador é usado sobre um valor de struct/enum concreto cujo tipo não implementa o trait exigido. Sem ele não há método para dirigir o operador, e quebraria em runtime.

Why this fires

An operator (<, >, <=, >=, +, -, *, /, %, or unary -) is used on a concrete struct/enum value whose type doesn't implement the required trait. Without the trait there's no method to drive the operator, so it would crash at runtime.

struct Tag { id: int }
let a = Tag { id: 1 }
let b = Tag { id: 2 }
let worse = a < b
//          ^^^^^ error[TE823]: operator `<` requires `Tag` to implement `Ord`

Fix it

Implement the trait. For ordering, add impl Ord with a cmp method returning -1 / 0 / 1; <, >, <=, >= are all derived from it:

impl Ord for Tag {
    fn cmp(self, other: Tag) -> int {
        if self.id < other.id { 0 - 1 } else { if self.id > other.id { 1 } else { 0 } }
    }
}

For arithmetic, implement Add/Sub/… (method add/sub/… or @op("+")).

Notes

  • ==/!= are NOT gated — every struct/enum has automatic structural equality.
  • Generic type parameters use TE821 (missing bound) instead.

See also

Veja também

Buscar no Zolo

9 resultados

en