首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用特征标志运行cargo

如何使用特征标志运行cargo
EN

Stack Overflow用户
提问于 2020-04-11 23:30:27
回答 1查看 610关注 0票数 0

我试图通过编写命令行界面来学习rust,但我不能做通过功能的cargo run,我不明白为什么。我阅读了文档/堆栈,但我仍然不明白为什么会发生这种情况。感觉它应该是这样工作的https://doc.rust-lang.org/cargo/commands/cargo-run.html

我正在尝试运行这段代码

https://github.com/clap-rs/clap/blob/master/examples/17_yaml.rs

使用命令cargo run --features=yamlcargo run --features yaml。我尝试了许多组合,但都不起作用。

我的Cargo.toml是这样的:

代码语言:javascript
复制
[dependencies.clap]
version = "*"
default-features = false
features = ["yaml"]

当我运行时,我有错误:

代码语言:javascript
复制
:!cargo run --features=yaml
error: Package `fun v0.1.0 (/Users/XXX/Projekty/rust/fun)` does not have these fe
atures: `yaml`

shell returned 101

我做错了什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-12 08:12:52

他们的代码要求您克隆clap存储库,将其更改到其目录中,然后从那里运行cargo run --features yaml --example 17_yamlYou can read more about how the cargo examples feature works here

如果你打算以noted in that example code的身份复制他们的代码,你必须删除这个条件编译属性:

代码语言:javascript
复制
// Note: If you're using clap as a dependency and don't have a feature for your users called
// "yaml", you'll need to remove the #[cfg(feature = "yaml")] conditional compilation attribute
#[cfg(feature = "yaml")]
fn main() {

否则,它将加载this other main implementation并发出该错误:

代码语言:javascript
复制
#[cfg(not(feature = "yaml"))]
fn main() {
    // As stated above, if clap is not compiled with the YAML feature, it is disabled.
    println!("YAML feature is disabled.");
    println!("Pass --features yaml to cargo when trying this example.");
}

您实际上不需要在命令行上传递--features,除非您是在如上所述的机箱内运行它们的示例。如果你正在复制他们的代码,你也应该删除整个函数!它只在作为示例运行时才相关。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61159287

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档