Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Check if struct implements a specific trait
// Based on:
// trait for T
trait TTrate {
const VALUE: bool = false;
impl<T> TTrate for T {}
// custom trait
trait MyTrait {}
impl MyTrait for Vec<u32> {}
// example structure
struct Example<T>(std::marker::PhantomData<T>);
impl<T: MyTrait> Example<T> {
const VALUE: bool = true;
// run
fn main() {
println!("Implements MyTrait?");
println!("=> {}", Example::<u32>::VALUE);
println!("=> {}", Example::<Vec<u32>>::VALUE);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment