如何使用本地未发布的箱子?

时间:2015-10-08 21:01:22

标签: rust multifile rust-crates

我做了一个图书馆:

cargo new my_lib

我想在另一个程序中使用该库:

cargo new my_program --bin
extern crate my_lib;

fn main {
    println!("Hello, World!");
}

我需要做些什么才能让它发挥作用?

它们不在同一个项目文件夹中。

.
├── my_lib
└── my_program

希望这是有道理的。

我以为我可以根据Cargo guide覆盖路径,但是它说明了

  

您不能使用此功能告诉Cargo如何找到本地未发布的板条箱。

这是使用最新稳定版本的Rust(1.3)。

1 个答案:

答案 0 :(得分:0)

我正在寻找与mvn install等效的内容。尽管此问题与我的原始问题并不完全相同,但是任何偶然发现我的原始问题并点击此处链接的人都将找到更完整的答案。

答案是“没有等效于mvn install的原因是,您必须在Cargo.toml文件中对路径进行硬编码,这在其他人的计算机上可能是错误的,但是您可以接近它。”

现有的答案有点简短,我不得不花一些时间才能使事情真正起作用,所以这里有更多详细信息:

/usr/bin/cargo run --color=always --package re5 --bin re5
   Compiling re5 v0.1.0 (file:///home/thoth/art/2019/radial-embroidery/re5)
error[E0432]: unresolved import `embroidery_stitcher`
 --> re5/src/main.rs:5:5
  |
5 | use embroidery_stitcher;
  |     ^^^^^^^^^^^^^^^^^^^ no `embroidery_stitcher` in the root

rustc --explain E0432包含此段,以呼应Shepmaster的回答:

  

或者,如果您尝试使用外部包装箱中的模块,则可能错过了   extern crate声明(通常放在板条箱根中):

extern crate core; // Required to use the `core` crate

use core::any;

use切换到extern crate得到了这个:

/usr/bin/cargo run --color=always --package re5 --bin re5
   Compiling embroidery_stitcher v0.1.0 (file:///home/thoth/art/2019/radial-embroidery/embroidery_stitcher)
warning: function is never used: `svg_header`
 --> embroidery_stitcher/src/lib.rs:2:1
  |
2 | fn svg_header(w: i32, h: i32) -> String
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: #[warn(dead_code)] on by default

   Compiling re5 v0.1.0 (file:///home/thoth/art/2019/radial-embroidery/re5)
error[E0603]: function `svg_header` is private
 --> re5/src/main.rs:8:19
  |
8 |     let mut svg = embroidery_stitcher::svg_header(100,100);
  |                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

我不得不在该函数的前面打一个pub

pub fn svg_header(w: i32, h: i32) -> String

现在可以了。