0Pricing
Learn Rust Coding · Lesson

Calling from a gRPC Client

Consume the service.

The Generated Client

For each service tonic generates a client struct, for example GreeterClient, in the greeter_client module. It exposes one async method per rpc.

You construct it from a transport channel or by connecting directly to an endpoint.

use greeter::v1::greeter_client::GreeterClient;
use greeter::v1::HelloRequest;

Connecting to a Server

The simplest path is GreeterClient::connect, which takes an address string and returns a connected client.

It runs inside an async context, so call it under #[tokio::main] with .await.

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut client = GreeterClient::connect("http://[::1]:50051").await?;
    Ok(())
}

All lessons in this course

  1. Protobuf and Service Definitions
  2. Generating Code with tonic-build
  3. Implementing a gRPC Server
  4. Calling from a gRPC Client
← Back to Learn Rust Coding