Cheat-code for bypassing trait bounds!

โš“ Rust    ๐Ÿ“… 2026-02-20    ๐Ÿ‘ค surdeus    ๐Ÿ‘๏ธ 1      

surdeus

There 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);
}

(playground)

Based on: Associated type bound from supertrait isn't checked for trait object ยท Issue #152607 ยท rust-lang/rust ยท GitHub

Obviously donโ€™t use this in production lol

3 posts - 3 participants

Read full topic

๐Ÿท๏ธ Rust_feed