Quickstart: Use Compute Engine with Rust

The Compute Engine API lets you create and run virtual machines (VMs) on Google Cloud.

This guide shows you how to initialize the Compute Engine client library for Rust and perform basic operations using the library.

Prerequisites

Before you start this guide, make sure that you have an existing Google Cloud project with billing enabled.

Add the client library as a dependency

Use cargo to add the google-cloud-compute-v1 crate to your Cargo.toml file:

cargo add google-cloud-compute-v1

List virtual machines

The client to create and manage virtual machines is called Instances. You can list the VMs in a project and zone using the list() method of this type:

pub async fn sample(project_id: &str) -> anyhow::Result<()> {
    use google_cloud_compute_v1::client::Instances;
    use google_cloud_gax::paginator::ItemPaginator;

    const ZONE: &str = "us-central1-a";

    let client = Instances::builder().build().await?;
    println!("Listing instances for project {project_id}");
    let mut instances = client
        .list()
        .set_project(project_id)
        .set_zone(ZONE)
        .by_item();
    while let Some(item) = instances.next().await.transpose()? {
        println!("  {:?}", item.name);
    }
    println!("DONE");
    Ok(())
}

What's next