Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
919 views
in Technique[技术] by (71.8m points)

rust - How to use a local unpublished crate?

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).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...