Locking a mutex in a function call

I have a blocking receiver that needs some shared state to work.
I wrote something like this.

blocking_recv(state_mutex().lock().unwrap().clone());

To my surprise I deadlocked the service. A few desperate sighs later and I tried the following:

let state = state_mutex.lock().unwrap().clone();
blocking_recv(state);

What is the explanation for this behavior? Why is the guard dropped after the fn returns?

1 Like

Temporary values are dropped at end of the enclosing statement, which in this case is after the function returns.

3 Likes

And the reason for this behavior is that in general, the function call might want to borrow something from the temporary rather than cloning it as you do — so the drop order has to be such as to permit that.

2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.