我正在使用 Rust 1.35.0 来尝试一些 Rust 示例,但我无法编译它,因为我不断收到以下消息:
error[E0463]: can't find crate for `core`
我跑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.
这是我的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"
这是我的 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.
你的代码在 Rust 操场上运行良好,所以我建议检查一下你的 Rust 安装和环境设置。
你可能希望使用预配置的 Rust Docker 镜像来运行你的应用。安装 Docker 后:
docker pull rust
转到项目文件夹并运行:
docker run --rm --user "$(id -u)":"$(id -g)" -v "$PWD":/usr/src/myapp -w /usr/src/myapp rust cargo run
输出:
hello
5
对于电脑上的简单示例,不需要以下任何依赖项:
[dependencies]
dwm1001 = "0.1.0"
panic-halt = "0.2.0"
nb = "0.1.1"
以下是我在 Linux 上测试示例的步骤:
cargo new hello
cd hello
code .
打开main.rs
并粘贴示例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.
在 hello
文件夹内的终端中,运行:
cargo run
而且输出很好:
hello
5
这可能有助于:
命令行管理程序命令
rustup component list --installed
输出:
cargo-x86_64-unknown-linux-gnu clippy-x86_64-unknown-linux-gnu rls-x86_64-unknown-linux-gnu rust-analysis-x86_64-unknown-linux-gnu rust-docs-x86_64-unknown-linux-gnu rust-src rust-std-x86_64-unknown-linux-gnu rustc-x86_64-unknown-linux-gnu rustfmt-x86_64-unknown-linux-gnu
外壳命令:
rustup show
输出:
Default host: x86_64-unknown-linux-gnu installed toolchains -------------------- stable-x86_64-unknown-linux-gnu (default) nightly-x86_64-unknown-linux-gnu active toolchain ---------------- stable-x86_64-unknown-linux-gnu (default) rustc 1.35.0 (3c235d560 2019-05-20)
我用以下方法解决了这个问题:
rustup target add wasm32-unknown-unknown
首次安装工具链时,rustup 仅安装主机平台的标准库 - 即您当前运行的架构和操作系统。要编译到其他平台,您必须安装其他目标平台。这是通过 rustup 目标添加命令完成的。例如,要添加安卓目标,请执行以下操作:
见鲁斯塔普书 -交叉编译
例如:
$ rustup target add aarch64-unknown-linux-gnu
info: downloading component 'rust-std' for 'aarch64-unknown-linux-gnu'
info: installing component 'rust-std' for 'aarch64-unknown-linux-gnu'
要在目标机器上获取目标字符串,如果安装了 rust,则可以运行:
rustc -vV
例如输出
rustc 1.67.1 (d5a82bbd2 2023-02-07)
binary: rustc
commit-hash: d5a82bbd26e1ad8b7401f6a718a9c57c96905483
commit-date: 2023-02-07
host: aarch64-unknown-linux-gnu
release: 1.67.1
LLVM version: 15.0.6
host
字段可用作参数以rustup target add
注意:您可能会遇到链接器问题,在这种情况下,您可以配置链接器,请参阅货物簿 - 配置 - 目标