the trait bound \ is not satisfied means a function or generic requires a trait that your type doesnβt implement.
Fix 1: Derive the trait
Many common traits can be auto-derived:
// β Missing Debug, Clone, etc.
struct User { name: String }
// β
Derive common traits
#[derive(Debug, Clone, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
struct User { name: String }
Fix 2: Implement the trait manually
use std::fmt;
impl fmt::Display for User {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "User: {}", self.name)
}
}
Fix 3: Add trait bounds to your generics
// β T might not implement Display
fn print_it<T>(item: T) {
println!("{}", item);
}
// β
Require Display
fn print_it<T: std::fmt::Display>(item: T) {
println!("{}", item);
}
Common traits youβll need
Debugβ for{:?}formattingClone/Copyβ for duplicating valuesPartialEq/Eqβ for==comparisonHashβ for use in HashMap keysSend/Syncβ for thread safetySerialize/Deserializeβ for serde (JSON, etc.)
Related: Rust cheat sheet Β· Rust: Cannot Borrow as Mutable Β· Rust: Value Used After Move Β· Cargo Cheat Sheet