Lifetime conversion explanation Why is allowed?
โ Rust ๐ 2025-11-22 ๐ค surdeus ๐๏ธ 10I got a question how does this compile.
fn this_function<'a, 'b>(arg: &'a mut &'b u8) ->
&'b mut &'a u8
{
let a: &'a mut &'a u8 = arg;
a
}
Is there some implicit lifetime bound, why able to โswapโ 2 lifetimes
Also if we introduce 2 new lifetimes
fn functionthing<'a, 'b, 'c, 'd>(arg: &'a mut &'b u8) ->
&'b mut &'a u8
where
'b: 'c, 'c: 'b,
'b: 'd, 'd: 'b,
{
let a: &'d mut &'c u8 = arg;
a
}
but at the bounds you can replace the bounds to
โb: โc, โc: โa,
โb: โc, โc: โb
No compile error so extrapolating to (and changing &โc โฆ to &โd โฆ)
fn functionthing<'a, 'b, 'c, 'd>(arg: &'a mut &'b u8) ->
&'b mut &'a u8
where
/*โb or โa*/: 'c, 'c: /*โb or โa*/,
/*โb or โa*/: 'd, 'd: /*โb or โa*/,
{
let a: &/*โc or โd*/ mut &/*โc orโd*/u8 = arg;
a
}
I tested some combination in rust playground but why does this work when compiling,
and does changing bounds, change the usability of this function
3 posts - 2 participants
๐ท๏ธ Rust_feed