英文:
Rust, How to configure a workspace of libraries?
问题
I have a workspace lets call my_workspace
. my_workspace
has a series of small libraries related but each one with their own responsibility. Something like:
[workspace]
members = [
"my_lib_1",
"my_lib_2",
"my_lib_3",
...
]
And my_workspace
are structured like this:
|my_lib_1/
|my_lib_2/
|my_lib_2/
|Cargo.toml
...
This workspace doesn't export any binaries only libraries.
The thing how I can configure my Cargo.toml
to treat each members as a feature? to use like this:
# package that use the lib
[dependencies]
my_workspace = { ... , features = ["my_lib_1"] }
// inside of the package that use `my_workspace`
use my_workspace::my_lib_1;
fn do_something() {
my_lib_1::whatever_that_exposes_this_lib();
}
I'm sorry if this is a noob question, but I can't find any information in the cargo documentation that would shed light on how to accomplish this.
英文:
I have a workspace lets call my_workspace
. my_workspace
has a series of small libraries related but each one with their own responsibility. Something like:
[workspace]
members = [
"my_lib_1",
"my_lib_2",
"my_lib_3",
...
]
And my_workspace
are structured like this:
|my_lib_1/
|my_lib_2/
|my_lib_2/
|Cargo.toml
...
This workspace doesn't export any binaries only libraries.
The thing how I can configure my Cargo.toml
to treat each members as a feature? to use like this:
# package that use the lib
[dependencies]
my_workspace = { ... , features = ["my_lib_1"] }
// inside of the package that use `my_workspace`
use my_workspace::my_lib_1;
fn do_something() {
my_lib_1::whatever_that_exposes_this_lib();
}
I'm sorry if this is a noob question, but I can't find any information in the cargo documentation that would shed light on how to accomplish this.
答案1
得分: 0
以下是翻译好的部分:
似乎 Rust 不允许我将工作区设置为 'main' 项目,因此我通过添加一个新的 crate 来解决,该 crate 导入了所有工作区成员,并在 Cargo.toml
中的 features
键以及每个成员的 lib.rs
中的 #[cfg(feature)]
注解中重新导出它们。
英文:
It seems like rust doesn't let me have a workspace as 'main' project, so instead I solved by adding a new crate that import all workspace members and reexport them with the features
key in the Cargo.toml
and the #[cfg(feature)]
annotation in the lib.rs
for each member.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论