简体   繁体   English

Rust 找不到箱子

[英]Rust can't find crate

I'm trying to create a module in Rust and then use it from a different file.我正在尝试在 Rust 中创建一个模块,然后从另一个文件中使用它。 This is my file structure:这是我的文件结构:

matthias@X1:~/projects/bitter-oyster$ tree
.
├── Cargo.lock
├── Cargo.toml
├── Readme.md
├── src
│   ├── liblib.rlib
│   ├── lib.rs
│   ├── main.rs
│   ├── main.rs~
│   └── plot
│       ├── line.rs
│       └── mod.rs
└── target
    └── debug
        ├── bitter_oyster.d
        ├── build
        ├── deps
        ├── examples
        ├── libbitter_oyster.rlib
        └── native

8 directories, 11 files

This is Cargo.toml:这是 Cargo.toml:

[package]
name = "bitter-oyster"
version = "0.1.0"
authors = ["matthias"]

[dependencies]

This is main.rs:这是 main.rs:

extern crate plot;

fn main() {
    println!("----");
    plot::line::test();
}

This is lib.rs:这是 lib.rs:

mod plot;

this is plot/mod.rs这是情节/mod.rs

mod line;

and this is plot/line.rs这是 plot/line.rs

pub fn test(){
    println!("Here line");
}

When I try to compile my program using: cargo run I get:当我尝试使用cargo run编译我的程序时,我得到:

   Compiling bitter-oyster v0.1.0 (file:///home/matthias/projects/bitter-oyster)
/home/matthias/projects/bitter-oyster/src/main.rs:1:1: 1:19 error: can't find crate for `plot` [E0463]
/home/matthias/projects/bitter-oyster/src/main.rs:1 extern crate plot;

How do I compile my program?如何编译我的程序? As far as I can tell from online documentations this should work, but it doesn't.据我从在线文档中可以看出这应该有效,但事实并非如此。

If you see this error:如果您看到此错误:

error[E0463]: can't find crate for `PACKAGE`
  |
1 | extern crate PACKAGE;
  | ^^^^^^^^^^^^^^^^^^^^^ can't find crate

it could be that you haven't added the desired crate to the dependencies list in your Cargo.toml :可能是您没有将所需的 crate 添加到Cargo.toml的依赖项列表中:

[dependencies]
PACKAGE = "1.2.3"

See specifying dependencies in the Cargo docs .请参阅Cargo 文档中的指定依赖项

To add to the given answers, a library compiled as a cdylib ( docs ) can generate this error when you try to reference it in another project.要添加到给定的答案,当您尝试在另一个项目中引用它时,编译为cdylib ( docs ) 的库可能会生成此错误。 I solved it by separating the code I wished to reuse in a regular lib project.我通过分离我希望在常规lib项目中重用的代码来解决它。

You have the following problems:您有以下问题:

  1. you have to use extern crate bitter_oyster;你必须使用extern crate bitter_oyster; in main.rs , because the produced binary uses your crate, the binary is not a part of it.main.rs中,因为生成的二进制文件使用你的 crate,所以二进制文件不是它的一部分。

  2. Also, call bitter_oyster::plot::line::test();另外,调用bitter_oyster::plot::line::test(); in main.rs instead of plot::line::test();main.rs而不是plot::line::test(); . . plot is a module in the bitter_oyster crate, such as line . plotbitter_oyster crate 中的一个模块,例如line You are referring to the test function with its fully qualified name.您指的是带有完全限定名称的test函数。

  3. Make sure, that every module is exported in the fully qualified name.确保每个模块都以完全限定名称导出。 You can make a module public with the pub keyword, like pub mod plot;您可以使用pub关键字将模块公开,例如pub mod plot;

You can find more information about Rust's module system here: https://doc.rust-lang.org/book/crates-and-modules.html您可以在此处找到有关 Rust 模块系统的更多信息: https ://doc.rust-lang.org/book/crates-and-modules.html

A working copy of your module structure is as follows:您的模块结构的工作副本如下:

src/main.rs: src/main.rs:

extern crate bitter_oyster;

fn main() {
    println!("----");
    bitter_oyster::plot::line::test();
}

src/lib.rs: src/lib.rs:

pub mod plot;

src/plot/mod.rs: src/plot/mod.rs:

pub mod line;

src/plot/line.rs : src/plot/line.rs:

pub fn test(){
    println!("Here line");
}

当我在 [dev-dependencies] 而不是 [dependencies] 中导入我的 crate 时,我遇到了这个问题

This can also happen when you don't enable certain "feature flags" for specific crates.当您没有为特定的板条箱启用某些“功能标志”时,也会发生这种情况。 Unfortunately, these feature flags can sometimes be undocumented.不幸的是,这些功能标志有时可能没有记录。 When feature flags are missing when required, they show the same error ("Can't find crate")当需要时缺少功能标志时,它们会显示相同的错误(“找不到箱子”)

I was using diesel , and was trying to use BigInteger :我正在使用diesel ,并试图使用BigInteger

Wrong错误的

diesel = { version = "2.0.3", features = ["postgres", "chrono", "r2d2", "serde_json", "biginteger"] }

Correct:正确的:

diesel = { version = "2.0.3", features = ["postgres", "chrono", "r2d2", "serde_json", "numeric"] }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM