0

我是新手,我正在尝试使用人造丝从一系列 URL 中获取响应

use rayon::prelude::*;
use reqwest::*;

fn main() -> Result<()> {
    let urls = vec!["https://example.com"];
    urls.par_iter().for_each(|url: &&str| ->  Result<()> {
        println!("Hello... {:?}", url);
        let resp = reqwest::blocking::get(url)?.text()?;
        println!("{:#?}", resp);
        Ok(())
    });
    Ok(())
}

但我收到了这个错误

error[E0271]: type mismatch resolving `<[closure@src\main.rs:252:30: 257:6] as FnOnce<(&&str,)>>::Output == ()`
   --> src\main.rs:252:21
    |
252 |     urls.par_iter().for_each(|url| ->  Result<()> {
    |                     ^^^^^^^^ expected enum `std::result::Result`, found `()`
    |
    = note:   expected enum `std::result::Result<(), reqwest::Error>`
            found unit type `()`

error[E0277]: the trait bound `&&str: IntoUrl` is not satisfied
   --> src\main.rs:254:20
    |
254 |         let resp = reqwest::blocking::get(url)?.text()?;
    |                    ^^^^^^^^^^^^^^^^^^^^^^ the trait `IntoUrl` is not implemented for `&&str`
    | 
   ::: mod.rs:106:15
    |
106 | pub fn get<T: crate::IntoUrl>(url: T) -> crate::Result<Response> {
    |               -------------- required by this bound in `reqwest::blocking::get`
    |
    = help: the following implementations were found:
              <&'a str as IntoUrl>
4

1 回答 1

0

for_each取闭包/lambda,其返回类型unit不是Result
?运算unit -> ()
*

src/main.rs

use rayon::prelude::*;

fn main() {
    let urls = vec!["https://example.com"];
    urls.par_iter().for_each(|url: &&str| {
        println!("Hello... {:?}", *url);
        let resp = reqwest::blocking::get(*url).unwrap().text();
        println!("{:#?}", resp);
    });
}

货运.toml

[package]
name = "s71275260"
version = "0.1.0"
edition = "2021"

# see more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
rayon = "1.5.1"
reqwest = { version = "0.11", features = ["blocking"] }
于 2022-02-26T10:23:48.550 回答