[英]The code reference in `crates.io` does not match with the actual crate
我对 rust 使用库的语法很陌生。 嗯,总体来说是新的。
我已经包含了一个未完成的库,并且似乎不起作用。 该库名为“hours” , lib.rs
包含以下内容:
// #[derive(Clone, Debug, PartialEq, Eq)]
pub struct Hours {
pub rules: Vec<types::RuleSequence>,
pub tz: Tz,
}
impl Hours {
pub fn from(s: &str, tz: Tz) -> Result<Self, String> {
//... abbreviated
}
pub fn at(self: &Self, dt: &DateTime<Tz>) -> types::Modifier {
//... abbreviated
}
}
它包含在Cargo.toml
:相关行:
edition = "2018"
[dependencies]
hours = "0.0.1"
我想知道是否可以包含和使用from()
函数,到目前为止,我没有运气。 这是我尝试过的:
use hours;
fn main() {
//... abbreviated
let hours = Hours::from(example_hrs, Amsterdam).unwrap();
}
给出编译错误: Hours::from(example_hrs, Amsterdam).unwrap(); ^^^^^ use of undeclared type or module
Hours::from(example_hrs, Amsterdam).unwrap(); ^^^^^ use of undeclared type or module
hours``
use hours::Hours;
fn main() {
//... abbreviated
let hours = Hours::from(example_hrs, Amsterdam).unwrap();
}
给出编译错误: use hours::Hours; ^^^^^^^^^^^^ no ``Hours`` in the root
use hours::Hours; ^^^^^^^^^^^^ no ``Hours`` in the root
use hours;
fn main() {
//... abbreviated
let hours = hours::Hours::from(example_hrs, Amsterdam).unwrap();
}
给出编译错误: hours::Hours::from(example_hrs, Amsterdam).unwrap(); ^^^^^ could not find
hours::Hours::from(example_hrs, Amsterdam).unwrap(); ^^^^^ could not find
时间in
hours``
有没有办法包含和使用这个库? 我是否需要更改库,还是我只是错误地使用了它?
问题在这里,您共享的存储库链接中的代码与 crates.io 中的依赖项不匹配,自然 Rust 找不到所需的 api 组件。 在这种情况下,板条箱的所有者尚未在 gitlab 中发布代码。
要查看您可以从docs.rs快速检查源。 这是所需依赖项docs.rs/crate/hours/0.0.1/source/的链接。
如果要使用存储库中的当前代码
cargo.toml
指定路径来使用它hours = { git = "https://gitlab.com/alantrick/hours.git", rev="7b7d369796c209db7b61db71aa7396f2ec59f942"}
添加修订号或标签可能会有所帮助,因为 master 分支上的更新可能会破坏兼容性。
为什么 docs.rs 中的这个来源对于 crates.io 是准确的?
请查看docs.rs 中的about部分:
Docs.rs 使用 Rust 编译器的夜间版本自动构建在 crates.io 上发布的 crates 文档
这意味着它与 crates.io 同步。
为确保您还可以从本地存储库缓存中检查 crate 的来源。
## Note that this path is built with default cargo settings
$HOME/.cargo/registry/src/github.com-1ecc6299db9ec823/hours-0.0.1
^^^^^^^^^^^^^^^^^^^^^^^^^^^ github reigstry for crates io
为什么 crate.io 中 crate 的存储库链接与 crate 的源不匹配
请检查发布箱参考, repository
信息在包的metadata
指定(在cargo.toml
)。
根据包元数据参考,这些信息用于:
这些 URL 指向有关包的更多信息。 这些旨在成为相关数据的网络视图,不一定与 VCS 工具等兼容。
documentation = "..." homepage = "..." repository = "..."
您也可以检查流行的 crate,它们指向他们的 github(通常)主页,该主页指向 master 分支,而不是当前版本的标签。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.