如何配置一个 Rust 库的工作空间?

huangapple go评论77阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年5月25日 13:01:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76329073.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定