Skip to content

What is it mean "extend" in "Limits of Lifetimes" on rust nomicon? #35

Answered by lebensterben
towry asked this question in Q&A
Discussion options

You must be logged in to vote

The borrow checker always tries to minimize the extent of a lifetime

'x doesn't mean the block has lifetime 'x, but the block can at most live as long as 'x.

So the &mut foo can live as long as 'c


Then for the very basic rules that you cannot have mutable and immutable reference at same time.

Borrow checker needs to prove whether it can reduce the lifetime of foo so that it doesn't collide with the lifetime of the immutable borrow.

If it proves that, the &mut foo is done before the &foo then there's no conflict and it compiles.

If you move the "println" before &foo (and if there's no use or the mutable borrow after that), the lifetime of &mut foo would ends before &foo. That doesn't vi…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by towry
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants