Sophie

Sophie

distrib > Mageia > 6 > x86_64 > media > core-updates > by-pkgid > 878cdd00a13d17a73c6619a777ef5d74 > files > 2514

rust-doc-1.19.0-1.mga6.x86_64.rpm

<!DOCTYPE HTML>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Attributes - The Rust Reference</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="viewport" content="width=device-width, initial-scale=1">

        <base href="">

        <link rel="stylesheet" href="book.css">
        <link href="https://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800" rel="stylesheet" type="text/css">
        <link href="https://fonts.googleapis.com/css?family=Source+Code+Pro:500" rel="stylesheet" type="text/css">

        <link rel="shortcut icon" href="favicon.png">

        <!-- Font Awesome -->
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">

        <link rel="stylesheet" href="highlight.css">
        <link rel="stylesheet" href="tomorrow-night.css">

        <!-- MathJax -->
        <script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>

        <!-- Fetch JQuery from CDN but have a local fallback -->
        <script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
        <script>
            if (typeof jQuery == 'undefined') {
                document.write(unescape("%3Cscript src='jquery.js'%3E%3C/script%3E"));
            }
        </script>
    </head>
    <body class="light">
        <!-- Set the theme before any content is loaded, prevents flash -->
        <script type="text/javascript">
            var theme = localStorage.getItem('theme');
            if (theme == null) { theme = 'light'; }
            $('body').removeClass().addClass(theme);
        </script>

        <!-- Hide / unhide sidebar before it is displayed -->
        <script type="text/javascript">
            var sidebar = localStorage.getItem('sidebar');
            if (sidebar === "hidden") { $("html").addClass("sidebar-hidden") }
            else if (sidebar === "visible") { $("html").addClass("sidebar-visible") }
        </script>

        <div id="sidebar" class="sidebar">
            <ul class="chapter"><li class="affix"><a href="introduction.html">Introduction</a></li><li><a href="notation.html"><strong>1.</strong> Notation</a></li><li><ul class="section"><li><a href="unicode-productions.html"><strong>1.1.</strong> Unicode productions</a></li><li><a href="string-table-productions.html"><strong>1.2.</strong> String table productions</a></li></ul></li><li><a href="lexical-structure.html"><strong>2.</strong> Lexical structure</a></li><li><ul class="section"><li><a href="input-format.html"><strong>2.1.</strong> Input format</a></li><li><a href="identifiers.html"><strong>2.2.</strong> Identifiers</a></li><li><a href="comments.html"><strong>2.3.</strong> Comments</a></li><li><a href="whitespace.html"><strong>2.4.</strong> Whitespace</a></li><li><a href="tokens.html"><strong>2.5.</strong> Tokens</a></li><li><a href="paths.html"><strong>2.6.</strong> Paths</a></li></ul></li><li><a href="macros.html"><strong>3.</strong> Macros</a></li><li><ul class="section"><li><a href="macros-by-example.html"><strong>3.1.</strong> Macros By Example</a></li><li><a href="procedural-macros.html"><strong>3.2.</strong> Procedural Macros</a></li></ul></li><li><a href="crates-and-source-files.html"><strong>4.</strong> Crates and source files</a></li><li><a href="items-and-attributes.html"><strong>5.</strong> Items and attributes</a></li><li><ul class="section"><li><a href="items.html"><strong>5.1.</strong> Items</a></li><li><a href="visibility-and-privacy.html"><strong>5.2.</strong> Visibility and Privacy</a></li><li><a href="attributes.html" class="active"><strong>5.3.</strong> Attributes</a></li></ul></li><li><a href="statements-and-expressions.html"><strong>6.</strong> Statements and expressions</a></li><li><ul class="section"><li><a href="statements.html"><strong>6.1.</strong> Statements</a></li><li><a href="expressions.html"><strong>6.2.</strong> Expressions</a></li></ul></li><li><a href="type-system.html"><strong>7.</strong> Type system</a></li><li><ul class="section"><li><a href="types.html"><strong>7.1.</strong> Types</a></li><li><a href="subtyping.html"><strong>7.2.</strong> Subtyping</a></li><li><a href="type-coercions.html"><strong>7.3.</strong> Type coercions</a></li></ul></li><li><a href="special-traits.html"><strong>8.</strong> Special traits</a></li><li><ul class="section"><li><a href="the-copy-trait.html"><strong>8.1.</strong> The Copy trait</a></li><li><a href="the-sized-trait.html"><strong>8.2.</strong> The Sized trait</a></li><li><a href="the-drop-trait.html"><strong>8.3.</strong> The Drop trait</a></li><li><a href="the-deref-trait.html"><strong>8.4.</strong> The Deref trait</a></li><li><a href="the-send-trait.html"><strong>8.5.</strong> The Send trait</a></li><li><a href="the-sync-trait.html"><strong>8.6.</strong> The Sync trait</a></li></ul></li><li><a href="memory-model.html"><strong>9.</strong> Memory model</a></li><li><ul class="section"><li><a href="memory-allocation-and-lifetime.html"><strong>9.1.</strong> Memory allocation and lifetime</a></li><li><a href="memory-ownership.html"><strong>9.2.</strong> Memory ownership</a></li><li><a href="variables.html"><strong>9.3.</strong> Variables</a></li></ul></li><li><a href="linkage.html"><strong>10.</strong> Linkage</a></li><li><a href="unsafety.html"><strong>11.</strong> Unsafety</a></li><li><ul class="section"><li><a href="unsafe-functions.html"><strong>11.1.</strong> Unsafe functions</a></li><li><a href="unsafe-blocks.html"><strong>11.2.</strong> Unsafe blocks</a></li><li><a href="behavior-considered-undefined.html"><strong>11.3.</strong> Behavior considered undefined</a></li><li><a href="behavior-not-considered-unsafe.html"><strong>11.4.</strong> Behavior not considered unsafe</a></li></ul></li><li><a href="influences.html">Appendix: Influences</a></li><li class="affix"><a href="undocumented.html">Appendix: As-yet-undocumented Features</a></li></ul>
        </div>

        <div id="page-wrapper" class="page-wrapper">

            <div class="page">
                <div id="menu-bar" class="menu-bar">
                    <div class="left-buttons">
                        <i id="sidebar-toggle" class="fa fa-bars"></i>
                        <i id="theme-toggle" class="fa fa-paint-brush"></i>
                    </div>

                    <h1 class="menu-title">The Rust Reference</h1>

                    <div class="right-buttons">
                        <i id="print-button" class="fa fa-print" title="Print this book"></i>
                    </div>
                </div>

                <div id="content" class="content">
                    <a class="header" href="attributes.html#attributes" id="attributes"><h1>Attributes</h1></a>
<p>Any item declaration may have an <em>attribute</em> applied to it. Attributes in Rust
are modeled on Attributes in ECMA-335, with the syntax coming from ECMA-334
(C#). An attribute is a general, free-form metadatum that is interpreted
according to name, convention, and language and compiler version. Attributes
may appear as any of:</p>
<ul>
<li>A single identifier, the attribute name</li>
<li>An identifier followed by the equals sign '=' and a literal, providing a
key/value pair</li>
<li>An identifier followed by a parenthesized list of sub-attribute arguments</li>
</ul>
<p>Attributes with a bang (&quot;!&quot;) after the hash (&quot;#&quot;) apply to the item that the
attribute is declared within. Attributes that do not have a bang after the hash
apply to the item that follows the attribute.</p>
<p>An example of attributes:</p>
<pre><pre class="playpen"><code class="language-rust"># #![allow(unused_variables)]
#fn main() {
// General metadata applied to the enclosing module or crate.
#![crate_type = &quot;lib&quot;]

// A function marked as a unit test
#[test]
fn test_foo() {
    /* ... */
}

// A conditionally-compiled module
#[cfg(target_os=&quot;linux&quot;)]
mod bar {
    /* ... */
}

// A lint attribute used to suppress a warning/error
#[allow(non_camel_case_types)]
type int8_t = i8;

#}</code></pre></pre>
<blockquote>
<p><strong>Note:</strong> At some point in the future, the compiler will distinguish between
language-reserved and user-available attributes. Until then, there is
effectively no difference between an attribute handled by a loadable syntax
extension and the compiler.</p>
</blockquote>
<a class="header" href="attributes.html#crate-only-attributes" id="crate-only-attributes"><h2>Crate-only attributes</h2></a>
<ul>
<li><code>crate_name</code> - specify the crate's crate name.</li>
<li><code>crate_type</code> - see <a href="linkage.html">linkage</a>.</li>
<li><code>feature</code> - see <a href="attributes.html#compiler-features">compiler features</a>.</li>
<li><code>no_builtins</code> - disable optimizing certain code patterns to invocations of
library functions that are assumed to exist</li>
<li><code>no_main</code> - disable emitting the <code>main</code> symbol. Useful when some other
object being linked to defines <code>main</code>.</li>
<li><code>no_start</code> - disable linking to the <code>native</code> crate, which specifies the
&quot;start&quot; language item.</li>
<li><code>no_std</code> - disable linking to the <code>std</code> crate.</li>
<li><code>plugin</code> - load a list of named crates as compiler plugins, e.g.
<code>#![plugin(foo, bar)]</code>. Optional arguments for each plugin,
i.e. <code>#![plugin(foo(... args ...))]</code>, are provided to the plugin's
registrar function.  The <code>plugin</code> feature gate is required to use
this attribute.</li>
<li><code>recursion_limit</code> - Sets the maximum depth for potentially
infinitely-recursive compile-time operations like
auto-dereference or macro expansion. The default is
<code>#![recursion_limit=&quot;64&quot;]</code>.</li>
<li><code>windows_subsystem</code> - Indicates that when this crate is linked for a Windows
target it will configure the resulting binary's
<a href="https://msdn.microsoft.com/en-us/library/fcc1zstk.aspx">subsystem</a> via the linker. Valid values for this
attribute are <code>console</code> and <code>windows</code>, corresponding to
those two respective subsystems. More subsystems may be
allowed in the future, and this attribute is ignored on
non-Windows targets.</li>
</ul>
<a class="header" href="attributes.html#module-only-attributes" id="module-only-attributes"><h3>Module-only attributes</h3></a>
<ul>
<li><code>no_implicit_prelude</code> - disable injecting <code>use std::prelude::*</code> in this
module.</li>
<li><code>path</code> - specifies the file to load the module from. <code>#[path=&quot;foo.rs&quot;] mod bar;</code> is equivalent to <code>mod bar { /* contents of foo.rs */ }</code>. The path is
taken relative to the directory that the current module is in.</li>
</ul>
<a class="header" href="attributes.html#function-only-attributes" id="function-only-attributes"><h2>Function-only attributes</h2></a>
<ul>
<li><code>main</code> - indicates that this function should be passed to the entry point,
rather than the function in the crate root named <code>main</code>.</li>
<li><code>plugin_registrar</code> - mark this function as the registration point for
[compiler plugins][plugin], such as loadable syntax extensions.</li>
<li><code>start</code> - indicates that this function should be used as the entry point,
overriding the &quot;start&quot; language item. See the &quot;start&quot; <a href="attributes.html#language-items">language
item</a> for more details.</li>
<li><code>test</code> - indicates that this function is a test function, to only be compiled
in case of <code>--test</code>.
<ul>
<li><code>ignore</code> - indicates that this test function is disabled.</li>
</ul>
</li>
<li><code>should_panic</code> - indicates that this test function should panic, inverting the success condition.</li>
<li><code>cold</code> - The function is unlikely to be executed, so optimize it (and calls
to it) differently.</li>
<li><code>naked</code> - The function utilizes a custom ABI or custom inline ASM that requires
epilogue and prologue to be skipped.</li>
</ul>
<a class="header" href="attributes.html#static-only-attributes" id="static-only-attributes"><h2>Static-only attributes</h2></a>
<ul>
<li><code>thread_local</code> - on a <code>static mut</code>, this signals that the value of this
static may change depending on the current thread. The exact consequences of
this are implementation-defined.</li>
</ul>
<a class="header" href="attributes.html#ffi-attributes" id="ffi-attributes"><h2>FFI attributes</h2></a>
<p>On an <code>extern</code> block, the following attributes are interpreted:</p>
<ul>
<li><code>link_args</code> - specify arguments to the linker, rather than just the library
name and type. This is feature gated and the exact behavior is
implementation-defined (due to variety of linker invocation syntax).</li>
<li><code>link</code> - indicate that a native library should be linked to for the
declarations in this block to be linked correctly. <code>link</code> supports an optional
<code>kind</code> key with three possible values: <code>dylib</code>, <code>static</code>, and <code>framework</code>. See
<a href="items.html#external-blocks">external blocks</a> for more about external blocks. Two
examples: <code>#[link(name = &quot;readline&quot;)]</code> and
<code>#[link(name = &quot;CoreFoundation&quot;, kind = &quot;framework&quot;)]</code>.</li>
<li><code>linked_from</code> - indicates what native library this block of FFI items is
coming from. This attribute is of the form <code>#[linked_from = &quot;foo&quot;]</code> where
<code>foo</code> is the name of a library in either <code>#[link]</code> or a <code>-l</code> flag. This
attribute is currently required to export symbols from a Rust dynamic library
on Windows, and it is feature gated behind the <code>linked_from</code> feature.</li>
</ul>
<p>On declarations inside an <code>extern</code> block, the following attributes are
interpreted:</p>
<ul>
<li><code>link_name</code> - the name of the symbol that this function or static should be
imported as.</li>
<li><code>linkage</code> - on a static, this specifies the <a href="http://llvm.org/docs/LangRef.html#linkage-types">linkage
type</a>.</li>
</ul>
<p>On <code>enum</code>s:</p>
<ul>
<li><code>repr</code> - on C-like enums, this sets the underlying type used for
representation. Takes one argument, which is the primitive
type this enum should be represented for, or <code>C</code>, which specifies that it
should be the default <code>enum</code> size of the C ABI for that platform. Note that
enum representation in C is undefined, and this may be incorrect when the C
code is compiled with certain flags.</li>
</ul>
<p>On <code>struct</code>s:</p>
<ul>
<li><code>repr</code> - specifies the representation to use for this struct. Takes a list
of options. The currently accepted ones are <code>C</code> and <code>packed</code>, which may be
combined. <code>C</code> will use a C ABI compatible struct layout, and <code>packed</code> will
remove any padding between fields (note that this is very fragile and may
break platforms which require aligned access).</li>
</ul>
<a class="header" href="attributes.html#macro-related-attributes" id="macro-related-attributes"><h2>Macro-related attributes</h2></a>
<ul>
<li>
<p><code>macro_use</code> on a <code>mod</code> — macros defined in this module will be visible in the
module's parent, after this module has been included.</p>
</li>
<li>
<p><code>macro_use</code> on an <code>extern crate</code> — load macros from this crate.  An optional
list of names <code>#[macro_use(foo, bar)]</code> restricts the import to just those
macros named.  The <code>extern crate</code> must appear at the crate root, not inside
<code>mod</code>, which ensures proper function of the <a href="../book/first-edition/macros.html#the-variable-crate"><code>$crate</code> macro
variable</a>.</p>
</li>
<li>
<p><code>macro_reexport</code> on an <code>extern crate</code> — re-export the named macros.</p>
</li>
<li>
<p><code>macro_export</code> - export a macro for cross-crate usage.</p>
</li>
<li>
<p><code>no_link</code> on an <code>extern crate</code> — even if we load this crate for macros, don't
link it into the output.</p>
</li>
</ul>
<p>See the <a href="../book/first-edition/macros.html#scoping-and-macro-importexport">macros section of the
book</a> for more information on
macro scope.</p>
<a class="header" href="attributes.html#miscellaneous-attributes" id="miscellaneous-attributes"><h2>Miscellaneous attributes</h2></a>
<ul>
<li><code>deprecated</code> - mark the item as deprecated; the full attribute is
<code>#[deprecated(since = &quot;crate version&quot;, note = &quot;...&quot;)</code>, where both arguments
are optional.</li>
<li><code>export_name</code> - on statics and functions, this determines the name of the
exported symbol.</li>
<li><code>link_section</code> - on statics and functions, this specifies the section of the
object file that this item's contents will be placed into.</li>
<li><code>no_mangle</code> - on any item, do not apply the standard name mangling. Set the
symbol for this item to its identifier.</li>
<li><code>simd</code> - on certain tuple structs, derive the arithmetic operators, which
lower to the target's SIMD instructions, if any; the <code>simd</code> feature gate
is necessary to use this attribute.</li>
<li><code>unsafe_destructor_blind_to_params</code> - on <code>Drop::drop</code> method, asserts that the
destructor code (and all potential specializations of that code) will
never attempt to read from nor write to any references with lifetimes
that come in via generic parameters. This is a constraint we cannot
currently express via the type system, and therefore we rely on the
programmer to assert that it holds. Adding this to a Drop impl causes
the associated destructor to be considered &quot;uninteresting&quot; by the
Drop-Check rule, and thus it can help sidestep data ordering
constraints that would otherwise be introduced by the Drop-Check
rule. Such sidestepping of the constraints, if done incorrectly, can
lead to undefined behavior (in the form of reading or writing to data
outside of its dynamic extent), and thus this attribute has the word
&quot;unsafe&quot; in its name. To use this, the
<code>unsafe_destructor_blind_to_params</code> feature gate must be enabled.</li>
<li><code>doc</code> - Doc comments such as <code>/// foo</code> are equivalent to <code>#[doc = &quot;foo&quot;]</code>.</li>
<li><code>rustc_on_unimplemented</code> - Write a custom note to be shown along with the error
when the trait is found to be unimplemented on a type.
You may use format arguments like <code>{T}</code>, <code>{A}</code> to correspond to the
types at the point of use corresponding to the type parameters of the
trait of the same name. <code>{Self}</code> will be replaced with the type that is supposed
to implement the trait but doesn't. To use this, the <code>on_unimplemented</code> feature gate
must be enabled.</li>
<li><code>must_use</code> - on structs and enums, will warn if a value of this type isn't used or
assigned to a variable. You may also include an optional message by using
<code>#[must_use = &quot;message&quot;]</code> which will be given alongside the warning.</li>
</ul>
<a class="header" href="attributes.html#conditional-compilation" id="conditional-compilation"><h3>Conditional compilation</h3></a>
<p>Sometimes one wants to have different compiler outputs from the same code,
depending on build target, such as targeted operating system, or to enable
release builds.</p>
<p>Configuration options are boolean (on or off) and are named either with a
single identifier (e.g. <code>foo</code>) or an identifier and a string (e.g. <code>foo = &quot;bar&quot;</code>;
the quotes are required and spaces around the <code>=</code> are unimportant). Note that
similarly-named options, such as <code>foo</code>, <code>foo=&quot;bar&quot;</code> and <code>foo=&quot;baz&quot;</code> may each be set
or unset independently.</p>
<p>Configuration options are either provided by the compiler or passed in on the
command line using <code>--cfg</code> (e.g. <code>rustc main.rs --cfg foo --cfg 'bar=&quot;baz&quot;'</code>).
Rust code then checks for their presence using the <code>#[cfg(...)]</code> attribute:</p>
<pre><pre class="playpen"><code class="language-rust"># #![allow(unused_variables)]
#fn main() {
// The function is only included in the build when compiling for macOS
#[cfg(target_os = &quot;macos&quot;)]
fn macos_only() {
  // ...
}

// This function is only included when either foo or bar is defined
#[cfg(any(foo, bar))]
fn needs_foo_or_bar() {
  // ...
}

// This function is only included when compiling for a unixish OS with a 32-bit
// architecture
#[cfg(all(unix, target_pointer_width = &quot;32&quot;))]
fn on_32bit_unix() {
  // ...
}

// This function is only included when foo is not defined
#[cfg(not(foo))]
fn needs_not_foo() {
  // ...
}

#}</code></pre></pre>
<p>This illustrates some conditional compilation can be achieved using the
<code>#[cfg(...)]</code> attribute. <code>any</code>, <code>all</code> and <code>not</code> can be used to assemble
arbitrarily complex configurations through nesting.</p>
<p>The following configurations must be defined by the implementation:</p>
<ul>
<li><code>target_arch = &quot;...&quot;</code> - Target CPU architecture, such as <code>&quot;x86&quot;</code>,
<code>&quot;x86_64&quot;</code> <code>&quot;mips&quot;</code>, <code>&quot;powerpc&quot;</code>, <code>&quot;powerpc64&quot;</code>, <code>&quot;arm&quot;</code>, or
<code>&quot;aarch64&quot;</code>. This value is closely related to the first element of
the platform target triple, though it is not identical.</li>
<li><code>target_os = &quot;...&quot;</code> - Operating system of the target, examples
include <code>&quot;windows&quot;</code>, <code>&quot;macos&quot;</code>, <code>&quot;ios&quot;</code>, <code>&quot;linux&quot;</code>, <code>&quot;android&quot;</code>,
<code>&quot;freebsd&quot;</code>, <code>&quot;dragonfly&quot;</code>, <code>&quot;bitrig&quot;</code> , <code>&quot;openbsd&quot;</code> or
<code>&quot;netbsd&quot;</code>. This value is closely related to the second and third
element of the platform target triple, though it is not identical.</li>
<li><code>target_family = &quot;...&quot;</code> - Operating system family of the target, e. g.
<code>&quot;unix&quot;</code> or <code>&quot;windows&quot;</code>. The value of this configuration option is defined
as a configuration itself, like <code>unix</code> or <code>windows</code>.</li>
<li><code>unix</code> - See <code>target_family</code>.</li>
<li><code>windows</code> - See <code>target_family</code>.</li>
<li><code>target_env = &quot;..&quot;</code> - Further disambiguates the target platform with
information about the ABI/libc. Presently this value is either
<code>&quot;gnu&quot;</code>, <code>&quot;msvc&quot;</code>, <code>&quot;musl&quot;</code>, or the empty string. For historical
reasons this value has only been defined as non-empty when needed
for disambiguation. Thus on many GNU platforms this value will be
empty. This value is closely related to the fourth element of the
platform target triple, though it is not identical. For example,
embedded ABIs such as <code>gnueabihf</code> will simply define <code>target_env</code> as
<code>&quot;gnu&quot;</code>.</li>
<li><code>target_endian = &quot;...&quot;</code> - Endianness of the target CPU, either <code>&quot;little&quot;</code> or
<code>&quot;big&quot;</code>.</li>
<li><code>target_pointer_width = &quot;...&quot;</code> - Target pointer width in bits. This is set
to <code>&quot;32&quot;</code> for targets with 32-bit pointers, and likewise set to <code>&quot;64&quot;</code> for
64-bit pointers.</li>
<li><code>target_has_atomic = &quot;...&quot;</code> - Set of integer sizes on which the target can perform
atomic operations. Values are <code>&quot;8&quot;</code>, <code>&quot;16&quot;</code>, <code>&quot;32&quot;</code>, <code>&quot;64&quot;</code> and <code>&quot;ptr&quot;</code>.</li>
<li><code>target_vendor = &quot;...&quot;</code> - Vendor of the target, for example <code>apple</code>, <code>pc</code>, or
simply <code>&quot;unknown&quot;</code>.</li>
<li><code>test</code> - Enabled when compiling the test harness (using the <code>--test</code> flag).</li>
<li><code>debug_assertions</code> - Enabled by default when compiling without optimizations.
This can be used to enable extra debugging code in development but not in
production.  For example, it controls the behavior of the standard library's
<code>debug_assert!</code> macro.</li>
</ul>
<p>You can also set another attribute based on a <code>cfg</code> variable with <code>cfg_attr</code>:</p>
<pre><code class="language-rust ignore">#[cfg_attr(a, b)]
</code></pre>
<p>This is the same as <code>#[b]</code> if <code>a</code> is set by <code>cfg</code>, and nothing otherwise.</p>
<p>Lastly, configuration options can be used in expressions by invoking the <code>cfg!</code>
macro: <code>cfg!(a)</code> evaluates to <code>true</code> if <code>a</code> is set, and <code>false</code> otherwise.</p>
<a class="header" href="attributes.html#lint-check-attributes" id="lint-check-attributes"><h3>Lint check attributes</h3></a>
<p>A lint check names a potentially undesirable coding pattern, such as
unreachable code or omitted documentation, for the static entity to which the
attribute applies.</p>
<p>For any lint check <code>C</code>:</p>
<ul>
<li><code>allow(C)</code> overrides the check for <code>C</code> so that violations will go
unreported,</li>
<li><code>deny(C)</code> signals an error after encountering a violation of <code>C</code>,</li>
<li><code>forbid(C)</code> is the same as <code>deny(C)</code>, but also forbids changing the lint
level afterwards,</li>
<li><code>warn(C)</code> warns about violations of <code>C</code> but continues compilation.</li>
</ul>
<p>The lint checks supported by the compiler can be found via <code>rustc -W help</code>,
along with their default settings.  <a href="../unstable-book/language-features/plugin.html#lint-plugins">Compiler
plugins</a> can provide additional lint checks.</p>
<pre><code class="language-rust ignore">pub mod m1 {
    // Missing documentation is ignored here
    #[allow(missing_docs)]
    pub fn undocumented_one() -&gt; i32 { 1 }

    // Missing documentation signals a warning here
    #[warn(missing_docs)]
    pub fn undocumented_too() -&gt; i32 { 2 }

    // Missing documentation signals an error here
    #[deny(missing_docs)]
    pub fn undocumented_end() -&gt; i32 { 3 }
}
</code></pre>
<p>This example shows how one can use <code>allow</code> and <code>warn</code> to toggle a particular
check on and off:</p>
<pre><pre class="playpen"><code class="language-rust"># #![allow(unused_variables)]
#fn main() {
#[warn(missing_docs)]
pub mod m2{
    #[allow(missing_docs)]
    pub mod nested {
        // Missing documentation is ignored here
        pub fn undocumented_one() -&gt; i32 { 1 }

        // Missing documentation signals a warning here,
        // despite the allow above.
        #[warn(missing_docs)]
        pub fn undocumented_two() -&gt; i32 { 2 }
    }

    // Missing documentation signals a warning here
    pub fn undocumented_too() -&gt; i32 { 3 }
}

#}</code></pre></pre>
<p>This example shows how one can use <code>forbid</code> to disallow uses of <code>allow</code> for
that lint check:</p>
<pre><code class="language-rust ignore">#[forbid(missing_docs)]
pub mod m3 {
    // Attempting to toggle warning signals an error here
    #[allow(missing_docs)]
    /// Returns 2.
    pub fn undocumented_too() -&gt; i32 { 2 }
}
</code></pre>
<a class="header" href="attributes.html#language-items" id="language-items"><h3>Language items</h3></a>
<p>Some primitive Rust operations are defined in Rust code, rather than being
implemented directly in C or assembly language. The definitions of these
operations have to be easy for the compiler to find. The <code>lang</code> attribute
makes it possible to declare these operations. For example, the <code>str</code> module
in the Rust standard library defines the string equality function:</p>
<pre><code class="language-rust ignore">#[lang = &quot;str_eq&quot;]
pub fn eq_slice(a: &amp;str, b: &amp;str) -&gt; bool {
    // details elided
}
</code></pre>
<p>The name <code>str_eq</code> has a special meaning to the Rust compiler, and the presence
of this definition means that it will use this definition when generating calls
to the string equality function.</p>
<p>The set of language items is currently considered unstable. A complete
list of the built-in language items will be added in the future.</p>
<a class="header" href="attributes.html#inline-attributes" id="inline-attributes"><h3>Inline attributes</h3></a>
<p>The inline attribute suggests that the compiler should place a copy of
the function or static in the caller, rather than generating code to
call the function or access the static where it is defined.</p>
<p>The compiler automatically inlines functions based on internal heuristics.
Incorrectly inlining functions can actually make the program slower, so it
should be used with care.</p>
<p><code>#[inline]</code> and <code>#[inline(always)]</code> always cause the function to be serialized
into the crate metadata to allow cross-crate inlining.</p>
<p>There are three different types of inline attributes:</p>
<ul>
<li><code>#[inline]</code> hints the compiler to perform an inline expansion.</li>
<li><code>#[inline(always)]</code> asks the compiler to always perform an inline expansion.</li>
<li><code>#[inline(never)]</code> asks the compiler to never perform an inline expansion.</li>
</ul>
<a class="header" href="attributes.html#derive" id="derive"><h3><code>derive</code></h3></a>
<p>The <code>derive</code> attribute allows certain traits to be automatically implemented
for data structures. For example, the following will create an <code>impl</code> for the
<code>PartialEq</code> and <code>Clone</code> traits for <code>Foo</code>, the type parameter <code>T</code> will be given
the <code>PartialEq</code> or <code>Clone</code> constraints for the appropriate <code>impl</code>:</p>
<pre><pre class="playpen"><code class="language-rust"># #![allow(unused_variables)]
#fn main() {
#[derive(PartialEq, Clone)]
struct Foo&lt;T&gt; {
    a: i32,
    b: T,
}

#}</code></pre></pre>
<p>The generated <code>impl</code> for <code>PartialEq</code> is equivalent to</p>
<pre><pre class="playpen"><code class="language-rust"># #![allow(unused_variables)]
#fn main() {
# struct Foo&lt;T&gt; { a: i32, b: T }
impl&lt;T: PartialEq&gt; PartialEq for Foo&lt;T&gt; {
    fn eq(&amp;self, other: &amp;Foo&lt;T&gt;) -&gt; bool {
        self.a == other.a &amp;&amp; self.b == other.b
    }

    fn ne(&amp;self, other: &amp;Foo&lt;T&gt;) -&gt; bool {
        self.a != other.a || self.b != other.b
    }
}

#}</code></pre></pre>
<p>You can implement <code>derive</code> for your own type through <a href="procedural-macros.html">procedural
macros</a>.</p>
<a class="header" href="attributes.html#compiler-features" id="compiler-features"><h3>Compiler Features</h3></a>
<p>Certain aspects of Rust may be implemented in the compiler, but they're not
necessarily ready for every-day use. These features are often of &quot;prototype
quality&quot; or &quot;almost production ready&quot;, but may not be stable enough to be
considered a full-fledged language feature.</p>
<p>For this reason, Rust recognizes a special crate-level attribute of the form:</p>
<pre><code class="language-rust ignore">#![feature(feature1, feature2, feature3)]
</code></pre>
<p>This directive informs the compiler that the feature list: <code>feature1</code>,
<code>feature2</code>, and <code>feature3</code> should all be enabled. This is only recognized at a
crate-level, not at a module-level. Without this directive, all features are
considered off, and using the features will result in a compiler error.</p>
<p>The currently implemented features of the reference compiler are documented in
<a href="https://doc.rust-lang.org/nightly/unstable-book/">The Unstable Book</a>.</p>
<p>If a feature is promoted to a language feature, then all existing programs will
start to receive compilation warnings about <code>#![feature]</code> directives which enabled
the new feature (because the directive is no longer necessary). However, if a
feature is decided to be removed from the language, errors will be issued (if
there isn't a parser error first). The directive in this case is no longer
necessary, and it's likely that existing code will break if the feature isn't
removed.</p>
<p>If an unknown feature is found in a directive, it results in a compiler error.
An unknown feature is one which has never been recognized by the compiler.</p>

                </div>

                <!-- Mobile navigation buttons -->
                
                    <a href="visibility-and-privacy.html" class="mobile-nav-chapters previous">
                        <i class="fa fa-angle-left"></i>
                    </a>
                

                
                    <a href="statements-and-expressions.html" class="mobile-nav-chapters next">
                        <i class="fa fa-angle-right"></i>
                    </a>
                

            </div>

            
                <a href="visibility-and-privacy.html" class="nav-chapters previous" title="You can navigate through the chapters using the arrow keys">
                    <i class="fa fa-angle-left"></i>
                </a>
            

            
                <a href="statements-and-expressions.html" class="nav-chapters next" title="You can navigate through the chapters using the arrow keys">
                    <i class="fa fa-angle-right"></i>
                </a>
            

        </div>


        <!-- Local fallback for Font Awesome -->
        <script>
            if ($(".fa").css("font-family") !== "FontAwesome") {
                $('<link rel="stylesheet" type="text/css" href="_FontAwesome/css/font-awesome.css">').prependTo('head');
            }
        </script>

        <!-- Livereload script (if served using the cli tool) -->
        

        <script src="highlight.js"></script>
        <script src="book.js"></script>
    </body>
</html>