【问题标题】:Why building with rustc command cannot see crates?为什么用 rustc 命令构建时看不到 crate?
【发布时间】:2020-05-24 14:00:39
【问题描述】:

您好,我正在尝试探索 Rust。我想要做的是使用 glob 模块,当我使用 $cargo build 和 $cargo run 构建代码时,它会成功构建并运行可执行文件,但是如果我用 $rustc main.rs 尝试它,它会给我

error[E0432]: unresolved import `glob`
 --> src/main.rs:1:5
  |
1 | use glob::glob;
  |     ^^^^ use of undeclared type or module `glob`

有什么想法吗?

版本:╰─ rustc --version rustc 1.43.1 (8d69840ab 2020-05-04)

代码在这里:

use glob::glob;

fn main() {
    for entry in glob("/home/user/*.jpg").unwrap(){
        match entry {
            Ok(path) => println!("{:?}", path.display()),
            Err(e) => println!("{:?}",e),
        }

    };
}

我的手机

[package]
name = "test1"
version = "0.1.0"
authors = ["ieuD <example@mail.com>"]
edition = "2018"

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

[dependencies]
glob = "0.3.0"

【问题讨论】:

  • 您必须告诉 rustc 在哪里可以找到外部 crate(您已经构建)以及可以找到该 crate 依赖项的库路径。你的 rustc 命令行是什么?
  • @JussiKukkonen 我的命令是直接 rustc src/main.rs。为澄清起见,我已将 glob 依赖项添加到 toml 文件中。 glob crate 并用货物建造。我如何指定板条箱路径。你能给我举个例子吗?
  • 您向 Cargo.toml 文件添加了一个依赖项,该文件是 Cargo 的配置文件...如果您想查看 rustc 的工作示例调用,运行“cargo clean && cargo -v build”来查看 cargo 运行的命令。 doc.rust-lang.org/rustc 列出命令行选项。

标签: rust


【解决方案1】:

rustc 本身不处理你的依赖,它只是编译东西。当您在文件上运行rustc 时,它将开始编译它并遇到未知的glob crate。依赖关系是handled by cargo 通过Cargo.toml

虽然您只能使用rustc(请参阅答案here),但这是一项相当困难的任务,这就是cargo 存在的原因。

【讨论】:

  • 感谢您的详细回答。并对可能的重复问题表示抱歉:))
猜你喜欢
  • 2021-12-26
  • 1970-01-01
  • 2022-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-29
  • 2019-10-15
相关资源
最近更新 更多