我对 Rust 的使用还很陌生。我正在尝试使用 Rust、Actix 和 Azure Cosmosdb。目前我只是尝试连接到 cosmosdb 并返回结果。我现在对任何结果都感到满意。目前,我在一个函数中有一些代码,应该只返回一个带有 Cosmos 集合名称的字符串。我无法理解如何返回字符串作为结果。我需要返回结果,因为它是一个异步函数。如果有人能以正确的方式指出我,我将非常感激。 Cosmos 代码来自
azure_data_cosmos::prelude::*
;代码如下:
async fn datastuff() -> Result<String, String> {
let primary_key = std::env::var("COSMOSDB_READKEY").expect("Set env variable COSMOS_PRIMARY_KEY first!");
let account = std::env::var("COSMOSDB_ACCOUNT").expect("Set env variable COSMOS_ACCOUNT first!");
let database_name = "azdeployer";
let collection_name = "resourcetemplates";
// First, create an authorization token. There are two types of tokens: primary and resource constrained.
// Please check the Azure documentation or the examples folder on how to create and use token-based permissions.
let authorization_token = AuthorizationToken::primary_key(&primary_key)?;
// Next we will create a Cosmos client.
let client = CosmosClient::new(account, authorization_token);
// We know the database so we can obtain a database client.
let database = client.database_client(database_name);
// We know the collection so we can obtain a collection client.
let collection = database.collection_client(collection_name);
//let cosmos_collection_client: CollectionClient = cosmos::cosmosclient::create_collection_client().expect("prutser");
//let document_client: DocumentClient = cosmos_collection_client.document_client("resourceGroups@2022-09-01", "resourcegroup");
let documents1 = collection.list_documents();
let documents = collection.collection_name();
let result: String = documents.to_string();
Ok(result)
我收到的错误是:
error[E0277]: `?` couldn't convert the error
to `std::string::String` --> src\main.rs:17:76 | 17 | let
authorization_token = AuthorizationToken::primary_key(&primary_key)?;
|
^ the trait `std::convert::From<azure_core::error::Error>` is not
implemented for `std::string::String` | = note: the question
mark operation (`?`) implicitly performs a conversion on the error
value using the `From` trait = help: the following other types
implement trait `std::convert::From<T>`:
<std::string::String as std::convert::From<char>>
<std::string::String as std::convert::From<bytestring::ByteString>>
<std::string::String as std::convert::From<Box<str>>>
<std::string::String as std::convert::From<uuid::Uuid>>
<std::string::String as std::convert::From<Cow<'a, str>>>
<std::string::String as std::convert::From<url::Url>>
<std::string::String as std::convert::From<&str>>
<std::string::String as std::convert::From<&mut str>>
<std::string::String as std::convert::From<&std::string::String>> = note: required for
`Result<std::string::String, std::string::String>` to implement
`FromResidual<Result<Infallible, azure_core::error::Error>>`
Some errors have detailed explanations: E0277, E0425. For more
information about an error, try `rustc --explain E0277`.
该错误是由您在函数上声明的返回类型引起的:
Result<String, String>
。该类型表示:
String
,并且String
。第一个是有道理的,因为
result
变量的类型是 String
,所以 Ok(result)
适合声明的返回类型。然而,编译器抱怨这一行:
let authorization_token = AuthorizationToken::primary_key(&primary_key)?;
?
运算符是用于检查操作是否失败的语法糖,如果失败,则立即从出现该错误的当前函数返回Err
。为了方便起见,它还将尝试基于 From
实现来转换错误。
最后一部分不起作用,正如编译器所指出的:
the trait `std::convert::From<azure_core::error::Error>` is not
implemented for `std::string::String`
primary_key
方法返回一个azure_core::Result<AuthorizationToken>
,它是Result<AuthorizationToken, azure_core::error::Error>
的类型别名,但是azure_core::error::Error
不能转换为String
。
可能的解决方案:
azure_core::Result<String>
。这样 ?
运算符就会起作用,因为错误类型已经匹配。但是,您的界面会发生变化,因此调用站点也需要适应。unwrap
结果而不是 ?
。这通常是不好的,因为您正在向程序中引入潜在的恐慌(崩溃)来源,但如果您试图简单地对某些东西进行原型设计,那么这是最快的更改。