<!DOCTYPE HTML> <html lang="en" class="sidebar-visible no-js"> <head> <!-- Book generated using mdBook --> <meta charset="UTF-8"> <title>What is rustdoc? - </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 --> </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><a href="what-is-rustdoc.html" class="active"><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"><strong aria-hidden="true">4.</strong> Documentation tests</a></li><li><a href="passes.html"><strong aria-hidden="true">5.</strong> Passes</a></li><li><a href="unstable-features.html"><strong aria-hidden="true">6.</strong> Unstable features</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="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"></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="what-is-rustdoc.html#what-is-rustdoc" id="what-is-rustdoc"><h1>What is rustdoc?</h1></a> <p>The standard Rust distribution ships with a tool called <code>rustdoc</code>. Its job is to generate documentation for Rust projects. On a fundamental level, Rustdoc takes as an argument either a crate root or a Markdown file, and produces HTML, CSS, and JavaScript.</p> <a class="header" href="what-is-rustdoc.html#basic-usage" id="basic-usage"><h2>Basic usage</h2></a> <p>Let's give it a try! Let's create a new project with Cargo:</p> <pre><code class="language-bash">$ cargo new docs $ cd docs </code></pre> <p>In <code>src/lib.rs</code>, you'll find that Cargo has generated some sample code. Delete it and replace it with this:</p> <pre><pre class="playpen"><code class="language-rust"> # #![allow(unused_variables)] #fn main() { /// foo is a function fn foo() {} #}</code></pre></pre> <p>Let's run <code>rustdoc</code> on our code. To do so, we can call it with the path to our crate root like this:</p> <pre><code class="language-bash">$ rustdoc src/lib.rs </code></pre> <p>This will create a new directory, <code>doc</code>, with a website inside! In our case, the main page is located in <code>doc/lib/index.html</code>. If you open that up in a web browser, you'll see a page with a search bar, and "Crate lib" at the top, with no contents. There's two problems with this: first, why does it think that our package is named "lib"? Second, why does it not have any contents?</p> <p>The first problem is due to <code>rustdoc</code> trying to be helpful; like <code>rustc</code>, it assumes that our crate's name is the name of the file for the crate root. To fix this, we can pass in a command-line flag:</p> <pre><code class="language-bash">$ rustdoc src/lib.rs --crate-name docs </code></pre> <p>Now, <code>doc/docs/index.html</code> will be generated, and the page says "Crate docs."</p> <p>For the second issue, it's because our function <code>foo</code> is not public; <code>rustdoc</code> defaults to generating documentation for only public functions. If we change our code...</p> <pre><pre class="playpen"><code class="language-rust"> # #![allow(unused_variables)] #fn main() { /// foo is a function pub fn foo() {} #}</code></pre></pre> <p>... and then re-run <code>rustdoc</code>:</p> <pre><code class="language-bash">$ rustdoc src/lib.rs --crate-name docs </code></pre> <p>We'll have some generated documentation. Open up <code>doc/docs/index.html</code> and check it out! It should show a link to the <code>foo</code> function's page, which is located at <code>doc/docs/fn.foo.html</code>. On that page, you'll see the "foo is a function" we put inside the documentation comment in our crate.</p> <a class="header" href="what-is-rustdoc.html#using-rustdoc-with-cargo" id="using-rustdoc-with-cargo"><h2>Using rustdoc with Cargo</h2></a> <p>Cargo also has integration with <code>rustdoc</code> to make it easier to generate docs. Instead of the <code>rustdoc</code> command, we could have done this:</p> <pre><code class="language-bash">$ cargo doc </code></pre> <p>Internally, this calls out to <code>rustdoc</code> like this:</p> <pre><code class="language-bash">$ rustdoc --crate-name docs srclib.rs -o <path>\docs\target\doc -L dependency=<path>docs\target\debug\deps </code></pre> <p>You can see this with <code>cargo doc --verbose</code>.</p> <p>It generates the correct <code>--crate-name</code> for us, as well as pointing to <code>src/lib.rs</code> But what about those other arguments? <code>-o</code> controls the <em>o</em>utput of our docs. Instead of a top-level <code>doc</code> directory, you'll notice that Cargo puts generated documentation under <code>target</code>. That's the idiomatic place for generated files in Cargo projects. Also, it passes <code>-L</code>, a flag that helps rustdoc find the dependencies your code relies on. If our project used dependencies, we'd get documentation for them as well!</p> <a class="header" href="what-is-rustdoc.html#using-standalone-markdown-files" id="using-standalone-markdown-files"><h2>Using standalone Markdown files</h2></a> <p><code>rustdoc</code> can also generate HTML from standalone Markdown files. Let's give it a try: create a <code>README.md</code> file with these contents:</p> <pre><code class="language-text"> # Docs This is a project to test out `rustdoc`. [Here is a link!](https://www.rust-lang.org) ## Subheading ```rust fn foo() -> i32 { 1 + 1 } ``` </code></pre> <p>And call <code>rustdoc</code> on it:</p> <pre><code class="language-bash">$ rustdoc README.md </code></pre> <p>You'll find an HTML file in <code>docs/doc/README.html</code> generated from its Markdown contents.</p> <p>Cargo currently does not understand standalone Markdown files, unfortunately.</p> <a class="header" href="what-is-rustdoc.html#summary" id="summary"><h2>Summary</h2></a> <p>This covers the simplest use-cases of <code>rustdoc</code>. The rest of this book will explain all of the options that <code>rustdoc</code> has, and how to use them.</p> </main> <nav class="nav-wrapper" aria-label="Page navigation"> <!-- Mobile navigation buttons --> <a rel="next" href="command-line-arguments.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 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>