我对rust使用库的语法非常陌生。好吧,总体来说,大部分都是新生锈的。
我已经包含一个未完成的图书馆,看来还行不通。 library is called "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
小时``
use hours::Hours;
fn main() {
//... abbreviated
let hours = Hours::from(example_hrs, Amsterdam).unwrap();
}
给出编译错误: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
小时in
小时``
是否有任何方法可以包含和使用此库?我需要更改库,还是我只是在错误使用它?
这里的问题是,您共享的存储库链接中的代码与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.io同步。
确保您也可以从本地存储库缓存中检查板条箱的来源。
## 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
为什么crates.io中的箱子的存储库链接与箱子的源不匹配
请检查发布板条箱reference,repository
信息在软件包的metadata
(在cargo.toml
中指定。
根据包元数据reference,这些信息用于:
这些URL指向有关软件包的更多信息。这些是旨在成为相关数据的网页视图,不一定兼容使用VCS工具等。
documentation = "..." homepage = "..." repository = "..."
您也可能会检查受欢迎的包装箱,它们指向通常指向主分支的github(通常)主页,而不是当前版本的标签。