什么是 crate 属性,我应该在哪里添加它? [英] What is a crate attribute and where do I add it?

查看:127
本文介绍了什么是 crate 属性,我应该在哪里添加它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了了解 Rust 的工作原理,我决定看一个名为 的基于终端的文本编辑器伊奥塔.我克隆了存储库并运行 cargo build 却被告知:

<块引用>

错误:*if let* 语法是实验性的帮助:将 #![feature(if_let)] 添加到 crate 属性以启用

我应该在哪里将 #![feature(if_let)] 添加到 crate 属性?

解决方案

一个 crate 属性是一个适用于封闭上下文 (#![...]).这个属性必须添加到你的 crate root 的顶部,因此上下文是 crate 本身:

#![attribute_name]#![attribute_name(arg1, ...)]

如果你正在创作

  • 一个库——包根将是一个名为 lib.rs
  • 的文件
  • 一个应用程序 — crate 根将是您构建的主要 .rs 文件.在许多情况下,这将被称为 main.rs
  • 集成测试 - crate 根是 tests/
  • 中的每个文件
  • 一个例子 - crate 根是 examples/
  • 中的每个文件
<小时>

Rust 编程语言Rust Reference 在一般的.The Unstable Book 包含功能标志列表和有关其功能的简要文档.

有许多不同的 crate 属性,但是 feature crate 属性 (#![feature(feature1, feature2)]) 只能用于 编译器的每晚 版本.不允许在稳定的 Rust 版本中使用不稳定的功能.

In order to get a feel for how Rust works, I decided to look at a little terminal-based text editor called Iota. I cloned the repository and ran cargo build only to be told:

error: *if let* syntax is experimental

help: add #![feature(if_let)] to the crate attributes to enable

Where am I supposed to add #![feature(if_let)] to the crate attributes?

解决方案

A crate attribute is an attribute (#[...]) that applies to the enclosing context (#![...]). This attribute must be added to the top of your crate root, thus the context is the crate itself:

#![attribute_name]
#![attribute_name(arg1, ...)]

If you are creating

  • a library — the crate root will be a file called lib.rs
  • an application — the crate root would be the primary .rs file you build. In many cases, this will be called main.rs
  • an integration test - the crate root is each file in tests/
  • an example - the crate root is each file in examples/

The Rust Programming Language and the Rust Reference talk a bit about attributes in general. The Unstable Book contains a list of feature flags and brief documentation on what they do.

There are many different crate attributes, but the feature crate attribute (#![feature(feature1, feature2)]) may only be used in a nightly version of the compiler. Unstable features are not allowed to be used in stable Rust versions.

这篇关于什么是 crate 属性,我应该在哪里添加它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆