1

If rust code like below

struct LifeTimeRequired<'a> {
    src : &'a str
}

impl <'a> LifeTimeRequired<'a> {
    fn new(src:&'a str) -> Self {
        Self { src }
    }

    fn print(&self) {
        println!("{}", self.src);
    }
}


struct Wrapper<'a> {
    origin : LifeTimeRequired<'a>
}

impl <'a> Wrapper<'a> {
    fn print(&self) {
        println!("Wrapped");
        self.origin.print();
    }
}

fn main() {
    let wrapper = Wrapper { origin : LifeTimeRequired::new("abcd") };
    wrapper.print();
}

But i want to remove lifetime elision. (Above code is just example)

Like this :

struct LifeTimeRequired<'a> {
    src : &'a str
}

impl <'a> LifeTimeRequired<'a> {
    fn new(src:&'a str) -> Self {
        Self { src }
    }

    fn print(&self) {
        println!("{}", self.src);
    }
}


struct Wrapper {
    value : String,
    origin : LifeTimeRequired<'struct>
}

impl Wrapper {
    fn new(value:String) -> Self {
        Self {
            value,
            origin : LifeTimeRequired::new( value.as_str() )
        }
    }

    fn print(&self) {
        println!("Wrapped");
        self.origin.print();
    }
}

fn main() {
    let wrapper = Wrapper::new( "abcd".to_string() );
    wrapper.print();
}

Because the reference value is inner structure value.

I know this raise the error. And changing the structure can solve the above problem, but the LifeTimeRequired structure is an extern crate and my case is a bit more complicated.

So what I'm curious about is how to solve it while maintaining the above structure(missing keyword?), or if there is a similar RFC going on.

0 Answers0