我实现了 tonic helloworld教程。然后我尝试更改客户端代码,以便在等待任何请求之前发送多个请求。
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let num_requests = 10;
let mut client = GreeterClient::connect("http://[::1]:50051").await?;
let mut responses = Vec::with_capacity(num_requests);
for _ in 0..num_requests {
let request = tonic::Request::new(HelloRequest {
name: "Tonic".into(),
});
responses.push(client.say_hello(request));
}
for resp in responses {
assert!(resp.await.is_ok());
}
Ok(())
}
这会导致编译错误:
error[E0499]: cannot borrow `client` as mutable more than once at a time
--> src/client.rs:19:24
|
19 | responses.push(client.say_hello(request));
| ^^^^^^ mutable borrow starts here in previous iteration of loop
这是否意味着'client.say_hello()'返回一个仍然引用客户端的类型,因此我不能再次调用'say_hello',它本身需要'&mut self'?有没有办法在调用“等待”之前继续提出请求?