首页 > 解决方案 > 在 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。无论如何要导入具有特定别名的包吗?我正在考虑创建一些可以自动执行此类检查的东西。

标签: rustrust-cratestoml

解决方案


您需要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" }

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


推荐阅读