Rust Playground 和本地机器中 reqwest 示例的错误

Errors with reqwest example in Rust Playground and local machine

我正在研究 reqwest 库,想用 Rust Playground 尝试一些示例。我从网上获取了一个最简单的示例,但它无法编译。

请按照下面的link点击运行:

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=fb0e7911a018ddb927cb13debd34fff4

这是控制台显示的编译错误:

   Compiling playground v0.0.1 (/playground)
error: linking with `cc` failed: exit code: 1
  |
  = note: "cc" "-Wl,--as-needed" "-Wl,-z,noexecstack" "-m64" "-Wl,--eh-frame-hdr" "-L" "/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "/playground/target/debug/deps/playground-552f473283bcb85e.playground.1fpw7zol-cgu.0.rcgu.o" "-o" "/playground/target/debug/deps/playground-552f473283bcb85e" "/playground/target/debug/deps/playground-552f473283bcb85e.4jwju6fs7xga1d77.rcgu.o" "-Wl,--gc-sections" "-pie" "-Wl,-zrelro" "-Wl,-znow" "-nodefaultlibs" "-L" "/playground/target/debug/deps" "-L" "/playground/target/debug/build/libsqlite3-sys-16200be2df74876c/out" "-L" "/playground/target/debug/build/ring-bb20fb99fa6aa7fb/out" "-L" "/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-Wl,-Bstatic" "/playground/target/debug/deps/libfutures-e81e955c63e30b74.rlib" "/playground/target/debug/deps/libfutures_executor-cfdeba081d488308.rlib" "/playground/target/debug/deps/libreqwest-f9a05f7baf6db60c.rlib" "/playground/target/debug/deps/libcookie_store-140640047f1ead84.rlib" "/playground/target/debug/deps/libpublicsuffix-6ec6805d8d4576ca.rlib" "/playground/target/debug/deps/libregex-690a57b3d88f2f3e.rlib" "/playground/target/debug/deps/libthread_local-8d339f961a8cde82.rlib" "/playground/target/debug/deps/libregex_syntax-4b763771453cb6c9.rlib" "/playground/target/debug/deps/libaho_corasick-a90058961ca40ce7.rlib" "/playground/target/debug/deps/liberror_chain-bfc8e31e68aab259.rlib" "/playground/target/debug/deps/libbacktrace-1f6305b7986130af.rlib" "/playground/target/debug/deps/libobject-8fae803afce7a82f.rlib" "/playground/target/debug/deps/libaddr2line-4cb58e132c263024.rlib" "/playground/target/debug/deps/libobject-600bcc9a4024c890.rlib" "/playground/target/debug/deps/libflate2-7c0dda569caa75f2.rlib" "/playground/target/debug/deps/libminiz_oxide-06fcba964606bf52.rlib" "/playground/target/debug/deps/libadler-cf7f582a580fb2a1.rlib" "/playground/target/debug/deps/libcrc32fast-d8f8d6d441eb6dc8.rlib" "/playground/target/debug/deps/libwasmparser-18775bada4336335.rlib" "/playground/target/debug/deps/libgimli-e397da103aee7023.rlib" "/playground/target/debug/deps/libstable_deref_trait-33402efe82680c8b.rlib" "/playground/target/debug/deps/libfallible_iterator-2abec6be3aaf9504.rlib" "/playground/target/debug/deps/libcpp_demangle-af0e508f637e754e.rlib" "/playground/target/debug/deps/librustc_demangle-b0887c2590d815fd.rlib" "/playground/target/debug/deps/libcookie-212b7aa8adce5158.rlib" "/playground/target/debug/deps/libtime-fd9f56ac09845820.rlib" "/playground/target/debug/deps/libtime_macros-a8c33e90ea2139d7.rlib" "/playground/target/debug/deps/libstandback-f34bd7ec31cc9868.rlib" "/playground/target/debug/deps/libhyper_tls-9848d4b1ee8e7a27.rlib" "/playground/target/debug/deps/libipnet-8e33934d86be4d6e.rlib" "/playground/target/debug/deps/libtokio_native_tls-095fa4f445b9ada7.rlib" "/playground/target/debug/deps/libserde_urlencoded-c61505e8efe59184.rlib" "/playground/target/debug/deps/libencoding_rs-6db646af6aed58d2.rlib" "/playground/target/debug/deps/libserde_json-aaaea4d5f17e7bfd.rlib" "/playground/target/debug/deps/libryu-8177541eadb28761.rlib" "/playground/target/debug/deps/libbase64-263cbd5a5e64ab1a.rlib" "/playground/target/debug/deps/libmime_guess-eb8a1cb9893e4e18.rlib" "/playground/target/debug/deps/libunicase-b4234293fa194e2e.rlib" "/playground/target/debug/deps/libmime-f1104dec2de465c5.rlib" "/playground/target/debug/deps/libnative_tls-7028e32d0e94add2.rlib" "/playground/target/debug/deps/libopenssl_probe-7372e2218175cb8d.rlib" "/playground/target/debug/deps/libopenssl-84d1cdf5218f0b74.rlib" "/playground/target/debug/deps/libopenssl_sys-2038d81ad68cd2ed.rlib" "/playground/target/debug/deps/libforeign_types-37adb5bae539fda7.rlib" "/playground/target/debug/deps/libforeign_types_shared-22f2940fb592036b.rlib" "/playground/target/debug/deps/libbitflags-23cf02f711efe05e.rlib" "/playground/target/debug/deps/libhyper-430dd2c2e9afb7dd.rlib" "/playground/target/debug/deps/libwant-de668461c77b0f6d.rlib" "/playground/target/debug/deps/libtry_lock-30c95cee42568441.rlib" "/playground/target/debug/deps/libhttparse-ebba17a8d7f5b2dd.rlib" "/playground/target/debug/deps/libsocket2-abd46160d6c6c84a.rlib" "/playground/target/debug/deps/libh2-67e8c1f896ffcf25.rlib" "/playground/target/debug/deps/libtracing_futures-bfe252ef9bfa1357.rlib" "/playground/target/debug/deps/libindexmap-e59d655bbe32a039.rlib" "/playground/target/debug/deps/libhashbrown-89e2fd5a7081dfb3.rlib" "/playground/target/debug/deps/libahash-842a5b5404407a6a.rlib" "/playground/target/debug/deps/libconst_random-2c7202c77a6d1341.rlib" "/playground/target/debug/deps/libtokio_util-c597572dfab0a5a3.rlib" "/playground/target/debug/deps/libtower_service-25325533e6471f27.rlib" "/playground/target/debug/deps/libpin_project-85290df7927f46fd.rlib" "/playground/target/debug/deps/libhttpdate-7c9ae15df7de953a.rlib" "/playground/target/debug/deps/libtracing-1e9a893c023cc151.rlib" "/playground/target/debug/deps/libtracing_core-f4d747dfd45a5b1b.rlib" "/playground/target/debug/deps/libtokio-6caecbf8c961c6fc.rlib" "/playground/target/debug/deps/libsignal_hook_registry-464320536f92cd1a.rlib" "/playground/target/debug/deps/libnum_cpus-fa9405d418802dba.rlib" "/playground/target/debug/deps/libmio-8208b1b97ee75dd0.rlib" "/playground/target/debug/deps/libparking_lot-c70fd5290b63ba21.rlib" "/playground/target/debug/deps/libparking_lot_core-bec2d0cef1e036dd.rlib" "/playground/target/debug/deps/libsmallvec-be204749242d57d5.rlib" "/playground/target/debug/deps/liblock_api-9b94798a31b72387.rlib" "/playground/target/debug/deps/libscopeguard-9a7fcdcc3ef0c67a.rlib" "/playground/target/debug/deps/libinstant-801a426885a9fdfa.rlib" "/playground/target/debug/deps/libhttp_body-fa23070edd81973e.rlib" "/playground/target/debug/deps/libfutures_util-b297dd64f98a2e4a.rlib" "/playground/target/debug/deps/libmemchr-49ffcb39de512148.rlib" "/playground/target/debug/deps/libtokio_io-15767d74b5cad8b6.rlib" "/playground/target/debug/deps/libbytes-98612ff3704441d9.rlib" "/playground/target/debug/deps/libiovec-059f69bab14804ea.rlib" "/playground/target/debug/deps/liblibc-0a0510b4037c39f2.rlib" "/playground/target/debug/deps/libbyteorder-630514d63ec14067.rlib" "/playground/target/debug/deps/liblog-1181c8ecdf8fff43.rlib" "/playground/target/debug/deps/libcfg_if-ef24fecc97daf057.rlib" "/playground/target/debug/deps/libfutures-341be2426ac8006a.rlib" "/playground/target/debug/deps/libfutures_io-f642e5411fe1b621.rlib" "/playground/target/debug/deps/libslab-fae38ccb0866d90f.rlib" "/playground/target/debug/deps/libfutures_channel-ca57d40e72b73391.rlib" "/playground/target/debug/deps/libpin_project_lite-3a1c1edfcac5da66.rlib" "/playground/target/debug/deps/libfutures_sink-1e577cd08304b39b.rlib" "/playground/target/debug/deps/libfutures_task-285aea8d738140d9.rlib" "/playground/target/debug/deps/libonce_cell-49c2029294b89ee5.rlib" "/playground/target/debug/deps/libpin_utils-9d73d18d1a913b9e.rlib" "/playground/target/debug/deps/libfutures_core-3c03d3ccebe39c3b.rlib" "/playground/target/debug/deps/liblazy_static-a2d7352f0b2d8328.rlib" "/playground/target/debug/deps/liburl-3738c4378d8afb19.rlib" "/playground/target/debug/deps/libidna-44ceb179b4e434c3.rlib" "/playground/target/debug/deps/libunicode_normalization-7b85e62e0830fcdd.rlib" "/playground/target/debug/deps/libtinyvec-0fd102c28a026123.rlib" "/playground/target/debug/deps/libtinyvec_macros-373a732416c546b7.rlib" "/playground/target/debug/deps/libserde-f598327513f0f87c.rlib" "/playground/target/debug/deps/libunicode_bidi-a18e706d77b75aca.rlib" "/playground/target/debug/deps/libform_urlencoded-4539bb7bd3b31a2c.rlib" "/playground/target/debug/deps/libpercent_encoding-6c71419d296d9169.rlib" "/playground/target/debug/deps/libmatches-396398bf8ad55ae0.rlib" "/playground/target/debug/deps/libhttp-bc59be26651c0476.rlib" "/playground/target/debug/deps/libitoa-97b61b74c453bfb8.rlib" "/playground/target/debug/deps/libbytes-196811ca1432aaea.rlib" "/playground/target/debug/deps/libfnv-f0ba3a8dcbb1e827.rlib" "-Wl,--start-group" "/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-6f77337c1826707d.rlib" "/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libpanic_unwind-e238540f8faf9927.rlib" "/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libminiz_oxide-78f8506dd5004913.rlib" "/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libadler-c0e64497deb085aa.rlib" "/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libobject-217a514dc8d845c6.rlib" "/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libaddr2line-65e4f8df4b8bbd73.rlib" "/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libgimli-fec1b74170245419.rlib" "/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_demangle-e74f867eb2f15322.rlib" "/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libhashbrown-72a374d36f3d0a31.rlib" "/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_alloc-cb9206ed96de54c3.rlib" "/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libunwind-33ee81149d314805.rlib" "/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcfg_if-2e8740599b11634b.rlib" "/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liblibc-241cf59851357bea.rlib" "/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liballoc-edc29aa8c04b480f.rlib" "/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_core-6a141406945fda5a.rlib" "/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcore-2408224b4af46d83.rlib" "-Wl,--end-group" "/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcompiler_builtins-d36087076e1dd756.rlib" "-Wl,-Bdynamic" "-lssl" "-lcrypto" "-lgcc_s" "-lutil" "-lrt" "-lpthread" "-lm" "-ldl" "-lc"
  = note: collect2: fatal error: ld terminated with signal 9 [Killed]
          compilation terminated.
          

error: aborting due to previous error

error: could not compile `playground`

To learn more, run the command again with --verbose.

另一方面,当我在我的 本地机器上尝试同样的代码时 (Windows 10) 它确实可以编译但是它会抛出这个错误:

D:\DANI\rust\projects\httpget>cargo run    Compiling httpget v0.1.0 (D:\DANI\rust\projects\httpget)
    Finished dev [unoptimized + debuginfo] target(s) in 2.80s
     Running `target\debug\httpget.exe` Request: https://httpbin.org/ip thread 'main' panicked at 'not currently running on a Tokio 0.2.x runtime.', C:\Users\dani\.cargo\registry\src\github.com-1ecc6299db9ec823\tokio-0.2.25\src\runtime\handle.rs:118:28 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace         error: process didn't exit successfully: `target\debug\httpget.exe` (exit code: 101)

我检查了 Cargo.toml 中的依赖项:

[dependencies]
error-chain = "0.12.4"
reqwest = { version = "0.10" }
tokio = { version = "1", features = ["full"] }
futures = "0.3"

我认为它与 async/await 运行时有关,但我是 Rust 语言的新手,我在网上找到的关于这个问题的信息对我来说不是很清楚。

如果有人能帮助解决这些问题,我将不胜感激!

提前致谢!

丹妮·P.

reqwest的版本问题。将您的原始版本替换为

reqwest = { version = "0.11" }

在你的 Cargo.toml 中应该在你的本地机器上修复它。

是的,你可以!

Rust Playground 都提供了 reqwest crate 允许绑定端口,所以你至少可以建立本地网络连接。 带上你自己的 HTTP-Server!

如果你想玩 reqwest,这可能就是你所需要的(运行 它在发布模式下,因为在调试模式下更有可能获得连接): https://play.rust-lang.org/?version=stable&mode=release&edition=2021&gist=57b32e98df970013ffb8f83361a0b9c6

use std::io::Read;
use std::{thread};

use std::io::prelude::*;
use std::net::TcpListener;

const PORT: u16 = 18030;

fn main() {
    
    thread::spawn(|| {
        let listener = TcpListener::bind(format!("127.0.0.1:{}", PORT)).unwrap();
    
        for stream in listener.incoming() {
            let mut stream = stream.unwrap();
            let response = "HTTP/1.1 200 OK\r\n\r\nHello World";
            stream.write(response.as_bytes()).unwrap();
            stream.flush().unwrap();
            break;
        }
    });
    
    let mut res = reqwest::blocking::get(format!("http://127.0.0.1:{}", PORT)).expect("Connect");
    let mut body = String::new();
    res.read_to_string(&mut body).expect("Body");

    println!("Status: {}", res.status());
    println!("Body:\n{}", body);
}