{"id":9557,"date":"2025-02-26T05:00:14","date_gmt":"2025-02-26T13:00:14","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/cosmosdb\/?p=9557"},"modified":"2025-02-26T03:39:25","modified_gmt":"2025-02-26T11:39:25","slug":"announcing-the-public-preview-of-the-azure-cosmos-db-sdk-for-rust","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/cosmosdb\/announcing-the-public-preview-of-the-azure-cosmos-db-sdk-for-rust\/","title":{"rendered":"Announcing the Public Preview of the Azure Cosmos DB SDK for Rust!"},"content":{"rendered":"<p>We\u2019re excited to announce the public preview of the Azure Cosmos DB SDK for Rust, a native Rust SDK that enables developers to interact with Azure Cosmos DB for NoSQL accounts from their Rust applications.<\/p>\n<p>Following the release of <a href=\"https:\/\/nam06.safelinks.protection.outlook.com\/?url=https%3A%2F%2Fdevblogs.microsoft.com%2Fazure-sdk%2Frust-in-time-announcing-the-azure-sdk-for-rust-beta%2F&amp;data=05%7C02%7CTheo.van%40microsoft.com%7C03b862f485894ba8cb0e08dd511536d8%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C638755873805576780%7CUnknown%7CTWFpbGZsb3d8eyJFbXB0eU1hcGkiOnRydWUsIlYiOiIwLjAuMDAwMCIsIlAiOiJXaW4zMiIsIkFOIjoiTWFpbCIsIldUIjoyfQ%3D%3D%7C0%7C%7C%7C&amp;sdata=yLbXR6Be9kyUcdnYBlM8Jxwx746yNgn7Re5r4yd3HnM%3D&amp;reserved=0\" target=\"_blank\" rel=\"noopener\">Azure SDK for Rust Beta<\/a>, the Azure Cosmos DB Rust SDK provides an idiomatic API for performing operations on databases, containers, and items. With this release, Rust developers can now build high-performance, scalable applications using Azure Cosmos DB.<\/p>\n<p>Rust is an excellent choice for modern application development due to its focus on performance, memory safety, and concurrency. Rust eliminates common bugs such as null pointer dereferences and buffer overflows through its powerful ownership model. Additionally, Rust\u2019s strong type system and borrow checker ensure thread safety, making it ideal for building reliable, concurrent applications at scale. With its growing ecosystem and support for WebAssembly, Rust is increasingly becoming a go-to language for performance-critical workloads, cloud services, and distributed systems like Azure Cosmos DB.<\/p>\n<h3>Getting started<\/h3>\n<p data-pm-slice=\"1 1 []\"><strong>Prerequisites:<\/strong><\/p>\n<ul data-spread=\"false\">\n<li>An Azure subscription, an <a href=\"https:\/\/cosmos.azure.com\/try\/\" target=\"_blank\" rel=\"noopener\">Azure Cosmos DB for NoSQL free trial account<\/a>, or the <a href=\"https:\/\/learn.microsoft.com\/azure\/cosmos-db\/emulator\" target=\"_blank\" rel=\"noopener\">Azure Cosmos DB Emulator<\/a>.<\/li>\n<li>A working Rust development environment (Rust 1.70+ recommended).<\/li>\n<\/ul>\n<h3>Running a Rust program<\/h3>\n<p>To run a Rust program using the Azure Cosmos DB SDK, follow these steps:<\/p>\n<ol start=\"1\" data-spread=\"false\">\n<li>Ensure Rust and Cargo are installed. If not, install them using <a href=\"https:\/\/rustup.rs\/\">rustup.<\/a><\/li>\n<li>Create a new Rust project:\n<pre><code>cargo new cosmosdb-example\r\ncd cosmosdb-example<\/code><\/pre>\n<\/li>\n<li>Install the Azure Cosmos DB Rust SDK dependencies:\n<pre><code>cargo install azure_data_cosmos\r\ncargo install azure_identity<\/code><\/pre>\n<\/li>\n<li>Replace the contents of <code>src\/main.rs<\/code> with your Rust program.<\/li>\n<li>Build and run the program:\n<pre><code>cargo run<\/code><\/pre>\n<\/li>\n<\/ol>\n<h3><\/h3>\n<h3>Installing dependencies<\/h3>\n<p>For the samples below, create the following dependencies in Cargo.toml:<\/p>\n<pre class=\"prettyprint language-default\"><code class=\"language-default\">[dependencies]\r\n<span data-teams=\"true\">azure_data_cosmos = \"0.22.0\"\r\nazure_identity = \"0.22.0\"\r\nazure_core = \"0.22.0\"<\/span>\r\nclap = { version = \"4.3\", features = [\"derive\"] }\r\ntracing = \"0.1\"  \r\ntokio = { version = \"1\", features = [\"full\"] }  # for async support\r\ntracing-subscriber = { version = \"0.3\", features = [\"env-filter\"] }\r\nserde = { version = \"1.0\", features = [\"derive\"] }  # For JSON serialization\/deserialization\r\nserde_json = \"1.0\"                                  # For working with JSON data\r\nfutures = \"0.3\"   <\/code><\/pre>\n<h3>Creating a client<\/h3>\n<p>To interact with Azure Cosmos DB, you need an account. You can create one using the <a href=\"https:\/\/portal.azure.com\/\">Azure portal<\/a>, Azure CLI, or Azure Resource Manager templates. If you don\u2019t have an Azure Subscription, you can <a href=\"https:\/\/cosmos.azure.com\/try\/\" target=\"_blank\" rel=\"noopener\">create a free trial account<\/a> or use the <a href=\"https:\/\/learn.microsoft.com\/azure\/cosmos-db\/emulator\" target=\"_blank\" rel=\"noopener\">Azure Cosmos DB Emulator<\/a>.<\/p>\n<p>Once you have an account, you can create a client using <code>CosmosClient::new<\/code> and authenticate using either the <code>DefaultAzureCredential<\/code> (recommended) or an account key.<\/p>\n<h4>Use DefaultAzureCredential for authentication (recommended)<\/h4>\n<p><code>DefaultAzureCredential<\/code> provides authentication using Microsoft Entra ID (formerly Azure Active Directory) and is the recommended approach for production and development scenarios.<\/p>\n<pre class=\"prettyprint language-default\"><code class=\"language-default\">use azure_core::StatusCode;\r\nuse azure_data_cosmos::{CosmosClient, PartitionKey};\r\nuse azure_identity::DefaultAzureCredential;\r\nuse serde::Serialize;\r\nuse futures::StreamExt;\r\nuse std::env;\r\nuse tokio;\r\n\r\n#[tokio::main(flavor = \"current_thread\")]\r\nasync fn main() -&gt; Result&lt;(), Box&lt;dyn std::error::Error&gt;&gt; {\r\n    let credential = DefaultAzureCredential::new().unwrap();\r\n    let client = CosmosClient::new(\"myCosmosAccount\", credential, None)?;\r\n\r\n    println!(\"Cosmos client initialized successfully\");\r\n\r\n    Ok(())\r\n}<\/code><\/pre>\n<h3>Retrieve a database<\/h3>\n<p>To retrieve a database, use the <code class=\"language-default\">database_client<\/code> method on <code class=\"language-default\">client<\/code>:<\/p>\n<pre class=\"prettyprint language-default\"><code class=\"language-default\">\/\/ must exist - create database not supported in Entra ID\r\nlet database = client.database_client(\"my-database\");<\/code><\/pre>\n<h3>Creating a container<\/h3>\n<p>To retrieve a container, use <code class=\"language-default\">container_client<\/code> on <code class=\"language-default\">database<\/code>:<\/p>\n<pre class=\"prettyprint language-default\"><code class=\"language-default\">\/\/ must exist with myPartitionKey as partition key for examples below - create container not supported in Entra ID)\r\nlet container_client = database.container_client(\"my-container\");<\/code><\/pre>\n<h3>Performing operations on items<\/h3>\n<h4>Create an item<\/h4>\n<p>To insert an item into a container, use <code class=\"language-default\">create_item<\/code>:<\/p>\n<pre class=\"prettyprint language-default\"><code class=\"language-default\">#[derive(Serialize)] \/\/ensure serde is in Cargo.toml as a dependency \r\nstruct Item {\r\n    id: String,\r\n    myPartitionKey: String,\r\n    name: String,\r\n}\r\n\r\nlet item = Item {\r\n    id: \"aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb\".to_string(),\r\n    myPartitionKey: \"myPartitionKeyValue\".to_string(),\r\n    name: \"Theo\".to_string(),\r\n};\r\n\r\nlet partition_key = PartitionKey::from(item.myPartitionKey.clone());\r\n\r\ncontainer_client.create_item(partition_key, item, None).await?;<\/code><\/pre>\n<p>&nbsp;<\/p>\n<h4>Read an item<\/h4>\n<p>To read an item, use <code class=\"language-default\">read_item<\/code>:<\/p>\n<pre class=\"prettyprint language-default\"><code class=\"language-default\">\r\n\/\/ read item\r\nlet read_response = container_client\r\n    .read_item(\"myPartitionKeyValue\", \"aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb\", None)\r\n    .await;\r\nmatch read_response {\r\n    Err(e) if e.http_status() == Some(StatusCode::NotFound) =&gt; println!(\"Item not found!\"),\r\n    Ok(r) =&gt; {\r\n        let item: serde_json::Value = r.into_json_body().await?;\r\n        println!(\"Found item:\");\r\n        println!(\"{:#?}\", item);\r\n    }\r\n    Err(e) =&gt; return Err(e.into()),\r\n};<\/code><\/pre>\n<h4>Query items<\/h4>\n<p>You can perform queries using SQL-like syntax:<\/p>\n<pre class=\"prettyprint language-default\"><code class=\"language-default\">\/\/ query items\r\nlet query = \"SELECT * FROM c\";\r\n\r\nlet pk = PartitionKey::from(\"myPartitionKeyValue\");\r\nlet mut items =\r\n    container_client.query_items::&lt;serde_json::Value&gt;(&amp;query.to_string(), pk, None)?;\r\n\r\nwhile let Some(page) = items.next().await {\r\n    let page = page?.into_body().await?;\r\n    println!(\"Query results page\");\r\n    println!(\"  Items:\");\r\n    for item in page.items {\r\n        println!(\"    * {:#?}\", item);\r\n    }\r\n}<\/code><\/pre>\n<h3>Next steps<\/h3>\n<p>The Azure Cosmos DB client library for Rust enables developers to build highly available applications with Azure Cosmos DB. Check out our <a href=\"https:\/\/aka.ms\/cosmos-db-rust-sdk-samples\">samples repository<\/a> and follow the <a href=\"https:\/\/aka.ms\/cosmos-db-rust-sdk-quickstart\">quickstart guide<\/a> to get started. Find out more about the Azure SDK for Rust Beta <a href=\"https:\/\/nam06.safelinks.protection.outlook.com\/?url=https%3A%2F%2Fdevblogs.microsoft.com%2Fazure-sdk%2Frust-in-time-announcing-the-azure-sdk-for-rust-beta%2F&amp;data=05%7C02%7CTheo.van%40microsoft.com%7C03b862f485894ba8cb0e08dd511536d8%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C638755873805576780%7CUnknown%7CTWFpbGZsb3d8eyJFbXB0eU1hcGkiOnRydWUsIlYiOiIwLjAuMDAwMCIsIlAiOiJXaW4zMiIsIkFOIjoiTWFpbCIsIldUIjoyfQ%3D%3D%7C0%7C%7C%7C&amp;sdata=yLbXR6Be9kyUcdnYBlM8Jxwx746yNgn7Re5r4yd3HnM%3D&amp;reserved=0\" target=\"_blank\" rel=\"noopener\">here<\/a>.<\/p>\n<p>We welcome feedback! If you have questions or suggestions, please report issues on our <a href=\"https:\/\/github.com\/Azure\/azure-sdk-for-rust\">GitHub repository<\/a>. We look forward to seeing what you build with Azure Cosmos DB and Rust!<\/p>\n<h3>About Azure Cosmos DB<\/h3>\n<p>Azure Cosmos DB is a fully managed and serverless NoSQL and vector database for modern app development, including AI applications. With its SLA-backed speed and availability as well as instant dynamic scalability, it is ideal for real-time NoSQL and MongoDB applications that require high performance and distributed computing over massive volumes of NoSQL and vector data.<\/p>\n<p><a href=\"https:\/\/cosmos.azure.com\/try\/\" target=\"_blank\" rel=\"noopener\">Try Azure Cosmos DB for free here.<\/a> To stay in the loop on Azure Cosmos DB updates, follow us on <a href=\"https:\/\/twitter.com\/AzureCosmosDB\" target=\"_blank\" rel=\"noopener\">X<\/a>, <a href=\"https:\/\/aka.ms\/AzureCosmosDBYouTube\" target=\"_blank\" rel=\"noopener\">YouTube<\/a>, and <a href=\"https:\/\/www.linkedin.com\/company\/azure-cosmos-db\/\" target=\"_blank\" rel=\"noopener\">LinkedIn<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We\u2019re excited to announce the public preview of the Azure Cosmos DB SDK for Rust, a native Rust SDK that enables developers to interact with Azure Cosmos DB for NoSQL accounts from their Rust applications. Following the release of Azure SDK for Rust Beta, the Azure Cosmos DB Rust SDK provides an idiomatic API for [&hellip;]<\/p>\n","protected":false},"author":9387,"featured_media":9610,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[14],"tags":[940],"class_list":["post-9557","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-core-sql-api","tag-rust"],"acf":[],"blog_post_summary":"<p>We\u2019re excited to announce the public preview of the Azure Cosmos DB SDK for Rust, a native Rust SDK that enables developers to interact with Azure Cosmos DB for NoSQL accounts from their Rust applications. Following the release of Azure SDK for Rust Beta, the Azure Cosmos DB Rust SDK provides an idiomatic API for [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/posts\/9557","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/users\/9387"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/comments?post=9557"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/posts\/9557\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/media\/9610"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/media?parent=9557"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/categories?post=9557"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/tags?post=9557"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}