简体   繁体   English

如何使用不在 crates.io 上的库?

[英]How can I use a library that is not on crates.io?

I want to use this library: https://github.com/stepfunc/dnp3 , but it is not on crates.io, it only has a repository and I can't implement it.我想使用这个库: https : //github.com/stepfunc/dnp3 ,但它不在 crates.io 上,它只有一个存储库,我无法实现它。 I tried to add it to my Cargo.toml like [dependencies] dnp3 = "0.9.1" but it says that it does not exist, and indeed it does not have a crate.我试图将它添加到我的Cargo.toml[dependencies] dnp3 = "0.9.1"但它说它不存在,而且确实它没有板条箱。 Inside the repository, it has some examples in dnp3/example that have use dnp3;在存储库中,它在dnp3/example中有一些use dnp3; as if it were a crate.好像它是一个板条箱。

How can I use this?我怎样才能使用它?

You can directly specify a Github (or any other git repository) as the source of the dependency.您可以直接指定 Github(或任何其他 git 存储库)作为依赖项的来源。

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

See the Cargo reference: https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#specifying-dependencies-from-git-repositories请参阅货物参考: https : //doc.rust-lang.org/cargo/reference/specifying-dependencies.html#specifying-dependencies-from-git-repositories

You can specify dependencies as Git repositories.您可以将依赖项指定为 Git 存储库。

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

If you want to specify a branch (assuming you do not want to use main / master ), you can add a branch key to the declaration above:如果你想指定一个分支(假设你不想使用main / master ),你可以在上面的声明中添加一个branch键:

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

Related read: Specifying dependencies from git repositories相关阅读: 从 git 存储库指定依赖项

Another way to do it would be to clone the repository and use a dependency with a local path.另一种方法是克隆存储库并使用具有本地路径的依赖项。

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

Related rust docs相关的 Rust 文档

But of course as other answers mentioned using the git version is better in your case.但当然,正如提到的使用 git 版本的其他答案在您的情况下更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM