This code does not compile:
fn range_intersect(mut a: &Range<usize>, mut b: &Range<usize>) -> Option<Range<usize>> {
if a.start > b.start {
std::mem::swap(&mut a, &mut b);
}
if b.start < a.end {
Some(b.start..a.end)
} else {
None
}
}
This is because a
and b
have different lifetimes.
I fixed it like so:
fn range_intersect<'a>(mut a: &'a Range<usize>, mut b: &'a Range<usize>) -> Option<Range<usize>>
But then I was thinking, why do I need to annotate this? This could be automatic. In particular, if a fn
's output type has the static lifetime, all elided input lifetimes should be able to have the same lifetime (that of the function call). What do people think about implementing this relaxation of the elision rules?