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

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

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:

  1. [workspace]
  2. members = [
  3. "my_lib_1",
  4. "my_lib_2",
  5. "my_lib_3",
  6. ...
  7. ]

And my_workspace are structured like this:

  1. |my_lib_1/
  2. |my_lib_2/
  3. |my_lib_2/
  4. |Cargo.toml
  5. ...

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:

  1. # package that use the lib
  2. [dependencies]
  3. my_workspace = { ... , features = ["my_lib_1"] }
  1. // inside of the package that use `my_workspace`
  2. use my_workspace::my_lib_1;
  3. fn do_something() {
  4. my_lib_1::whatever_that_exposes_this_lib();
  5. }

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:

  1. [workspace]
  2. members = [
  3. "my_lib_1",
  4. "my_lib_2",
  5. "my_lib_3",
  6. ...
  7. ]

And my_workspace are structured like this:

  1. |my_lib_1/
  2. |my_lib_2/
  3. |my_lib_2/
  4. |Cargo.toml
  5. ...

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:

  1. # package that use the lib
  2. [dependencies]
  3. my_workspace = { ... , features = ["my_lib_1"] }
  1. // inside of the package that use `my_workspace`
  2. use my_workspace::my_lib_1;
  3. fn do_something() {
  4. my_lib_1::whatever_that_exposes_this_lib();
  5. }

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:

确定