Cheat-code for bypassing trait bounds!
โ Rust ๐ 2026-02-20 ๐ค surdeus ๐๏ธ 1There was previously another post demonstrating a way to bypass trait bounds. That one was a bit too unreadable. Hereโs my take on it.
trait Super {
type Assoc;
}
trait Sub: Super<Assoc: std::fmt::Debug> {}
fn do_debug<O: Sub + ?Sized>(x: O::Assoc) {
println!("{x:?}");
}
// Doesn't need T: Debug
// Debug-prints the value of x.
// Works even in generic contexts where we don't have a T: Debug bound.
fn debug<T>(x: T) {
do_debug::<dyn Sub<Assoc = T>>(x);
}
fn main() {
debug("lol");
debug(123);
}
Obviously donโt use this in production lol
3 posts - 3 participants
๐ท๏ธ Rust_feed