快速入門導覽課程:搭配使用 Compute Engine 和 Rust

Compute Engine API 可讓您在 Google Cloud建立及執行虛擬機器 (VM)。

本指南說明如何初始化 Rust 適用的 Compute Engine 用戶端程式庫,並使用該程式庫執行基本作業。

必要條件

開始使用本指南前,請確認您已有Google Cloud 專案,且已啟用計費功能。

將用戶端程式庫新增為依附元件

使用 cargo 將 google-cloud-compute-v1 crate 新增至 Cargo.toml 檔案:

cargo add google-cloud-compute-v1

列出虛擬機器

用於建立及管理虛擬機器的用戶端稱為 Instances。您可以使用此類型的 list() 方法,列出專案和可用區中的 VM:

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(())
}

後續步驟