Self reference clone in one line

Hi, I have tonic grpc client that is cloning the Client<Channel> like the docs suggested.

I have this 2 kind implementation of the cloning. As im really new in rust, which one is suggested and why?

let mut client = self.tonic_client.clone();
let response = client.method().await;


let response = self.tonic_client.clone().method().await;

Im not sure how this differences will affect the scope, especially when the self is called by multiple caller concurrently.

I don't know about tonic, but as a matter of Rust semantics, the way in which the two versions are different is that in the first, client will be dropped (deallocated) at the end of the containing block, and in the second, the client (not stored in any variable) will be dropped right after response is given its value.

This does not have any effect at all on which things are mutated or which things are shared versus independent, so

when the self is called by multiple caller concurrently.

there will be no difference unless there is some reason it matters when the clone is dropped.


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.