找到你要的答案

Q:How to use a local unpublished crate?

Q:如何使用本地未发布的机箱?

I've made a library:

cargo new my_lib

and I want to use that library in a different program:

cargo new my_program --bin
extern crate my_lib;

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

what do I need to do to get this to work?

They aren't in the same project folder.

.
├── my_lib
└── my_program

Hopefully this makes sense.

I thought I'd be able to override the path as per the Cargo guide, but it states

You cannot use this feature to tell Cargo how to find local unpublished crates.

This is when using the latest stable version of Rust (1.3).

我做了一个图书馆:

cargo new my_lib

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

cargo new my_program --bin
extern crate my_lib;

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

我需要做什么才能让这个工作起来?

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

.
├── my_lib
└── my_program

希望这是有意义的。

我想我可以按照货物指南重写路径,但它声明

你不能使用此功能来告诉货物如何找到当地的未发表的板条箱。

这是当使用最新的稳定版本的铁锈(1.3)。

answer1: 回答1:

Add a dependency section to your executable's Cargo.toml and specify the path:

[dependencies.my_lib]
path = "../my_lib"

or the equivalent alternate TOML:

[dependencies]
my_lib = { path = "../my_lib" }

Check out the Cargo docs for specifying dependencies for more detail, like how to use a git repository instead of a local path.

添加一个依赖部分可执行文件的Cargo.toml和指定路径:

[dependencies.my_lib]
path = "../my_lib"

或等效替代toml:

[dependencies]
my_lib = { path = "../my_lib" }

查看指定更详细的依赖的货运文件,如如何使用Git仓库而不是本地路径。

rust  multifile  rust-crates