Get started with Rust

Before you get started, you should meet the following prerequisites:

  • Install Rust. If you have not yet installed Rust, see the Rust documentation on Getting started.

    You can confirm that you have Rust installed (and your version of Rust) with the following command:

    cargo --version
    
  • Install an editor or IDE.

  • Install the Google Cloud CLI.

Create a new Rust project

To create a new Rust project, run the following command:

cargo new my-project

Change your directory to the new project by running the following command:

cd my-project

Install a Google Cloud Client Library

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.

  1. Add the Secret Manager client library to your new project:

    cargo add google-cloud-secretmanager-v1
    
  2. 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
    
  3. Add the google-cloud-gax crate to the new project:

    cargo add google-cloud-gax
    
  4. Add the tokio crate to the new project:

    cargo add tokio --features macros
    
  5. 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(())
    }
    
  6. Finally, build your program:

    cargo build
    

The program should build without errors.

Running your Rust application

To use the Cloud Client Libraries in a local development environment, set up Application Default Credentials.

To set up Application Default Credentials, run the following command:

gcloud auth application-default login

Run your program, supplying your Google Cloud project 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. Create a secret and rerun the program, and you should see the secret printed in the output.