Rust reqwest 示例 json 代码未编译
Rust reqwest example json code does not compile
example for dynamic json with reqwest
extern crate reqwest;
extern crate tokio;
extern crate serde_json; // added to after upgrade to 1.39.0
#[tokio::main]
async fn main() -> Result<(), reqwest::Error> {
let echo_json: serde_json::Value = reqwest::Client::new()
.post("https://jsonplaceholder.typicode.com/posts")
.json(&serde_json::json!({
"title": "Reqwest.rs",
"body": "https://docs.rs/reqwest",
"userId": 1
}))
.send()
.await?
.json()
.await?;
println!("{:#?}", echo_json);
// Object(
// {
// "body": String(
// "https://docs.rs/reqwest"
// ),
// "id": Number(
// 101
// ),
// "title": String(
// "Reqwest.rs"
// ),
// "userId": Number(
// 1
// )
// }
// )
Ok(())
}
使用 reqwest = "0.9.22"
和 tokio = "0.2.2"
:
为 rustc 1.38.0
编译失败
Compiling pin-project-lite v0.1.1
error: `core::slice::<impl [T]>::len` is not yet stable as a const fn
--> /Users/mick/.cargo/registry/src/github.com-1ecc6299db9ec823/bytes-0.5.2/src/bytes.rs:121:18
|
121 | len: bytes.len(),
| ^^^^^^^^^^^
error: aborting due to previous error
error: Could not compile `bytes`.
根据 Ömer 下面的评论,此问题已通过升级解决,但现在下一个问题是
error[E0433]: failed to resolve: could not find `main` in `tokio`
--> main.rs:5:10
|
5 | #[tokio::main]
| ^^^^ could not find `main` in `tokio`
error[E0277]: the trait bound `std::result::Result<reqwest::Response, reqwest::Error>: std::future::Future` is not satisfied
--> main.rs:7:40
|
7 | let echo_json: serde_json::Value = reqwest::Client::new()
| ________________________________________^
8 | | .post("https://jsonplaceholder.typicode.com/posts")
9 | | .json(&serde_json::json!({
10 | | "title": "Reqwest.rs",
... |
14 | | .send()
15 | | .await?
| |______________^ the trait `std::future::Future` is not implemented for `std::result::Result<reqwest::Response, reqwest::Error>`
error[E0277]: `main` has invalid return type `impl std::future::Future`
--> main.rs:6:20
|
6 | async fn main() -> Result<(), reqwest::Error> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ `main` can only return types that implement `std::process::Termination`
|
= help: consider using `()`, or a `Result`
error: aborting due to 3 previous errors
Some errors have detailed explanations: E0277, E0433.
For more information about an error, try `rustc --explain E0277`.
error: could not compile `webtrekk`.
To learn more, run the command again with --verbose.
和下一个reqwest = "0.10.0-alpha.2" tokio = { version = "0.2.2", features = ["macros"] } serde_json = "1.0.44"
➜ rust git:(master) ✗ cargo run
Updating crates.io index
error: failed to select a version for `tokio`.
... required by package `reqwest v0.10.0-alpha.2`
... which is depended on by `webtrekk v0.1.0 (/Users/mick/repo/webtrekk-client/rust)`
versions that meet the requirements `= 0.2.0-alpha.6` are: 0.2.0-alpha.6
all possible versions conflict with previously selected packages.
previously selected package `tokio v0.2.2`
... which is depended on by `webtrekk v0.1.0 (/Users/mick/repo/webtrekk-client/rust)`
failed to select a version for `tokio` which could resolve this conflict
如何解决这些问题?
您的示例来自 https://github.com/seanmonstar/reqwest/blob/master/examples/json_dynamic.rs 并适用于以下 Cargo.toml
,使用 Rust 1.39:
[package]
name = "reqwest-json_dynamic-example"
version = "0.1.0"
authors = ["fyaa"]
edition = "2018"
[dependencies]
tokio = { version = "=0.2.0-alpha.6", features = ["io", "tcp", "timer"] }
reqwest = { version = "0.10.0-alpha.2", features = ["json"] }
serde_json = "1.0.44"
example for dynamic json with reqwest
extern crate reqwest;
extern crate tokio;
extern crate serde_json; // added to after upgrade to 1.39.0
#[tokio::main]
async fn main() -> Result<(), reqwest::Error> {
let echo_json: serde_json::Value = reqwest::Client::new()
.post("https://jsonplaceholder.typicode.com/posts")
.json(&serde_json::json!({
"title": "Reqwest.rs",
"body": "https://docs.rs/reqwest",
"userId": 1
}))
.send()
.await?
.json()
.await?;
println!("{:#?}", echo_json);
// Object(
// {
// "body": String(
// "https://docs.rs/reqwest"
// ),
// "id": Number(
// 101
// ),
// "title": String(
// "Reqwest.rs"
// ),
// "userId": Number(
// 1
// )
// }
// )
Ok(())
}
使用 reqwest = "0.9.22"
和 tokio = "0.2.2"
:
1.38.0
编译失败
Compiling pin-project-lite v0.1.1 error: `core::slice::<impl [T]>::len` is not yet stable as a const fn --> /Users/mick/.cargo/registry/src/github.com-1ecc6299db9ec823/bytes-0.5.2/src/bytes.rs:121:18 | 121 | len: bytes.len(), | ^^^^^^^^^^^ error: aborting due to previous error error: Could not compile `bytes`.
根据 Ömer 下面的评论,此问题已通过升级解决,但现在下一个问题是
error[E0433]: failed to resolve: could not find `main` in `tokio`
--> main.rs:5:10
|
5 | #[tokio::main]
| ^^^^ could not find `main` in `tokio`
error[E0277]: the trait bound `std::result::Result<reqwest::Response, reqwest::Error>: std::future::Future` is not satisfied
--> main.rs:7:40
|
7 | let echo_json: serde_json::Value = reqwest::Client::new()
| ________________________________________^
8 | | .post("https://jsonplaceholder.typicode.com/posts")
9 | | .json(&serde_json::json!({
10 | | "title": "Reqwest.rs",
... |
14 | | .send()
15 | | .await?
| |______________^ the trait `std::future::Future` is not implemented for `std::result::Result<reqwest::Response, reqwest::Error>`
error[E0277]: `main` has invalid return type `impl std::future::Future`
--> main.rs:6:20
|
6 | async fn main() -> Result<(), reqwest::Error> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ `main` can only return types that implement `std::process::Termination`
|
= help: consider using `()`, or a `Result`
error: aborting due to 3 previous errors
Some errors have detailed explanations: E0277, E0433.
For more information about an error, try `rustc --explain E0277`.
error: could not compile `webtrekk`.
To learn more, run the command again with --verbose.
和下一个reqwest = "0.10.0-alpha.2" tokio = { version = "0.2.2", features = ["macros"] } serde_json = "1.0.44"
➜ rust git:(master) ✗ cargo run
Updating crates.io index
error: failed to select a version for `tokio`.
... required by package `reqwest v0.10.0-alpha.2`
... which is depended on by `webtrekk v0.1.0 (/Users/mick/repo/webtrekk-client/rust)`
versions that meet the requirements `= 0.2.0-alpha.6` are: 0.2.0-alpha.6
all possible versions conflict with previously selected packages.
previously selected package `tokio v0.2.2`
... which is depended on by `webtrekk v0.1.0 (/Users/mick/repo/webtrekk-client/rust)`
failed to select a version for `tokio` which could resolve this conflict
如何解决这些问题?
您的示例来自 https://github.com/seanmonstar/reqwest/blob/master/examples/json_dynamic.rs 并适用于以下 Cargo.toml
,使用 Rust 1.39:
[package]
name = "reqwest-json_dynamic-example"
version = "0.1.0"
authors = ["fyaa"]
edition = "2018"
[dependencies]
tokio = { version = "=0.2.0-alpha.6", features = ["io", "tcp", "timer"] }
reqwest = { version = "0.10.0-alpha.2", features = ["json"] }
serde_json = "1.0.44"