Lifetime issue - expected lifetime getting replaced with unexpected lifetime

This code does not compile but breaks on a lifetime issue that I'd like to understand. You can read the code by following the types and expected returns.

My main function is annotated like this

// Main struct
pub struct Cocas2<'a> {
    pub combo:&'a Combo2<'a>,
    pub candles:Vec<&'a Candle>
}

// Entry point function
fn kset<'a>(mut combos:Vec<Vec<Combo2<'a>>>,y2_unsafe:&'a YorexInstrument3<'a>, yc:&YorexConfig) ->Vec<Vec<Combo2<'a>>> {

// Main function
pub fn filter2<'a,'b>(combo: &'a Combo2<'a>, candles:&'b Vec<&'a Candle>) -> Cocas2<'a> {

// This issue
let mut cocas:Vec<Cocas2<'_>> = b_clone.iter().flat_map(|cg:&Vec<Combo2<'a>>|{
  cg.iter().map(|c:&Combo2<'a>|{

  // This Line is the problem        
  let a:Cocas2<'a>  = filter2(c,y2_unsafe.candles.index(&c.instrument));

   a
   }).collect::<Vec<_>>()
}).collect::<Vec<_>>();

I am expecting the line to be valid but complains and says that b_clone is required to be burrowed for lifetime 'a. This makes sense but I need b_clone to last for some time while referring to lifetime 'a. This doesn't seem possible because whatever let a is determines the lifetime for fields in Cocas and let a will be anonymous.

Understanding this will answer a lot of questions. Probably it needs a rewrite if I can't return from a nested map longer-lifetime references.

I changed the entry point function to indicate a lifetime longer than an anonymous lifetime and assigned it to Cocas but still complained it doesn't live long enough.

fn kset<'a:'b, 'b>

I’m on mobile, so can’t give a detailed reply; the issue is likely the repeated 'a in pub combo:&'a Combo2<'a>. You have a short-lived reference to a Combo2<'a>, but the structure requires a long-lived reference instead.

1 Like

Yes, it looks suspicious on inspection. I'll try a few modifications.

Please, put more work into making your code run. I've tried fixing it to the point where I get the error message, but I gave up, because there's not enough information.

This is how far I got:
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=9cb13bbe48816acc0ed9d214216e1c94

If you can fill in some more dummy code to trigger the error you received, helping will be easier.

1 Like

The code is far from working, it is not expected that you run it and I'm only concerned with 1 specific error that I've mentioned out of maybe many.

It's not a make-it-compile problem, it's a based-on-how-the-types-are-used, how can the error mentioned be possible.

It's a support problem. You want help. You get an error message. You don't show the error message. You don't provide a code snippet, that can be compiled either in the playground or locally, to trigger the error message.

I hope someone, who is more experienced and/or smarter, can help you with the limited information you provided. I sadly can't.

@Phlopsi I suppose so. I just need a nudge in the right direction. Even one word could lead to the answer.

I think I found the answer. Basically combos is an owned type that has references. The combo reference defined by filter2 will later reference a combo that is owned by the entry function. Because of how filter2 is defined it will force Cocas to have a short lifetime not the expected long lifetime 'a.

This indirectly related to what @2e71828 said.

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.