【问题标题】:How can I use a library that is not on crates.io?如何使用不在 crates.io 上的库?
【发布时间】:2021-11-17 20:39:01
【问题描述】:

我想使用这个库:https://github.com/stepfunc/dnp3,但它不在 crates.io 上,它只有一个存储库,我无法实现它。我试图将它添加到我的Cargo.toml 中,就像[dependencies] dnp3 = "0.9.1" 一样,但它说它不存在,而且它确实没有板条箱。在存储库中,它在dnp3/example 中有一些示例,它们具有use dnp3;,就好像它是一个板条箱一样。

我该如何使用它?

【问题讨论】:

    标签: rust rust-cargo crate dnp3


    【解决方案1】:

    您可以直接指定一个 Github(或任何其他 git 存储库)作为依赖的来源。

    [dependencies]
    dnp3 = { git = "https://github.com/stepfunc/dnp3" }
    

    参见货运参考:https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#specifying-dependencies-from-git-repositories

    【讨论】:

    • 谢谢!我马上试试
    • 非常感谢!它工作得很好,从现在开始我会更好地阅读文档
    【解决方案2】:

    您可以将依赖项指定为 Git 存储库。

    [dependencies]
    dnp3 = { git = "https://github.com/stepfunc/dnp3" }
    

    如果你想指定一个分支(假设你不想使用main/master),你可以在上面的声明中添加一个branch键:

    [dependencies]
    dnp3 = { git = "https://github.com/stepfunc/dnp3", branch = "feature/rustls" }
    

    相关阅读:Specifying dependencies from git repositories

    【讨论】:

      【解决方案3】:

      另一种方法是克隆存储库并使用具有本地路径的依赖项。

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

      相关锈docs

      当然,在您的情况下,使用 git 版本提到的其他答案更好。

      【讨论】:

      • 这个也很有用!非常感谢
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-25
      • 1970-01-01
      • 2014-07-10
      • 1970-01-01
      • 1970-01-01
      • 2021-08-28
      • 2021-02-25
      相关资源
      最近更新 更多