编译错误:找不到`core`的板条箱

spa*_*rkr 5 rust rust-cargo crate

我正在使用 Rust 1.35.0 来尝试一些 Rust 示例,但我无法编译它,因为我不断收到以下消息:

error[E0463]: can't find crate for `core`
Run Code Online (Sandbox Code Playgroud)

我跑了rustc --explain E0463,我看到以下消息:

You need to link your code to the relevant crate in order to be able to use it
(through Cargo or the `-L` option of rustc example). Plugins are crates as
well, and you link to them the same way.
Run Code Online (Sandbox Code Playgroud)

这是我的 Cargo.toml:

[package]
name = "sensor-node"
version = "0.1.0"
authors = ["joesan <email@gmail.com>"]
edition = "2018"

[dependencies]
dwm1001 = "0.1.0"
panic-halt = "0.2.0"
nb = "0.1.1"
Run Code Online (Sandbox Code Playgroud)

这是我的 main.rs:

fn main() {
    let s = String::from("hello");  // s comes into scope

    takes_ownership(s);             // s's value moves into the function...
                                    // ... and so is no longer valid here

    let x = 5;                      // x comes into scope

    makes_copy(x);                  // x would move into the function,
                                    // but i32 is Copy, so it’s okay to still
                                    // use x afterward

} // Here, x goes out of scope, then s. But because s's value was moved, nothing
  // special happens.

fn takes_ownership(some_string: String) { // some_string comes into scope
    println!("{}", some_string);
} // Here, some_string goes out of scope and `drop` is called. The backing
  // memory is freed.

fn makes_copy(some_integer: i32) { // some_integer comes into scope
    println!("{}", some_integer);
} // Here, some_integer goes out of scope. Nothing special happens.
Run Code Online (Sandbox Code Playgroud)

Nae*_*Nae 7

我在我的案例中解决了这个问题:

rustup target add wasm32-unknown-unknown
Run Code Online (Sandbox Code Playgroud)


was*_*mup 4

您的代码在 Rust Playground上运行良好,因此我建议检查您的 Rust 安装和环境设置。

\n\n
\n\n

您可能想要使用预配置的 Rust Docker映像来运行您的应用程序。安装了 Docker,然后:

\n\n
docker pull rust\n
Run Code Online (Sandbox Code Playgroud)\n\n

转到您的项目文件夹并运行:

\n\n
docker run --rm --user "$(id -u)":"$(id -g)" -v "$PWD":/usr/src/myapp -w /usr/src/myapp rust cargo run\n
Run Code Online (Sandbox Code Playgroud)\n\n

输出:

\n\n
hello\n5\n
Run Code Online (Sandbox Code Playgroud)\n\n
\n\n

对于 PC 上的简单示例,您不需要任何以下依赖项:

\n\n
[dependencies]\ndwm1001 = "0.1.0"\npanic-halt = "0.2.0"\nnb = "0.1.1"\n
Run Code Online (Sandbox Code Playgroud)\n\n

以下是我在 Linux 上测试示例的步骤:

\n\n
[dependencies]\ndwm1001 = "0.1.0"\npanic-halt = "0.2.0"\nnb = "0.1.1"\n
Run Code Online (Sandbox Code Playgroud)\n\n

打开main.rs并粘贴示例main.rs并保存:

\n\n
cargo new hello\ncd hello\ncode .\n
Run Code Online (Sandbox Code Playgroud)\n\n

在文件夹内的终端中hello,运行:

\n\n
fn main() {\n    let s = String::from("hello"); // s comes into scope\n\n    takes_ownership(s); // s\'s value moves into the function...\n                        // ... and so is no longer valid here\n\n    let x = 5; // x comes into scope\n\n    makes_copy(x); // x would move into the function,\n                   // but i32 is Copy, so it\xe2\x80\x99s okay to still\n                   // use x afterward\n} // Here, x goes out of scope, then s. But because s\'s value was moved, nothing\n  // special happens.\n\nfn takes_ownership(some_string: String) {\n    // some_string comes into scope\n    println!("{}", some_string);\n} // Here, some_string goes out of scope and `drop` is called. The backing\n  // memory is freed.\n\nfn makes_copy(some_integer: i32) {\n    // some_integer comes into scope\n    println!("{}", some_integer);\n} // Here, some_integer goes out of scope. Nothing special happens.\n
Run Code Online (Sandbox Code Playgroud)\n\n

并且输出效果很好:

\n\n
cargo run\n
Run Code Online (Sandbox Code Playgroud)\n\n
\n\n

这可能会有所帮助:

\n\n
    \n
  1. 外壳命令

    \n\n
    hello\n5\n
    Run Code Online (Sandbox Code Playgroud)\n\n

    输出:

    \n\n
    rustup component list --installed\n
    Run Code Online (Sandbox Code Playgroud)
  2. \n
  3. 外壳命令:

    \n\n
    cargo-x86_64-unknown-linux-gnu\nclippy-x86_64-unknown-linux-gnu\nrls-x86_64-unknown-linux-gnu\nrust-analysis-x86_64-unknown-linux-gnu\nrust-docs-x86_64-unknown-linux-gnu\nrust-src\nrust-std-x86_64-unknown-linux-gnu\nrustc-x86_64-unknown-linux-gnu\nrustfmt-x86_64-unknown-linux-gnu\n
    Run Code Online (Sandbox Code Playgroud)\n\n

    输出:

    \n\n
    rustup show\n
    Run Code Online (Sandbox Code Playgroud)
  4. \n
\n