Rust 找不到 crate [英] Rust can't find crate

查看:75
本文介绍了Rust 找不到 crate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Rust 中创建一个模块,然后从不同的文件中使用它.这是我的文件结构:

I'm trying to create a module in Rust and then use it from a different file. 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

这是 Cargo.toml:

This is Cargo.toml:

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

[dependencies]

这是 main.rs:

This is main.rs:

extern crate plot;

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

这是 lib.rs:

mod plot;

这是 plot/mod.rs

this is plot/mod.rs

mod line;

这是 plot/line.rs

and this is plot/line.rs

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

当我尝试使用以下命令编译我的程序时:cargo run 我得到:

When I try to compile my program using: cargo run I get:

   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.

推荐答案

您遇到以下问题:

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

  1. you have to use extern crate bitter_oyster; in main.rs, because the produced binary uses your crate, the binary is not a part of it.

另外,在 main.rs 中调用 bitter_oyster::plot::line::test(); 而不是 plot::line::test();.plotbitter_oyster crate 中的一个模块,例如 line.您指的是具有完全限定名称的 test 函数.

Also, call bitter_oyster::plot::line::test(); in main.rs instead of plot::line::test();. plot is a module in the bitter_oyster crate, such as line. You are referring to the test function with its fully qualified name.

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

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;

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

You can find more information about Rust's module system here: 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");
}

这篇关于Rust 找不到 crate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆