Some questions about life cycle

fn get_bigger<'a,'b>(p1 :&'a i32,p2 :&'b i32) -> &'a i32
where 
	'b: 'a,
{
	if p1 > p2{
		p1
	}else{
		p2 // reutrn a longer lifetimes
	}
}

If the compiler checks my code according to the shorter life cycle, this is indeed no problem, but will it happen that I return a reference with a longer life cycle, and the compiler checks according to the shorter life cycle, will this cause me Wasted some lifecycle?

"Lifetime", not "life cycle".

The lifetimes of shared references are covariant, meaning a longer lifetime can coerce to a shorter lifetime. That's what's going on in your example. You don't return a &'b i32 when you return p2; it gets coerced into an &'a i32.

I don't know what that means.

1 Like

Thanks!
Rust shortened my lifetime. In fact, I had some places that were available, but rust banned them.

It's no clear to your real problem, some code that caused compile error may help

In this case, Rust compiler must garentee all the references are available, so borrow checker will regard the lifetime of the return reference of the function as the shortest lifetime that it possible

maybe only Chinese will say "生命周期" as "life circle"
LOL

Ahh