我是 Rust 和 Typed 语言的新手,在 Rust 中使用 reqwest 发出帖子请求时遇到了麻烦。我不确定应该将哪些参数传递给 Result 返回类型枚举。
我也不确定到目前为止我的方法是否正确,因为我在调用.await;
after时会出错send
。这个函数需要异步吗?
error[E0308]: mismatched types
--> rust/src/lib.rs:41:13
|
41 | Ok(res) => res,
| ^^^^^^^ expected opaque type, found enum `std::result::Result`
|
::: home/.cargo/registry/src/reqwest-0.11.3/src/async_impl/request.rs:416:26
|
416 | pub fn send(self) -> impl Future<Output = Result<Response, crate::Error>> {
| ---------------------------------------------------- the expected opaque type
|
= note: expected opaque type `impl std::future::Future`
found enum `std::result::Result<_, _>`
error[E0308]: mismatched types
--> rust/src/lib.rs:42:13
|
42 | Err(err) => err,
| ^^^^^^^^ expected opaque type, found enum `std::result::Result`
|
::: /home/.cargo/registry/src/reqwest-0.11.3/src/async_impl/request.rs:416:26
|
416 | pub fn send(self) -> impl Future<Output = Result<Response, crate::Error>> {
| ---------------------------------------------------- the expected opaque type
|
= note: expected opaque type `impl std::future::Future`
found enum `std::result::Result<_, _>`
error: aborting due to 2 previous errors
最终,我不确定使用 reqwest 发出 POST 请求是否走在正确的轨道上,但这就是我所拥有的:
enum Result<T, E> {
Ok(T),
Err(E),
}
pub async fn make_request(path: &str) -> Result<(), ()>{
let client = reqwest::Client::new();
let request = client.post(format!("http://localhost:8000/{}", path))
.body("tbd")
.send();
match request {
Ok(res) => res,
Err(err) => err,
}
}
任何有关前进的建议将不胜感激。