Rust · Intermediário · Inglês · Técnico
trait bound
Restrição que exige que um tipo genérico implemente certos traits, como T: Display. Define o que a função pode fazer com o valor.
Onde usar
Aparece em assinaturas genéricas e na mensagem 'the trait bound X: Y is not satisfied'. Em review, prefira bounds mínimos, só com o que a função usa.
Em código
use std::fmt::Debug;
fn mostra<T: Debug>(x: T) {
println!("{:?}", x);
}
mostra(5);
struct Sem;
// mostra(Sem); erro: Sem não é DebugExemplos
The trait bound is not satisfied.
O trait bound não foi satisfeito.
Add a Debug bound to the generic parameter.
Adicione um bound de Debug ao parâmetro genérico.
Cuidado
Bound não é herança: ele não cria relação entre tipos, só exige que o trait esteja implementado.
Sinônimos
restrição de trait, bound