While let borrow lifetime seems to be too long

It looks like the RefMut is kept alive longer than expected because of some variation of temporary lifetime extension. However, it seems temporaries in the matched expression of while let and match do not follow the same rules as other loops and conditionals such as if let or if or while.

If I understand correctly, temporaries in these matched expressions are always kept alive until the end of the entire while let or match block, even in cases where it should not be required.

This behavior is not documented or specified anywhere, as far as I can tell. There was an issue filed about this a long time ago, and more recently this documentation issue and this forum thread.

3 Likes