How to initialize a client

The Google Cloud Client Libraries for Rust use clients as the main abstraction to interface with specific services. Clients are implemented as Rust structs, with methods corresponding to each RPC offered by the service. To use a Google Cloud service using the Rust client libraries, you need to first initialize a client.

In this guide you'll initialize a client and then use the client to make an RPC using the Secret Manager API. The same structure applies to any other service in Google Cloud.

Before following this guide, you should do the following:

Dependencies

With Rust, you must declare the dependency in your Cargo.toml file:

$ cargo add google-cloud-secretmanager-v1

To initialize a client, you first call Client::builder() to obtain an appropriate ClientBuilder and then call build() on that builder to create a client.

The following creates a client with the default configuration, which is designed to meet requirements for most use cases.

    let client = SecretManagerService::builder().build().await?;

Once the client is successfully initialized, you can use it to make RPCs:

    use google_cloud_gax::paginator::Paginator as _;
    let mut items = client
        .list_locations()
        .set_name(format!("projects/{project_id}"))
        .by_page();
    while let Some(page) = items.next().await {
        let page = page?;
        for location in page.locations {
            println!("{}", location.name);
        }
    }

This example shows a call to list_locations, which returns information about the supported locations for the service (in this case, Secret Manager).

The output of the example should look something like this:

projects/123456789012/locations/europe-west8
projects/123456789012/locations/europe-west9
projects/123456789012/locations/us-east5