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. Verify your installation:
cargo --versionInstall an editor or IDE.
Select or create a Google Cloud project with billing enabled.
Install the Google Cloud CLI and initialize it 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:
Create a binary crate:
cargo new my-projectChange to the new project directory:
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:
Enable the Secret Manager API in your project:
gcloud services enable secretmanager.googleapis.comAdd the Secret Manager client library to your project:
cargo add google-cloud-secretmanager-v1Add the
google-cloud-gaxcrate for pagination helpers:cargo add google-cloud-gaxAdd the
tokiocrate for the asynchronous runtime:cargo add tokio --features macros,rt-multi-threadReplace the contents of
src/main.rswith the following code to list secrets in your project:Build your program to verify that it compiles without errors:
cargo build
Run your Rust program
Authenticate your local environment and run the sample program:
Configure Application Default Credentials for your local environment:
gcloud auth application-default loginRun your program, passing your Google Cloud project ID as an argument:
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 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 that you created
and remove the local my-project directory when you're finished.