<!DOCTYPE HTML> <html lang="en"> <head> <meta charset="UTF-8"> <title>Documentation tests - </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"><strong aria-hidden="true">3.</strong> The #[doc] attribute</a></li><li><a href="documentation-tests.html" class="active"><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="documentation-tests.html#documentation-tests" id="documentation-tests"><h1>Documentation tests</h1></a> <p><code>rustdoc</code> supports executing your documentation examples as tests. This makes sure that your tests are up to date and working.</p> <p>The basic idea is this:</p> <pre><code class="language-ignore">/// # Examples /// /// ``` /// let x = 5; /// ``` </code></pre> <p>The triple backticks start and end code blocks. If this were in a file named <code>foo.rs</code>, running <code>rustdoc --test foo.rs</code> will extract this example, and then run it as a test.</p> <p>Please note that by default, if no language is set for the block code, <code>rustdoc</code> assumes it is <code>Rust</code> code. So the following:</p> <pre><pre class="playpen"><code class="language-rust"> # #![allow(unused_variables)] #fn main() { let x = 5; #}</code></pre></pre> <p>is strictly equivalent to:</p> <pre><code>let x = 5; </code></pre> <p>There's some subtlety though! Read on for more details.</p> <a class="header" href="documentation-tests.html#pre-processing-examples" id="pre-processing-examples"><h2>Pre-processing examples</h2></a> <p>In the example above, you'll note something strange: there's no <code>main</code> function! Forcing you to write <code>main</code> for every example, no matter how small, adds friction. So <code>rustdoc</code> processes your examples slightly before running them. Here's the full algorithm rustdoc uses to preprocess examples:</p> <ol> <li>Some common <code>allow</code> attributes are inserted, including <code>unused_variables</code>, <code>unused_assignments</code>, <code>unused_mut</code>, <code>unused_attributes</code>, and <code>dead_code</code>. Small examples often trigger these lints.</li> <li>Any attributes specified with <code>#![doc(test(attr(...)))]</code> are added.</li> <li>Any leading <code>#![foo]</code> attributes are left intact as crate attributes.</li> <li>If the example does not contain <code>extern crate</code>, and <code>#![doc(test(no_crate_inject))]</code> was not specified, then <code>extern crate <mycrate>;</code> is inserted (note the lack of <code>#[macro_use]</code>).</li> <li>Finally, if the example does not contain <code>fn main</code>, the remainder of the text is wrapped in <code>fn main() { your_code }</code>.</li> </ol> <p>For more about that caveat in rule 4, see "Documenting Macros" below.</p> <a class="header" href="documentation-tests.html#hiding-portions-of-the-example" id="hiding-portions-of-the-example"><h2>Hiding portions of the example</h2></a> <p>Sometimes, you need some setup code, or other things that would distract from your example, but are important to make the tests work. Consider an example block that looks like this:</p> <pre><code class="language-text">/// Some documentation. # fn foo() {} </code></pre> <p>It will render like this:</p> <pre><pre class="playpen"><code class="language-rust"> # #![allow(unused_variables)] #fn main() { /// Some documentation. # fn foo() {} #}</code></pre></pre> <p>Yes, that's right: you can add lines that start with <code>#</code>, and they will be hidden from the output, but will be used when compiling your code. You can use this to your advantage. In this case, documentation comments need to apply to some kind of function, so if I want to show you just a documentation comment, I need to add a little function definition below it. At the same time, it's only there to satisfy the compiler, so hiding it makes the example more clear. You can use this technique to explain longer examples in detail, while still preserving the testability of your documentation.</p> <p>For example, imagine that we wanted to document this code:</p> <pre><pre class="playpen"><code class="language-rust"> # #![allow(unused_variables)] #fn main() { let x = 5; let y = 6; println!("{}", x + y); #}</code></pre></pre> <p>We might want the documentation to end up looking like this:</p> <blockquote> <p>First, we set <code>x</code> to five:</p> <pre><pre class="playpen"><code class="language-rust"> # #![allow(unused_variables)] #fn main() { let x = 5; # let y = 6; # println!("{}", x + y); #}</code></pre></pre> <p>Next, we set <code>y</code> to six:</p> <pre><pre class="playpen"><code class="language-rust"> # #![allow(unused_variables)] #fn main() { # let x = 5; let y = 6; # println!("{}", x + y); #}</code></pre></pre> <p>Finally, we print the sum of <code>x</code> and <code>y</code>:</p> <pre><pre class="playpen"><code class="language-rust"> # #![allow(unused_variables)] #fn main() { # let x = 5; # let y = 6; println!("{}", x + y); #}</code></pre></pre> </blockquote> <p>To keep each code block testable, we want the whole program in each block, but we don't want the reader to see every line every time. Here's what we put in our source code:</p> <pre><code class="language-text"> First, we set `x` to five: ``` let x = 5; # let y = 6; # println!("{}", x + y); ``` Next, we set `y` to six: ``` # let x = 5; let y = 6; # println!("{}", x + y); ``` Finally, we print the sum of `x` and `y`: ``` # let x = 5; # let y = 6; println!("{}", x + y); ``` </code></pre> <p>By repeating all parts of the example, you can ensure that your example still compiles, while only showing the parts that are relevant to that part of your explanation.</p> <p>Another case where the use of <code>#</code> is handy is when you want to ignore error handling. Lets say you want the following,</p> <pre><code class="language-ignore">/// use std::io; /// let mut input = String::new(); /// io::stdin().read_line(&mut input)?; </code></pre> <p>The problem is that <code>?</code> returns a <code>Result<T, E></code> and test functions don't return anything so this will give a mismatched types error.</p> <pre><code class="language-ignore">/// A doc test using ? /// /// ``` /// use std::io; /// # fn foo() -> io::Result<()> { /// let mut input = String::new(); /// io::stdin().read_line(&mut input)?; /// # Ok(()) /// # } /// ``` # fn foo() {} </code></pre> <p>You can get around this by wrapping the code in a function. This catches and swallows the <code>Result<T, E></code> when running tests on the docs. This pattern appears regularly in the standard library.</p> <a class="header" href="documentation-tests.html#documenting-macros" id="documenting-macros"><h3>Documenting macros</h3></a> <p>Here’s an example of documenting a macro:</p> <pre><pre class="playpen"><code class="language-rust">/// Panic with a given message unless an expression evaluates to true. /// /// # Examples /// /// ``` /// # #[macro_use] extern crate foo; /// # fn main() { /// panic_unless!(1 + 1 == 2, “Math is broken.”); /// # } /// ``` /// /// ```should_panic /// # #[macro_use] extern crate foo; /// # fn main() { /// panic_unless!(true == false, “I’m broken.”); /// # } /// ``` #[macro_export] macro_rules! panic_unless { ($condition:expr, $($rest:expr),+) => ({ if ! $condition { panic!($($rest),+); } }); } # fn main() {} </code></pre></pre> <p>You’ll note three things: we need to add our own <code>extern crate</code> line, so that we can add the <code>#[macro_use]</code> attribute. Second, we’ll need to add our own <code>main()</code> as well (for reasons discussed above). Finally, a judicious use of <code>#</code> to comment out those two things, so they don’t show up in the output.</p> <a class="header" href="documentation-tests.html#attributes" id="attributes"><h2>Attributes</h2></a> <p>There are a few annotations that are useful to help <code>rustdoc</code> do the right thing when testing your code:</p> <pre><pre class="playpen"><code class="language-rust"> # #![allow(unused_variables)] #fn main() { /// ```ignore /// fn foo() { /// ``` # fn foo() {} #}</code></pre></pre> <p>The <code>ignore</code> directive tells Rust to ignore your code. This is almost never what you want, as it's the most generic. Instead, consider annotating it with <code>text</code> if it's not code, or using <code>#</code>s to get a working example that only shows the part you care about.</p> <pre><pre class="playpen"><code class="language-rust"> # #![allow(unused_variables)] #fn main() { /// ```should_panic /// assert!(false); /// ``` # fn foo() {} #}</code></pre></pre> <p><code>should_panic</code> tells <code>rustdoc</code> that the code should compile correctly, but not actually pass as a test.</p> <pre><code class="language-text">/// ```no_run /// loop { /// println!("Hello, world"); /// } /// ``` # fn foo() {} </code></pre> <p><code>compile_fail</code> tells <code>rustdoc</code> that the compilation should fail. If it compiles, then the test will fail. However please note that code failing with the current Rust release may work in a future release, as new features are added.</p> <pre><code class="language-text">/// ```compile_fail /// let x = 5; /// x += 2; // shouldn't compile! /// ``` </code></pre> <p>The <code>no_run</code> attribute will compile your code, but not run it. This is important for examples such as "Here's how to retrieve a web page," which you would want to ensure compiles, but might be run in a test environment that has no network access.</p> </main> <nav class="nav-wrapper" aria-label="Page navigation"> <!-- Mobile navigation buttons --> <a rel="prev" href="the-doc-attribute.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="passes.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="the-doc-attribute.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="passes.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>