Rust无法找到箱子

时间:2015-12-25 15:15:43

标签: rust rust-crates

我试图在Rust中创建一个模块,然后从另一个文件中使用它。这是我的文件结构:

TemplateSyntaxError at /
Could not parse the remainder: '['cookie_name']' from 'request.COOKIES.['cookie_name']'

这是Cargo.toml:

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

这是主要的:

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

[dependencies]

这是lib.rs:

extern crate plot;

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

这是plot / mod.rs

mod plot;

这是plot / line.rs

mod line;

当我尝试使用:pub fn test(){ println!("Here line"); } 编译我的程序时,我得到:

cargo run

如何编译我的程序?据我可以从在线文档中看出这应该有效,但它没有。

3 个答案:

答案 0 :(得分:16)

您有以下问题:

  1. 您必须在extern crate bitter_oyster;中使用main.rs,因为生成的二进制文件使用您的包,二进制文件不是它的一部分。

  2. 此外,请在bitter_oyster::plot::line::test();中拨打main.rs,而不是plot::line::test();plotbitter_oyster包中的模块,例如line。您指的是test函数及其完全限定名称。

  3. 确保每个模块都以完全限定名称导出。您可以使用pub关键字公开模块,例如pub mod plot;

  4. 您可以在此处找到有关Rust模块系统的更多信息:https://doc.rust-lang.org/book/crates-and-modules.html

    模块结构的工作副本如下:

    的src / main.rs:

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

    的src / lib.rs:

    pub mod plot;
    

    的src /情节/ mod.rs:

    pub mod line;
    

    src / plot / line.rs:

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

答案 1 :(得分:10)

如果您看到此错误:

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

可能是您没有将所需的包添加到Cargo.toml中的依赖项列表中:

[dependencies]
PACKAGE = "1.2.3"

请参阅specifying dependencies in the Cargo docs

答案 2 :(得分:7)

要添加到给定的答案,编译为cdylibdocs)的库可能会在您尝试在另一个项目中引用它时生成此错误。我通过分离我希望在常规lib项目中重用的代码来解决它。

相关问题