Setting up Rust on Cloud Shell
Cloud Shell is a great environment to run small examples and tests.
Start up Cloud Shell
-
Open https://shell.cloud.google.com to start a new shell.
-
Select a project.
Configure Rust
-
Cloud Shell comes with rustup pre-installed. You can use it to install and configure the default version of Rust:
rustup default stable
-
Confirm that you have the most recent version of Rust installed:
cargo --version
Install Rust client libraries in Cloud Shell
-
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
-
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_secretmanager_v1::client::SecretManagerService;
let project_id = std::env::args().nth(1).unwrap();
let client = SecretManagerService::new().await?;
let mut items = client
.list_secrets(format!("projects/{project_id}"))
.paginator()
.await
.items();
while let Some(item) = items.next().await {
println!("{}", item?.name);
}
Ok(())
}
-
Run your program, replacing
[PROJECT ID]
with the id of your project:cargo run [PROJECT ID]