Dyn Any + std::marker::Send can't use Any's trait function

I can't understand this behavior, can help explain why this happend?

The error msg:

no method named type_id found for type &dyn std::any::Any + std::marker::Send in the current scope

    let builder = thread::Builder::new().name(String::from("thread-1"));
    let thread = builder
        .spawn(|| {
            println!("thread is runing");
            panic!("error");
            println!("thread is panic");
        })
        .unwrap();

    let res = thread.join();
    println!("res is : {:?}", res);
    match res {
        Err(e) => {
            println!("erros is {}", e.deref().type_id());
        }
        _ => {}
    }

I had test some function that return Box to test whether the Trait's function can be called,
the test code passed

So i don't understand why the example code get the error message.

Thanks for help

I'm not sure why you're getting an error. This works for me (with .deref() too, but that's unnecessary):

let res = std::thread::spawn(|| panic!("hi")).join();
    match res {
        Err(e) => {
            println!("erros is {:?}", e.type_id());
        }
        _ => {}
    }

Which version of rustc are you using?

Your snippet works perfectly fine using stable Rust on the playground, I just needed to change the print statement to println!("erros is {:?}", e.deref().type_id()) because TypeId doesn't implement Display.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.