Skip to content

Mutable.replace_with() confusion #84

Answered by Pauan
sullyj3 asked this question in Q&A
Discussion options

You must be logged in to vote

My assumption is that the ordinary way to use replace_with is just to have f return the new value to be placed inside the Mutable without directly mutating through that reference. This is the interface that makes intuitive sense to me.

Your assumption is correct. Because the closure must return an owned value, it cannot (directly) return the reference, so it will usually return a completely new value.

The only other thing that would happen in the case of mutation would be that the caller of replace_with is given back value, which I guess would be whatever value f wrote through &mut state.value.

Yes, that is one possible use case.

But the bigger use case is when the closure needs to mu…

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@sullyj3
Comment options

@Pauan
Comment options

@sullyj3
Comment options

Answer selected by sullyj3
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