Sophie

Sophie

distrib > Mageia > 7 > armv7hl > media > core-release > by-pkgid > 0c2243f8a1696816431e7210e991fa52 > files > 13329

rust-doc-1.35.0-1.mga7.armv7hl.rpm

<!DOCTYPE HTML>
<html lang="en" class="sidebar-visible no-js">
    <head>
        <!-- Book generated using mdBook -->
        <meta charset="UTF-8">
        <title>Diagnostics - 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">
        <meta name="theme-color" content="#ffffff" />

        <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="_FontAwesome/css/font-awesome.css">

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

        <!-- Custom theme stylesheets -->
        
        <link rel="stylesheet" href="src/theme/reference.css">
        

        

    </head>
    <body class="light">
        <!-- Work around some values being stored in localStorage wrapped in quotes -->
        <script type="text/javascript">
            try {
                var theme = localStorage.getItem('mdbook-theme');
                var sidebar = localStorage.getItem('mdbook-sidebar');

                if (theme.startsWith('"') && theme.endsWith('"')) {
                    localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
                }

                if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
                    localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
                }
            } catch (e) { }
        </script>

        <!-- Set the theme before any content is loaded, prevents flash -->
        <script type="text/javascript">
            var theme;
            try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { } 
            if (theme === null || theme === undefined) { theme = 'light'; }
            document.body.className = theme;
            document.querySelector('html').className = theme + ' js';
        </script>

        <!-- Hide / unhide sidebar before it is displayed -->
        <script type="text/javascript">
            var html = document.querySelector('html');
            var sidebar = 'hidden';
            if (document.body.clientWidth >= 1080) {
                try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
                sidebar = sidebar || 'visible';
            }
            html.classList.remove('sidebar-visible');
            html.classList.add("sidebar-" + sidebar);
        </script>

        <nav id="sidebar" class="sidebar" aria-label="Table of contents">
            <ol class="chapter"><li class="affix"><a href="introduction.html">Introduction</a></li><li><a href="notation.html"><strong aria-hidden="true">1.</strong> Notation</a></li><li><a href="lexical-structure.html"><strong aria-hidden="true">2.</strong> Lexical structure</a></li><li><ol class="section"><li><a href="input-format.html"><strong aria-hidden="true">2.1.</strong> Input format</a></li><li><a href="keywords.html"><strong aria-hidden="true">2.2.</strong> Keywords</a></li><li><a href="identifiers.html"><strong aria-hidden="true">2.3.</strong> Identifiers</a></li><li><a href="comments.html"><strong aria-hidden="true">2.4.</strong> Comments</a></li><li><a href="whitespace.html"><strong aria-hidden="true">2.5.</strong> Whitespace</a></li><li><a href="tokens.html"><strong aria-hidden="true">2.6.</strong> Tokens</a></li><li><a href="paths.html"><strong aria-hidden="true">2.7.</strong> Paths</a></li></ol></li><li><a href="macros.html"><strong aria-hidden="true">3.</strong> Macros</a></li><li><ol class="section"><li><a href="macros-by-example.html"><strong aria-hidden="true">3.1.</strong> Macros By Example</a></li><li><a href="procedural-macros.html"><strong aria-hidden="true">3.2.</strong> Procedural Macros</a></li></ol></li><li><a href="crates-and-source-files.html"><strong aria-hidden="true">4.</strong> Crates and source files</a></li><li><a href="conditional-compilation.html"><strong aria-hidden="true">5.</strong> Conditional compilation</a></li><li><a href="items.html"><strong aria-hidden="true">6.</strong> Items</a></li><li><ol class="section"><li><a href="items/modules.html"><strong aria-hidden="true">6.1.</strong> Modules</a></li><li><a href="items/extern-crates.html"><strong aria-hidden="true">6.2.</strong> Extern crates</a></li><li><a href="items/use-declarations.html"><strong aria-hidden="true">6.3.</strong> Use declarations</a></li><li><a href="items/functions.html"><strong aria-hidden="true">6.4.</strong> Functions</a></li><li><a href="items/type-aliases.html"><strong aria-hidden="true">6.5.</strong> Type aliases</a></li><li><a href="items/structs.html"><strong aria-hidden="true">6.6.</strong> Structs</a></li><li><a href="items/enumerations.html"><strong aria-hidden="true">6.7.</strong> Enumerations</a></li><li><a href="items/unions.html"><strong aria-hidden="true">6.8.</strong> Unions</a></li><li><a href="items/constant-items.html"><strong aria-hidden="true">6.9.</strong> Constant items</a></li><li><a href="items/static-items.html"><strong aria-hidden="true">6.10.</strong> Static items</a></li><li><a href="items/traits.html"><strong aria-hidden="true">6.11.</strong> Traits</a></li><li><a href="items/implementations.html"><strong aria-hidden="true">6.12.</strong> Implementations</a></li><li><a href="items/external-blocks.html"><strong aria-hidden="true">6.13.</strong> External blocks</a></li><li><a href="items/generics.html"><strong aria-hidden="true">6.14.</strong> Type and lifetime parameters</a></li><li><a href="items/associated-items.html"><strong aria-hidden="true">6.15.</strong> Associated Items</a></li><li><a href="visibility-and-privacy.html"><strong aria-hidden="true">6.16.</strong> Visibility and Privacy</a></li></ol></li><li><a href="attributes.html"><strong aria-hidden="true">7.</strong> Attributes</a></li><li><ol class="section"><li><a href="attributes/testing.html"><strong aria-hidden="true">7.1.</strong> Testing</a></li><li><a href="attributes/derive.html"><strong aria-hidden="true">7.2.</strong> Derive</a></li><li><a href="attributes/diagnostics.html" class="active"><strong aria-hidden="true">7.3.</strong> Diagnostics</a></li><li><a href="attributes/codegen.html"><strong aria-hidden="true">7.4.</strong> Code generation</a></li><li><a href="attributes/limits.html"><strong aria-hidden="true">7.5.</strong> Limits</a></li></ol></li><li><a href="statements-and-expressions.html"><strong aria-hidden="true">8.</strong> Statements and expressions</a></li><li><ol class="section"><li><a href="statements.html"><strong aria-hidden="true">8.1.</strong> Statements</a></li><li><a href="expressions.html"><strong aria-hidden="true">8.2.</strong> Expressions</a></li><li><ol class="section"><li><a href="expressions/literal-expr.html"><strong aria-hidden="true">8.2.1.</strong> Literal expressions</a></li><li><a href="expressions/path-expr.html"><strong aria-hidden="true">8.2.2.</strong> Path expressions</a></li><li><a href="expressions/block-expr.html"><strong aria-hidden="true">8.2.3.</strong> Block expressions</a></li><li><a href="expressions/operator-expr.html"><strong aria-hidden="true">8.2.4.</strong> Operator expressions</a></li><li><a href="expressions/grouped-expr.html"><strong aria-hidden="true">8.2.5.</strong> Grouped expressions</a></li><li><a href="expressions/array-expr.html"><strong aria-hidden="true">8.2.6.</strong> Array and index expressions</a></li><li><a href="expressions/tuple-expr.html"><strong aria-hidden="true">8.2.7.</strong> Tuple and index expressions</a></li><li><a href="expressions/struct-expr.html"><strong aria-hidden="true">8.2.8.</strong> Struct expressions</a></li><li><a href="expressions/enum-variant-expr.html"><strong aria-hidden="true">8.2.9.</strong> Enum variant expressions</a></li><li><a href="expressions/call-expr.html"><strong aria-hidden="true">8.2.10.</strong> Call expressions</a></li><li><a href="expressions/method-call-expr.html"><strong aria-hidden="true">8.2.11.</strong> Method call expressions</a></li><li><a href="expressions/field-expr.html"><strong aria-hidden="true">8.2.12.</strong> Field access expressions</a></li><li><a href="expressions/closure-expr.html"><strong aria-hidden="true">8.2.13.</strong> Closure expressions</a></li><li><a href="expressions/loop-expr.html"><strong aria-hidden="true">8.2.14.</strong> Loop expressions</a></li><li><a href="expressions/range-expr.html"><strong aria-hidden="true">8.2.15.</strong> Range expressions</a></li><li><a href="expressions/if-expr.html"><strong aria-hidden="true">8.2.16.</strong> If and if let expressions</a></li><li><a href="expressions/match-expr.html"><strong aria-hidden="true">8.2.17.</strong> Match expressions</a></li><li><a href="expressions/return-expr.html"><strong aria-hidden="true">8.2.18.</strong> Return expressions</a></li></ol></li></ol></li><li><a href="patterns.html"><strong aria-hidden="true">9.</strong> Patterns</a></li><li><a href="type-system.html"><strong aria-hidden="true">10.</strong> Type system</a></li><li><ol class="section"><li><a href="types.html"><strong aria-hidden="true">10.1.</strong> Types</a></li><li><ol class="section"><li><a href="types/boolean.html"><strong aria-hidden="true">10.1.1.</strong> Boolean type</a></li><li><a href="types/numeric.html"><strong aria-hidden="true">10.1.2.</strong> Numeric types</a></li><li><a href="types/textual.html"><strong aria-hidden="true">10.1.3.</strong> Textual types</a></li><li><a href="types/never.html"><strong aria-hidden="true">10.1.4.</strong> Never type</a></li><li><a href="types/tuple.html"><strong aria-hidden="true">10.1.5.</strong> Tuple types</a></li><li><a href="types/array.html"><strong aria-hidden="true">10.1.6.</strong> Array types</a></li><li><a href="types/slice.html"><strong aria-hidden="true">10.1.7.</strong> Slice types</a></li><li><a href="types/struct.html"><strong aria-hidden="true">10.1.8.</strong> Struct types</a></li><li><a href="types/enum.html"><strong aria-hidden="true">10.1.9.</strong> Enumerated types</a></li><li><a href="types/union.html"><strong aria-hidden="true">10.1.10.</strong> Union types</a></li><li><a href="types/function-item.html"><strong aria-hidden="true">10.1.11.</strong> Function item types</a></li><li><a href="types/closure.html"><strong aria-hidden="true">10.1.12.</strong> Closure types</a></li><li><a href="types/pointer.html"><strong aria-hidden="true">10.1.13.</strong> Pointer types</a></li><li><a href="types/function-pointer.html"><strong aria-hidden="true">10.1.14.</strong> Function pointer types</a></li><li><a href="types/trait-object.html"><strong aria-hidden="true">10.1.15.</strong> Trait object types</a></li><li><a href="types/impl-trait.html"><strong aria-hidden="true">10.1.16.</strong> Impl trait type</a></li><li><a href="types/parameters.html"><strong aria-hidden="true">10.1.17.</strong> Type parameters</a></li><li><a href="types/inferred.html"><strong aria-hidden="true">10.1.18.</strong> Inferred type</a></li></ol></li><li><a href="dynamically-sized-types.html"><strong aria-hidden="true">10.2.</strong> Dynamically Sized Types</a></li><li><a href="type-layout.html"><strong aria-hidden="true">10.3.</strong> Type layout</a></li><li><a href="interior-mutability.html"><strong aria-hidden="true">10.4.</strong> Interior mutability</a></li><li><a href="subtyping.html"><strong aria-hidden="true">10.5.</strong> Subtyping and Variance</a></li><li><a href="trait-bounds.html"><strong aria-hidden="true">10.6.</strong> Trait and lifetime bounds</a></li><li><a href="type-coercions.html"><strong aria-hidden="true">10.7.</strong> Type coercions</a></li><li><a href="destructors.html"><strong aria-hidden="true">10.8.</strong> Destructors</a></li><li><a href="lifetime-elision.html"><strong aria-hidden="true">10.9.</strong> Lifetime elision</a></li></ol></li><li><a href="special-types-and-traits.html"><strong aria-hidden="true">11.</strong> Special types and traits</a></li><li><a href="memory-model.html"><strong aria-hidden="true">12.</strong> Memory model</a></li><li><ol class="section"><li><a href="memory-allocation-and-lifetime.html"><strong aria-hidden="true">12.1.</strong> Memory allocation and lifetime</a></li><li><a href="memory-ownership.html"><strong aria-hidden="true">12.2.</strong> Memory ownership</a></li><li><a href="variables.html"><strong aria-hidden="true">12.3.</strong> Variables</a></li></ol></li><li><a href="linkage.html"><strong aria-hidden="true">13.</strong> Linkage</a></li><li><a href="unsafety.html"><strong aria-hidden="true">14.</strong> Unsafety</a></li><li><ol class="section"><li><a href="unsafe-functions.html"><strong aria-hidden="true">14.1.</strong> Unsafe functions</a></li><li><a href="unsafe-blocks.html"><strong aria-hidden="true">14.2.</strong> Unsafe blocks</a></li><li><a href="behavior-considered-undefined.html"><strong aria-hidden="true">14.3.</strong> Behavior considered undefined</a></li><li><a href="behavior-not-considered-unsafe.html"><strong aria-hidden="true">14.4.</strong> Behavior not considered unsafe</a></li></ol></li><li><a href="const_eval.html"><strong aria-hidden="true">15.</strong> Constant Evaluation</a></li><li><a href="abi.html"><strong aria-hidden="true">16.</strong> Application Binary Interface</a></li><li><a href="runtime.html"><strong aria-hidden="true">17.</strong> The Rust runtime</a></li><li><a href="appendices.html"><strong aria-hidden="true">18.</strong> Appendices</a></li><li><ol class="section"><li><a href="macro-ambiguity.html"><strong aria-hidden="true">18.1.</strong> Macro Follow-Set Ambiguity Formal Specification</a></li><li><a href="influences.html"><strong aria-hidden="true">18.2.</strong> Influences</a></li><li><a href="glossary.html"><strong aria-hidden="true">18.3.</strong> Glossary</a></li></ol></li></ol>
        </nav>

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

            <div class="page">
                <header><p class="warning">
    For now, this reference is a best-effort document. We strive for validity
    and completeness, but are not yet there. In the future, the docs and lang
    teams will work together to figure out how best to do this. Until then, this
    is a best-effort attempt. If you find something wrong or missing, file an
    <a href="https://github.com/rust-lang-nursery/reference/issues">issue</a> or
    send in a pull request.
</p></header>
                <div id="menu-bar" class="menu-bar">
                    <div id="menu-bar-sticky-container">
                        <div class="left-buttons">
                            <button id="sidebar-toggle" class="icon-button" type="button" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
                                <i class="fa fa-bars"></i>
                            </button>
                            <button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
                                <i class="fa fa-paint-brush"></i>
                            </button>
                            <ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
                                <li role="none"><button role="menuitem" class="theme" id="light">Light <span class="default">(default)</span></button></li>
                                <li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
                                <li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
                                <li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
                                <li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
                            </ul>
                            
                            <button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
                                <i class="fa fa-search"></i>
                            </button>
                            
                        </div>

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

                        <div class="right-buttons">
                            <a href="print.html" title="Print this book" aria-label="Print this book">
                                <i id="print-button" class="fa fa-print"></i>
                            </a>
                        </div>
                    </div>
                </div>

                
                <div id="search-wrapper" class="hidden">
                    <form id="searchbar-outer" class="searchbar-outer">
                        <input type="search" name="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
                    </form>
                    <div id="searchresults-outer" class="searchresults-outer hidden">
                        <div id="searchresults-header" class="searchresults-header"></div>
                        <ul id="searchresults">
                        </ul>
                    </div>
                </div>
                

                <!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
                <script type="text/javascript">
                    document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
                    document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
                    Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
                        link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
                    });
                </script>

                <div id="content" class="content">
                    <main>
                        <a class="header" href="attributes/diagnostics.html#diagnostic-attributes" id="diagnostic-attributes"><h1>Diagnostic attributes</h1></a>
<p>The following <a href="attributes.html">attributes</a> are used for controlling or generating diagnostic
messages during compilation.</p>
<a class="header" href="attributes/diagnostics.html#lint-check-attributes" id="lint-check-attributes"><h2>Lint check attributes</h2></a>
<p>A lint check names a potentially undesirable coding pattern, such as
unreachable code or omitted documentation. The lint attributes <code>allow</code>,
<code>warn</code>, <code>deny</code>, and <code>forbid</code> use the <a href="attributes.html#meta-item-attribute-syntax"><em>MetaListPaths</em></a> syntax to specify a
list of lint names to change the lint level for the 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>warn(C)</code> warns about violations of <code>C</code> but continues compilation.</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>
</ul>
<blockquote>
<p>Note: The lint checks supported by <code>rustc</code> can be found via <code>rustc -W help</code>,
along with their default settings and are documented in the <a href="../rustc/lints/index.html">rustc book</a>.</p>
</blockquote>
<pre><pre class="playpen"><code class="language-rust">
# #![allow(unused_variables)]
#fn main() {
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></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><pre class="playpen"><code class="language-rust compile_fail">
# #![allow(unused_variables)]
#fn main() {
#[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></pre>
<a class="header" href="attributes/diagnostics.html#tool-lint-attributes" id="tool-lint-attributes"><h3>Tool lint attributes</h3></a>
<p>Tool lints allows using scoped lints, to <code>allow</code>, <code>warn</code>, <code>deny</code> or <code>forbid</code>
lints of certain tools.</p>
<p>Currently <code>clippy</code> is the only available lint tool.</p>
<p>Tool lints only get checked when the associated tool is active. If a lint
attribute, such as <code>allow</code>, references a nonexistent tool lint, the compiler
will not warn about the nonexistent lint until you use the tool.</p>
<p>Otherwise, they work just like regular lint attributes:</p>
<pre><pre class="playpen"><code class="language-rust">// set the entire `pedantic` clippy lint group to warn
#![warn(clippy::pedantic)]
// silence warnings from the `filter_map` clippy lint
#![allow(clippy::filter_map)]

fn main() {
    // ...
}

// silence the `cmp_nan` clippy lint just for this function
#[allow(clippy::cmp_nan)]
fn foo() {
    // ...
}
</code></pre></pre>
<a class="header" href="attributes/diagnostics.html#the-deprecated-attribute" id="the-deprecated-attribute"><h2>The <code>deprecated</code> attribute</h2></a>
<p>The <em><code>deprecated</code> attribute</em> marks an item as deprecated. <code>rustc</code> will issue
warnings on usage of <code>#[deprecated]</code> items. <code>rustdoc</code> will show item
deprecation, including the <code>since</code> version and <code>note</code>, if available.</p>
<p>The <code>deprecated</code> attribute has several forms:</p>
<ul>
<li><code>deprecated</code> — Issues a generic message.</li>
<li><code>deprecated = &quot;message&quot;</code> — Includes the given string in the deprecation
message.</li>
<li><a href="attributes.html#meta-item-attribute-syntax"><em>MetaListNameValueStr</em></a> syntax with two optional fields:
<ul>
<li><code>since</code> — Specifies a version number when the item was deprecated. <code>rustc</code>
does not currently interpret the string, but external tools like <a href="https://github.com/rust-lang/rust-clippy">Clippy</a>
may check the validity of the value.</li>
<li><code>note</code> — Specifies a string that should be included in the deprecation
message. This is typically used to provide an explanation about the
deprecation and preferred alternatives.</li>
</ul>
</li>
</ul>
<p>The <code>deprecated</code> attribute may be applied to any <a href="items.html">item</a>, <a href="items/traits.html">trait item</a>, <a href="items/enumerations.html">enum
variant</a>, <a href="items/structs.html">struct field</a>, or <a href="items/external-blocks.html">external block item</a>. It cannot be applied to <a href="items/implementations.html#trait-implementations">trait
implementation items</a>. When applied to an item containing other items, such as
a <a href="items/modules.html">module</a> or <a href="items/implementations.html">implementation</a>, all child items inherit the deprecation attribute.</p>
<!-- NOTE: Currently broken for macros, see https://github.com/rust-lang/rust/issues/49912
Also, it is only rejected for trait impl items (AnnotationKind::Prohibited). In all
other locations, it is silently ignored. Tuple struct fields are ignored.
-->
<p>Here is an example:</p>
<pre><pre class="playpen"><code class="language-rust">
# #![allow(unused_variables)]
#fn main() {
#[deprecated(since = &quot;5.2&quot;, note = &quot;foo was rarely used. Users should instead use bar&quot;)]
pub fn foo() {}

pub fn bar() {}
#}</code></pre></pre>
<p>The <a href="https://github.com/rust-lang/rfcs/blob/master/text/1270-deprecation.md">RFC</a> contains motivations and more details.</p>
<a class="header" href="attributes/diagnostics.html#the-must_use-attribute" id="the-must_use-attribute"><h2>The <code>must_use</code> attribute</h2></a>
<p>The <em><code>must_use</code> attribute</em> is used to issue a diagnostic warning when a value
is not &quot;used&quot;. It can be applied to user-defined composite types
(<a href="items/structs.html"><code>struct</code>s</a>, <a href="items/enumerations.html"><code>enum</code>s</a>, and <a href="items/unions.html"><code>union</code>s</a>), <a href="items/functions.html">functions</a>,
and <a href="items/traits.html">traits</a>.</p>
<p>The <code>must_use</code> attribute may include a message by using the
<a href="attributes.html#meta-item-attribute-syntax"><em>MetaNameValueStr</em></a> syntax such as <code>#[must_use = &quot;example message&quot;]</code>. The
message will be given alongside the warning.</p>
<p>When used on user-defined composite types, if the <a href="expressions.html">expression</a> of an
<a href="statements.html#expression-statements">expression statement</a> has that type, then the <code>unused_must_use</code> lint is
violated.</p>
<pre><pre class="playpen"><code class="language-rust">
# #![allow(unused_variables)]
#fn main() {
#[must_use]
struct MustUse {
    // some fields
}

# impl MustUse {
#   fn new() -&gt; MustUse { MustUse {} }
# }
#
// Violates the `unused_must_use` lint.
MustUse::new();
#}</code></pre></pre>
<p>When used on a function, if the <a href="expressions.html">expression</a> of an <a href="statements.html#expression-statements">expression statement</a> is a
<a href="expressions/call-expr.html">call expression</a> to that function, then the <code>unused_must_use</code> lint is
violated.</p>
<pre><pre class="playpen"><code class="language-rust">
# #![allow(unused_variables)]
#fn main() {
#[must_use]
fn five() -&gt; i32 { 5i32 }

// Violates the unused_must_use lint.
five();
#}</code></pre></pre>
<p>When used on a <a href="items/traits.html">trait declaration</a>, a <a href="expressions/call-expr.html">call expression</a> of an <a href="statements.html#expression-statements">expression
statement</a> to a function that returns an <a href="types/impl-trait.html">impl trait</a> of that trait violates
the <code>unsued_must_use</code> lint.</p>
<pre><pre class="playpen"><code class="language-rust">
# #![allow(unused_variables)]
#fn main() {
#[must_use]
trait Critical {}
impl Critical for i32 {}

fn get_critical() -&gt; impl Critical {
    4i32
}

// Violates the `unused_must_use` lint.
get_critical();
#}</code></pre></pre>
<p>When used on a function in a trait declaration, then the behavior also applies
when the call expression is a function from an implementation of the trait.</p>
<pre><pre class="playpen"><code class="language-rust">
# #![allow(unused_variables)]
#fn main() {
trait Trait {
    #[must_use]
    fn use_me(&amp;self) -&gt; i32;
}

impl Trait for i32 {
    fn use_me(&amp;self) -&gt; i32 { 0i32 }
}

// Violates the `unused_must_use` lint.
5i32.use_me();
#}</code></pre></pre>
<p>When used on a function in a trait implementation, the attribute does nothing.</p>
<blockquote>
<p>Note: Trivial no-op expressions containing the value will not violate the
lint. Examples include wrapping the value in a type that does not implement
<a href="special-types-and-traits.html#drop"><code>Drop</code></a> and then not using that type and being the final expression of a
<a href="expressions/block-expr.html">block expression</a> that is not used.</p>
<pre><pre class="playpen"><code class="language-rust">
# #![allow(unused_variables)]
#fn main() {
#[must_use]
fn five() -&gt; i32 { 5i32 }

// None of these violate the unused_must_use lint.
(five(),);
Some(five());
{ five() };
if true { five() } else { 0i32 };
match true {
    _ =&gt; five()
};
#}</code></pre></pre>
</blockquote>
<blockquote>
<p>Note: It is idiomatic to use a <a href="statements.html#let-statements">let statement</a> with a pattern of <code>_</code>
when a must-used value is purposely discarded.</p>
<pre><pre class="playpen"><code class="language-rust">
# #![allow(unused_variables)]
#fn main() {
#[must_use]
fn five() -&gt; i32 { 5i32 }

// Does not violate the unused_must_use lint.
let _ = five();
#}</code></pre></pre>
</blockquote>

                    </main>

                    <nav class="nav-wrapper" aria-label="Page navigation">
                        <!-- Mobile navigation buttons -->
                        
                            <a rel="prev" href="attributes/derive.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
                                <i class="fa fa-angle-left"></i>
                            </a>
                        

                        
                            <a rel="next" href="attributes/codegen.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
                                <i class="fa fa-angle-right"></i>
                            </a>
                        

                        <div style="clear: both"></div>
                    </nav>
                </div>
            </div>

            <nav class="nav-wide-wrapper" aria-label="Page navigation">
                
                    <a href="attributes/derive.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
                        <i class="fa fa-angle-left"></i>
                    </a>
                

                
                    <a href="attributes/codegen.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
                        <i class="fa fa-angle-right"></i>
                    </a>
                
            </nav>

        </div>

        

        

        

        

        
        <script src="searchindex.js" type="text/javascript" charset="utf-8"></script>
        
        
        <script src="elasticlunr.min.js" type="text/javascript" charset="utf-8"></script>
        <script src="mark.min.js" type="text/javascript" charset="utf-8"></script>
        <script src="searcher.js" type="text/javascript" charset="utf-8"></script>
        

        <script src="clipboard.min.js" type="text/javascript" charset="utf-8"></script>
        <script src="highlight.js" type="text/javascript" charset="utf-8"></script>
        <script src="book.js" type="text/javascript" charset="utf-8"></script>

        <!-- Custom JS scripts -->
        

    </body>
</html>