Lifetime issues for generic and recursive trait impl


I want to apply a trait to an Option<T> for all T: MyTrait. I find this kind of hard to explain:

use std::borrow::Borrow;

struct Consumer {}
impl Consumer {
    fn consume<T>(&self, _v: T) where T: MyTrait { }

trait MyTrait {
    fn do_something<T>(consumer: &Consumer, value: T) where T: Borrow<Self>;

impl<U> MyTrait for Option<U> {
    fn do_something<'a, T>(consumer: &Consumer, value: T)
            where T: Borrow<Self>, &'a U: MyTrait {
        if let Some(v) = value.borrow() {
        } else {

This get’s me a: the parameter typeUmay not live long enough. I tried several other solutions and received different error messages. I cannot wrap my head around this.

ps: I successfully implemented this pattern for &[T] but I fail to transfer this to an Option



Hi, I got it to compile but I don’t really know if that’s what you wanted playground

1 Like


Sorry that I can only give you a single like! This absolutely solves my problem!

I haven’t seen the where for clause yet. I have an idea how this works but would like to read more about it. Does this have a name or do you know the location in the book?

Thank you very much!



It’s called Higher-Rank Trait Bounds you can find here and you can google it if you want to learn even more about it

1 Like


Thanks again!