在 toml 中导入带有别名的 rust 包

问题描述

我正在尝试制作一个简单的程序来检查同一个 rust 项目的两个不同分支上的执行时间。

我想让我的 .toml 看起来像这样

[dependencies]
cron_original = { git = "https://github.com/zslayton/cron" }
cron_fork = { git = "https://github.com/koenichiwa/cron",branch = "feature/reimplement-queries"}

我的程序看起来像这样:

fn main() {
    let expression = String::from("0-59 * 0-23 ?/2 1,2-4 ? *");
    let schedule_orig = cron_original::Schedule::from_str(expression);
    let schedule_fork = cron_fork::Schedule::from_str(expression);
    // Check difference in execution times on these structs
}

但我收到了 no matching package named 'cron_fork' found。无论如何要导入具有特定别名的包?我正在考虑创建一些可以像这样自动检查的东西。

解决方法

您需要为这些依赖项指定 package 键,这样即使您指定了不同的名称,cargo 也知道您确实想要这些包:

[dependencies]
cron_original = { git = "https://github.com/zslayton/cron",package="cron" }
cron_fork = { git = "https://github.com/koenichiwa/cron",branch = "feature/reimplement-queries",package="cron" }

有关详细信息,请参阅 Specifying Dependencies 文档中的在 Cargo.toml 中重命名依赖项部分。