The type parameter may not live long enough + sync::mpsc::Sender

#1

Hello,

I have a problem while using generics and std::sync::mpsc::{Sender, Receiver} , and I wonder if someone can explain me better my error?

Here is my problem: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=6431b36e2220793481d5479df35fc8d6

I don’t understand why this lifetime is asked, because the “Sender” struct is Send, whatever is the type inside? Is that a limitation of generics + Sender/Receiver?

Thank you in advance,
Denis

#3

The Box is what’s forcing you here, as it defaults to 'static. You could add a lifetime like this:

impl<'a, T: 'a> A<Sender<T>> {
    fn de(self) -> Box<dyn Test + 'a> {
        Box::new(self)
    }
}
2 Likes