Setting up your development environment
Prepare your environment for Rust app development and deployment on Google Cloud by installing the following tools.
Install Rust
-
To install Rust, see Getting Started.
-
Confirm that you have the most recent version of Rust installed:
cargo --version
Install an editor
The Getting Started guide links popular editor plugins and IDEs, which provide the following features:
- Fully integrated debugging capabilities
- Syntax highlighting
- Code completion
Install the Google Cloud CLI
The Google Cloud CLI is a set of tools for Google Cloud. It contains the
gcloud
and
bq
command-line
tools used to access Compute Engine, Cloud Storage, BigQuery, and other services
from the command line. You can run these tools interactively or in your
automated scripts.
To install the gcloud CLI, see Installing the gcloud CLI.
Install the Cloud Client Libraries for Rust in a new project
The Cloud Client Libraries for Rust is the idiomatic way for Rust developers to integrate with Google Cloud services, such as Secret Manager and Workflows.
For example, to use the package for an individual API, such as the Secret Manager API, do the following:
-
Create a new Rust project:
cargo new my-project
-
Change your directory to the new project:
cd my-project
-
Add the Secret Manager client library to the new project:
cargo add google-cloud-secretmanager-v1
If you haven't already enabled the Secret Manager API, enable it in APIs and services or by running the following command:
gcloud services enable secretmanager.googleapis.com
-
Add the google-cloud-gax crate to the new project:
cargo add google-cloud-gax
-
Add the tokio crate to the new project:
cargo add tokio --features macros
-
Edit
src/main.rs
in your project to use the Secret Manager client library:
#[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(())
}
-
Build your program:
cargo build
The program should build without errors.
Note: The source of the Cloud Client Libraries for Rust is on GitHub.
Running the program
-
To use the Cloud Client Libraries in a local development environment, set up Application Default Credentials.
gcloud auth application-default login
For more information, see Authenticate for using client libraries.
-
Run your program, supplying your Google Cloud Platform project's ID:
PROJECT_ID=$(gcloud config get project) cargo run ${PROJECT_ID}
The program will print the secrets associated with your project ID. If you don't see any secrets, you might not have any in Secret Manager. You can create a secret and rerun the program, and you should see the secret printed in the output.
What's next
- Explore authentication methods at Google.
- Browse the documentation for Google Cloud products.