What is a crate attribute and where do I add it?

前端 未结 1 1014
孤独总比滥情好
孤独总比滥情好 2020-11-28 10:14

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 onl

相关标签:
1条回答
  • 2020-11-28 11:05

    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.

    0 讨论(0)
提交回复
热议问题