<!DOCTYPE HTML> <html lang="en"> <head> <meta charset="UTF-8"> <title>The #[doc] attribute - </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="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"> <link rel="stylesheet" href="ayu-highlight.css"> <!-- Custom theme --> <!-- Fetch Clipboard.js from CDN but have a local fallback --> <script src="https://cdn.jsdelivr.net/clipboard.js/1.6.1/clipboard.min.js"></script> <script> if (typeof Clipboard == 'undefined') { document.write(unescape("%3Cscript src='clipboard.min.js'%3E%3C/script%3E")); } </script> </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; </script> <!-- Hide / unhide sidebar before it is displayed --> <script type="text/javascript"> var sidebar = 'hidden'; if (document.body.clientWidth >= 1080) { try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { } sidebar = sidebar || 'visible'; } document.querySelector('html').classList.add("sidebar-" + sidebar); </script> <nav id="sidebar" class="sidebar" aria-label="Table of contents"> <ol class="chapter"><li><a href="what-is-rustdoc.html"><strong aria-hidden="true">1.</strong> What is rustdoc?</a></li><li><a href="command-line-arguments.html"><strong aria-hidden="true">2.</strong> Command-line arguments</a></li><li><a href="the-doc-attribute.html" class="active"><strong aria-hidden="true">3.</strong> The #[doc] attribute</a></li><li><a href="documentation-tests.html"><strong aria-hidden="true">4.</strong> Documentation tests</a></li><li><a href="passes.html"><strong aria-hidden="true">5.</strong> Passes</a></li></ol> </nav> <div id="page-wrapper" class="page-wrapper"> <div class="page"> <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="submenu"> <li><button class="theme" id="light">Light <span class="default">(default)</span></button></li> <li><button class="theme" id="rust">Rust</button></li> <li><button class="theme" id="coal">Coal</button></li> <li><button class="theme" id="navy">Navy</button></li> <li><button class="theme" id="ayu">Ayu</button></li> </ul> </div> <h1 class="menu-title"></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> <!-- 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="the-doc-attribute.html#the-doc-attribute" id="the-doc-attribute"><h1>The <code>#[doc]</code> attribute</h1></a> <p>The <code>#[doc]</code> attribute lets you control various aspects of how <code>rustdoc</code> does its job.</p> <p>The most basic function of <code>#[doc]</code> is to handle the actual documentation text. That is, <code>///</code> is syntax sugar for <code>#[doc]</code>. This means that these two are the same:</p> <pre><code class="language-rust ignore">/// This is a doc comment. #[doc = " This is a doc comment."] </code></pre> <p>(Note the leading space in the attribute version.)</p> <p>In most cases, <code>///</code> is easier to use than <code>#[doc]</code>. One case where the latter is easier is when generating documentation in macros; the <code>collapse-docs</code> pass will combine multiple <code>#[doc]</code> attributes into a single doc comment, letting you generate code like this:</p> <pre><code class="language-rust ignore">#[doc = "This is"] #[doc = " a "] #[doc = "doc comment"] </code></pre> <p>Which can feel more flexible. Note that this would generate this:</p> <pre><code class="language-rust ignore">#[doc = "This is\n a \ndoc comment"] </code></pre> <p>but given that docs are rendered via Markdown, it will remove these newlines.</p> <p>The <code>doc</code> attribute has more options though! These don't involve the text of the output, but instead, various aspects of the presentation of the output. We've split them into two kinds below: attributes that are useful at the crate level, and ones that are useful at the item level.</p> <a class="header" href="the-doc-attribute.html#at-the-crate-level" id="at-the-crate-level"><h2>At the crate level</h2></a> <p>These options control how the docs look at a macro level.</p> <a class="header" href="the-doc-attribute.html#html_favicon_url" id="html_favicon_url"><h3><code>html_favicon_url</code></h3></a> <p>This form of the <code>doc</code> attribute lets you control the favicon of your docs.</p> <pre><code class="language-rust ignore">#![doc(html_favicon_url = "https://example.com/favicon.ico")] </code></pre> <p>This will put <code><link rel="shortcut icon" href="{}"></code> into your docs, where the string for the attribute goes into the <code>{}</code>.</p> <p>If you don't use this attribute, there will be no favicon.</p> <a class="header" href="the-doc-attribute.html#html_logo_url" id="html_logo_url"><h3><code>html_logo_url</code></h3></a> <p>This form of the <code>doc</code> attribute lets you control the logo in the upper left hand side of the docs.</p> <pre><code class="language-rust ignore">#![doc(html_logo_url = "https://example.com/logo.jpg")] </code></pre> <p>This will put <code><a href='index.html'><img src='{}' alt='logo' width='100'></a></code> into your docs, where the string for the attribute goes into the <code>{}</code>.</p> <p>If you don't use this attribute, there will be no logo.</p> <a class="header" href="the-doc-attribute.html#html_playground_url" id="html_playground_url"><h3><code>html_playground_url</code></h3></a> <p>This form of the <code>doc</code> attribute lets you control where the "run" buttons on your documentation examples make requests to.</p> <pre><code class="language-rust ignore">#![doc(html_playground_url = "https://playground.example.com/")] </code></pre> <p>Now, when you press "run", the button will make a request to this domain.</p> <p>If you don't use this attribute, there will be no run buttons.</p> <a class="header" href="the-doc-attribute.html#issue_tracker_base_url" id="issue_tracker_base_url"><h3><code>issue_tracker_base_url</code></h3></a> <p>This form of the <code>doc</code> attribute is mostly only useful for the standard library; When a feature is unstable, an issue number for tracking the feature must be given. <code>rustdoc</code> uses this number, plus the base URL given here, to link to the tracking issue.</p> <pre><code class="language-rust ignore">#![doc(issue_tracker_base_url = "https://github.com/rust-lang/rust/issues/")] </code></pre> <a class="header" href="the-doc-attribute.html#html_no_source" id="html_no_source"><h3><code>html_no_source</code></h3></a> <p>By default, <code>rustdoc</code> will include the source code of your program, with links to it in the docs. But if you include this:</p> <pre><code class="language-rust ignore">#![doc(html_no_source)] </code></pre> <p>it will not.</p> <a class="header" href="the-doc-attribute.html#testno_crate_inject" id="testno_crate_inject"><h3><code>test(no_crate_inject)</code></h3></a> <p>By default, <code>rustdoc</code> will automatically add a line with <code>extern crate my_crate;</code> into each doctest. But if you include this:</p> <pre><code class="language-rust ignore">#![doc(test(no_crate_inject))] </code></pre> <p>it will not.</p> <a class="header" href="the-doc-attribute.html#testattr" id="testattr"><h3><code>test(attr(...))</code></h3></a> <p>This form of the <code>doc</code> attribute allows you to add arbitrary attributes to all your doctests. For example, if you want your doctests to fail if they produce any warnings, you could add this:</p> <pre><code class="language-rust ignore">#![doc(test(attr(deny(warnings))))] </code></pre> <a class="header" href="the-doc-attribute.html#at-the-item-level" id="at-the-item-level"><h2>At the item level</h2></a> <p>These forms of the <code>#[doc]</code> attribute are used on individual items, to control how they are documented.</p> <a class="header" href="the-doc-attribute.html#docno_inlinedocinline" id="docno_inlinedocinline"><h2><code>#[doc(no_inline)]</code>/<code>#[doc(inline)]</code></h2></a> <p>These attributes are used on <code>use</code> statements, and control where the documentation shows up. For example, consider this Rust code:</p> <pre><code class="language-rust ignore">pub use bar::Bar; /// bar docs pub mod bar { /// the docs for Bar pub struct Bar; } </code></pre> <p>The documentation will generate a "Re-exports" section, and say <code>pub use bar::Bar;</code>, where <code>Bar</code> is a link to its page.</p> <p>If we change the <code>use</code> line like this:</p> <pre><code class="language-rust ignore">#[doc(inline)] pub use bar::Bar; </code></pre> <p>Instead, <code>Bar</code> will appear in a <code>Structs</code> section, just like <code>Bar</code> was defined at the top level, rather than <code>pub use</code>'d.</p> <p>Let's change our original example, by making <code>bar</code> private:</p> <pre><code class="language-rust ignore">pub use bar::Bar; /// bar docs mod bar { /// the docs for Bar pub struct Bar; } </code></pre> <p>Here, because <code>bar</code> is not public, <code>Bar</code> wouldn't have its own page, so there's nowhere to link to. <code>rustdoc</code> will inline these definitions, and so we end up in the same case as the <code>#[doc(inline)]</code> above; <code>Bar</code> is in a <code>Structs</code> section, as if it were defined at the top level. If we add the <code>no_inline</code> form of the attribute:</p> <pre><code class="language-rust ignore">#[doc(no_inline)] pub use bar::Bar; /// bar docs mod bar { /// the docs for Bar pub struct Bar; } </code></pre> <p>Now we'll have a <code>Re-exports</code> line, and <code>Bar</code> will not link to anywhere.</p> <a class="header" href="the-doc-attribute.html#dochidden" id="dochidden"><h2><code>#[doc(hidden)]</code></h2></a> <p>Any item annotated with <code>#[doc(hidden)]</code> will not appear in the documentation, unless the <code>strip-hidden</code> pass is removed.</p> <a class="header" href="the-doc-attribute.html#docprimitive" id="docprimitive"><h2><code>#[doc(primitive)]</code></h2></a> <p>Since primitive types are defined in the compiler, there's no place to attach documentation attributes. This attribute is used by the standard library to provide a way to generate documentation for primitive types.</p> </main> <nav class="nav-wrapper" aria-label="Page navigation"> <!-- Mobile navigation buttons --> <a rel="prev" href="command-line-arguments.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="documentation-tests.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="command-line-arguments.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="documentation-tests.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> <!-- Local fallback for Font Awesome --> <script> if (getComputedStyle(document.querySelector(".fa")).fontFamily !== "FontAwesome") { var link = document.createElement('link'); link.rel = 'stylesheet'; link.type = 'text/css'; link.href = '_FontAwesome/css/font-awesome.css'; document.head.insertBefore(link, document.head.firstChild) } </script> <script src="highlight.js"></script> <script src="book.js"></script> <!-- Custom JS script --> </body> </html>