This document shows you how to set up a local Rust development environment,
install one of the Cloud Client Libraries for Rust, and run your first Google Cloud
API request.

## Before you begin

Before running the commands in this document, complete the following
prerequisites:

- Install Rust by following the official
  [Rust getting started guide](https://www.rust-lang.org/learn/get-started). Verify your installation:

  ```bash
  cargo --version
  ```
- Install an editor or IDE.

- Select or create a [Google Cloud project](https://docs.cloud.google.com/resource-manager/docs/creating-managing-projects.md.txt) with
  [billing enabled](https://docs.cloud.google.com/billing/docs/how-to/verify-billing-enabled.md.txt#confirm_billing_is_enabled_on_a_project).

- Install the [Google Cloud CLI](https://docs.cloud.google.com/sdk/docs/install-sdk.md.txt) and
  [initialize it](https://docs.cloud.google.com/sdk/docs/initialize.md.txt) so that an active account and default project are
  configured.

## Create a Rust project

Complete the following steps to create a binary crate for your program:

1. Create a binary crate:

   ```bash
   cargo new my-project
   ```
2. Change to the new project directory:

   ```bash
   cd my-project
   ```

## Install a Google Cloud client library

The Cloud Client Libraries for Rust provide idiomatic Rust clients for
Google Cloud services such as Secret Manager. To add and build
the Secret Manager client library, complete the following steps:

1. Enable the Secret Manager API in your project:

   ```bash
   gcloud services enable secretmanager.googleapis.com
   ```
2. Add the [Secret Manager client library](https://docs.cloud.google.com/secret-manager/docs/overview.md.txt)
   to your project:

   ```bash
   cargo add google-cloud-secretmanager-v1
   ```
3. Add the [`google-cloud-gax` crate](https://crates.io/crates/google-cloud-gax) for pagination helpers:

   ```bash
   cargo add google-cloud-gax
   ```
4. Add the [`tokio` crate](https://crates.io/crates/tokio) for the asynchronous runtime:

   ```bash
   cargo add tokio --features macros,rt-multi-thread
   ```
5. Replace the contents of `src/main.rs` with the following code to list
   secrets in your project:


   ```rust
   #[tokio::main]
   async fn main() -> Result<(), Box<dyn std::error::Error>> {
       use google_cloud_gax::paginator::ItemPaginator as _;
       use google_cloud_secretmanager_v1::client::SecretManagerService;
       let project_id = std::env::args().nth(1).unwrap();
       let client = SecretManagerService::builder().build().await?;

       let mut items = client
           .list_secrets()
           .set_parent(format!("projects/{project_id}"))
           .by_item();
       while let Some(item) = items.next().await {
           println!("{}", item?.name);
       }
       Ok(())
   }
   ```

   <br />

6. Build your program to verify that it compiles without errors:

   ```bash
   cargo build
   ```

## Run your Rust program

Authenticate your local environment and run the sample program:

1. Configure Application Default Credentials for your local environment:

   ```bash
   gcloud auth application-default login
   ```

   > [!NOTE]
   > **Note:** For more information about authentication, see [Authenticate with client libraries](https://docs.cloud.google.com/docs/authentication/client-libraries.md.txt).

2. Run your program, passing your Google Cloud project ID as an
   argument:

   ```bash
   PROJECT_ID=$(gcloud config get project)
   cargo run -- "${PROJECT_ID}"
   ```

   The program prints the names of the secrets in your project. If your
   project doesn't have any secrets yet, [create a secret](https://docs.cloud.google.com/secret-manager/docs/creating-and-accessing-secrets.md.txt) and
   run the command again to see it in the output.

## Clean up

To avoid incurring charges to your Google Cloud account for the resources
used in this document, [delete any test secrets](https://docs.cloud.google.com/secret-manager/docs/delete-secrets.md.txt) that you created
and remove the local `my-project` directory when you're finished.