我正在尝试创建一个Rust模块,然后从另一个文件中使用它。这是我的文件结构:
这是 main.rs 文件:
这是lib.rs文件:
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:
[package]
name = "bitter-oyster"
version = "0.1.0"
authors = ["matthias"]
[dependencies]
这是 main.rs 文件:
extern crate plot;
fn main() {
println!("----");
plot::line::test();
}
这是lib.rs文件:
mod plot;
这是 plot/mod.rs 文件。
mod line;
这是plot/line.rs
(关于IT技术的内容)pub fn test(){
println!("Here line");
}
当我尝试使用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;
我该如何编译我的程序?从在线文档来看,这应该是可行的,但实际上并不起作用。
PY03
时需要cdylib
,为了解决这个问题,我设置了crate-type = ["cdylib", "lib"]
,这意味着该库既可以用于Python编译,也可以在其他Rust库中使用。 - sam