<!DOCTYPE HTML> <html lang="en"> <head> <meta charset="UTF-8"> <title>Publishing a Crate to Crates.io - The Rust Programming Language</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 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"> <style> .page-wrapper.has-warning > .nav-chapters { /* add height for warning content & margin */ top: 120px; } p.warning { background-color: rgb(242, 222, 222); border-bottom-color: rgb(238, 211, 215); border-bottom-left-radius: 4px; border-bottom-right-radius: 4px; border-bottom-style: solid; border-bottom-width: 0.666667px; border-image-outset: 0 0 0 0; border-image-repeat: stretch stretch; border-image-slice: 100% 100% 100% 100%; border-image-source: none; border-image-width: 1 1 1 1; border-left-color: rgb(238, 211, 215); border-left-style: solid; border-left-width: 0.666667px; border-right-color: rgb(238, 211, 215); border-right-style: solid; border-right-width: 0.666667px; border-top-color: rgb(238, 211, 215); border-top-left-radius: 4px; border-top-right-radius: 4px; border-top-style: solid; border-top-width: 0.666667px; color: rgb(185, 74, 72); margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 30px; padding-bottom: 8px; padding-left: 14px; padding-right: 35px; padding-top: 8px; } p.warning strong { color: rgb(185, 74, 72) } p.warning a { color: rgb(0, 136, 204) } </style> <!-- 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><a href="ch01-00-introduction.html"><strong>1.</strong> Introduction</a></li><li><ul class="section"><li><a href="ch01-01-installation.html"><strong>1.1.</strong> Installation</a></li><li><a href="ch01-02-hello-world.html"><strong>1.2.</strong> Hello, World!</a></li></ul></li><li><a href="ch02-00-guessing-game-tutorial.html"><strong>2.</strong> Guessing Game Tutorial</a></li><li><a href="ch03-00-common-programming-concepts.html"><strong>3.</strong> Common Programming Concepts</a></li><li><ul class="section"><li><a href="ch03-01-variables-and-mutability.html"><strong>3.1.</strong> Variables and Mutability</a></li><li><a href="ch03-02-data-types.html"><strong>3.2.</strong> Data Types</a></li><li><a href="ch03-03-how-functions-work.html"><strong>3.3.</strong> How Functions Work</a></li><li><a href="ch03-04-comments.html"><strong>3.4.</strong> Comments</a></li><li><a href="ch03-05-control-flow.html"><strong>3.5.</strong> Control Flow</a></li></ul></li><li><a href="ch04-00-understanding-ownership.html"><strong>4.</strong> Understanding Ownership</a></li><li><ul class="section"><li><a href="ch04-01-what-is-ownership.html"><strong>4.1.</strong> What is Ownership?</a></li><li><a href="ch04-02-references-and-borrowing.html"><strong>4.2.</strong> References & Borrowing</a></li><li><a href="ch04-03-slices.html"><strong>4.3.</strong> Slices</a></li></ul></li><li><a href="ch05-00-structs.html"><strong>5.</strong> Using Structs to Structure Related Data</a></li><li><ul class="section"><li><a href="ch05-01-defining-structs.html"><strong>5.1.</strong> Defining and Instantiating Structs</a></li><li><a href="ch05-02-example-structs.html"><strong>5.2.</strong> An Example Program Using Structs</a></li><li><a href="ch05-03-method-syntax.html"><strong>5.3.</strong> Method Syntax</a></li></ul></li><li><a href="ch06-00-enums.html"><strong>6.</strong> Enums and Pattern Matching</a></li><li><ul class="section"><li><a href="ch06-01-defining-an-enum.html"><strong>6.1.</strong> Defining an Enum</a></li><li><a href="ch06-02-match.html"><strong>6.2.</strong> The <code>match</code> Control Flow Operator</a></li><li><a href="ch06-03-if-let.html"><strong>6.3.</strong> Concise Control Flow with <code>if let</code></a></li></ul></li><li><a href="ch07-00-modules.html"><strong>7.</strong> Modules</a></li><li><ul class="section"><li><a href="ch07-01-mod-and-the-filesystem.html"><strong>7.1.</strong> <code>mod</code> and the Filesystem</a></li><li><a href="ch07-02-controlling-visibility-with-pub.html"><strong>7.2.</strong> Controlling Visibility with <code>pub</code></a></li><li><a href="ch07-03-importing-names-with-use.html"><strong>7.3.</strong> Importing Names with <code>use</code></a></li></ul></li><li><a href="ch08-00-common-collections.html"><strong>8.</strong> Common Collections</a></li><li><ul class="section"><li><a href="ch08-01-vectors.html"><strong>8.1.</strong> Vectors</a></li><li><a href="ch08-02-strings.html"><strong>8.2.</strong> Strings</a></li><li><a href="ch08-03-hash-maps.html"><strong>8.3.</strong> Hash Maps</a></li></ul></li><li><a href="ch09-00-error-handling.html"><strong>9.</strong> Error Handling</a></li><li><ul class="section"><li><a href="ch09-01-unrecoverable-errors-with-panic.html"><strong>9.1.</strong> Unrecoverable Errors with <code>panic!</code></a></li><li><a href="ch09-02-recoverable-errors-with-result.html"><strong>9.2.</strong> Recoverable Errors with <code>Result</code></a></li><li><a href="ch09-03-to-panic-or-not-to-panic.html"><strong>9.3.</strong> To <code>panic!</code> or Not To <code>panic!</code></a></li></ul></li><li><a href="ch10-00-generics.html"><strong>10.</strong> Generic Types, Traits, and Lifetimes</a></li><li><ul class="section"><li><a href="ch10-01-syntax.html"><strong>10.1.</strong> Generic Data Types</a></li><li><a href="ch10-02-traits.html"><strong>10.2.</strong> Traits: Defining Shared Behavior</a></li><li><a href="ch10-03-lifetime-syntax.html"><strong>10.3.</strong> Validating References with Lifetimes</a></li></ul></li><li><a href="ch11-00-testing.html"><strong>11.</strong> Testing</a></li><li><ul class="section"><li><a href="ch11-01-writing-tests.html"><strong>11.1.</strong> Writing tests</a></li><li><a href="ch11-02-running-tests.html"><strong>11.2.</strong> Running tests</a></li><li><a href="ch11-03-test-organization.html"><strong>11.3.</strong> Test Organization</a></li></ul></li><li><a href="ch12-00-an-io-project.html"><strong>12.</strong> An I/O Project</a></li><li><ul class="section"><li><a href="ch12-01-accepting-command-line-arguments.html"><strong>12.1.</strong> Accepting Command Line Arguments</a></li><li><a href="ch12-02-reading-a-file.html"><strong>12.2.</strong> Reading a File</a></li><li><a href="ch12-03-improving-error-handling-and-modularity.html"><strong>12.3.</strong> Improving Error Handling and Modularity</a></li><li><a href="ch12-04-testing-the-librarys-functionality.html"><strong>12.4.</strong> Testing the Library's Functionality</a></li><li><a href="ch12-05-working-with-environment-variables.html"><strong>12.5.</strong> Working with Environment Variables</a></li><li><a href="ch12-06-writing-to-stderr-instead-of-stdout.html"><strong>12.6.</strong> Writing to <code>stderr</code> instead of <code>stdout</code></a></li></ul></li><li><a href="ch13-00-functional-features.html"><strong>13.</strong> Functional Language Features in Rust</a></li><li><ul class="section"><li><a href="ch13-01-closures.html"><strong>13.1.</strong> Closures</a></li><li><a href="ch13-02-iterators.html"><strong>13.2.</strong> Iterators</a></li><li><a href="ch13-03-improving-our-io-project.html"><strong>13.3.</strong> Improving our I/O Project</a></li><li><a href="ch13-04-performance.html"><strong>13.4.</strong> Performance</a></li></ul></li><li><a href="ch14-00-more-about-cargo.html"><strong>14.</strong> More about Cargo and Crates.io</a></li><li><ul class="section"><li><a href="ch14-01-release-profiles.html"><strong>14.1.</strong> Release Profiles</a></li><li><a href="ch14-02-publishing-to-crates-io.html" class="active"><strong>14.2.</strong> Publishing a Crate to Crates.io</a></li><li><a href="ch14-03-cargo-workspaces.html"><strong>14.3.</strong> Cargo Workspaces</a></li><li><a href="ch14-04-installing-binaries.html"><strong>14.4.</strong> Installing Binaries from Crates.io with <code>cargo install</code></a></li><li><a href="ch14-05-extending-cargo.html"><strong>14.5.</strong> Extending Cargo with Custom Commands</a></li></ul></li><li><a href="ch15-00-smart-pointers.html"><strong>15.</strong> Smart Pointers</a></li><li><ul class="section"><li><a href="ch15-01-box.html"><strong>15.1.</strong> <code>Box<T></code> Points to Data on the Heap and Has a Known Size</a></li><li><a href="ch15-02-deref.html"><strong>15.2.</strong> The <code>Deref</code> Trait Allows Access to the Data Through a Reference</a></li><li><a href="ch15-03-drop.html"><strong>15.3.</strong> The <code>Drop</code> Trait Runs Code on Cleanup</a></li><li><a href="ch15-04-rc.html"><strong>15.4.</strong> <code>Rc<T></code>, the Reference Counted Smart Pointer</a></li><li><a href="ch15-05-interior-mutability.html"><strong>15.5.</strong> <code>RefCell<T></code> and the Interior Mutability Pattern</a></li><li><a href="ch15-06-reference-cycles.html"><strong>15.6.</strong> Creating Reference Cycles and Leaking Memory is Safe</a></li></ul></li><li><a href="ch16-00-concurrency.html"><strong>16.</strong> Fearless Concurrency</a></li><li><ul class="section"><li><a href="ch16-01-threads.html"><strong>16.1.</strong> Threads</a></li><li><a href="ch16-02-message-passing.html"><strong>16.2.</strong> Message Passing</a></li><li><a href="ch16-03-shared-state.html"><strong>16.3.</strong> Shared State</a></li><li><a href="ch16-04-extensible-concurrency-sync-and-send.html"><strong>16.4.</strong> Extensible Concurrency: <code>Sync</code> and <code>Send</code></a></li></ul></li><li><a href="ch17-00-oop.html"><strong>17.</strong> Is Rust an Object-Oriented Programming Language?</a></li><li><ul class="section"><li><a href="ch17-01-what-is-oo.html"><strong>17.1.</strong> What Does Object-Oriented Mean?</a></li><li><a href="ch17-02-trait-objects.html"><strong>17.2.</strong> Trait Objects for Using Values of Different Types</a></li><li><a href="ch17-03-oo-design-patterns.html"><strong>17.3.</strong> Object-Oriented Design Pattern Implementations</a></li></ul></li><li><a href="ch18-00-patterns.html"><strong>18.</strong> Patterns Match the Structure of Values</a></li><li><ul class="section"><li><a href="ch18-01-all-the-places-for-patterns.html"><strong>18.1.</strong> All the Places Patterns May be Used</a></li><li><a href="ch18-02-refutability.html"><strong>18.2.</strong> Refutability: Whether a Pattern Might Fail to Match</a></li><li><a href="ch18-03-pattern-syntax.html"><strong>18.3.</strong> All the Pattern Syntax</a></li></ul></li><li><a href="ch19-00-advanced-features.html"><strong>19.</strong> Advanced Features</a></li><li><ul class="section"><li><a href="ch19-01-unsafe-rust.html"><strong>19.1.</strong> Unsafe Rust</a></li><li><a href="ch19-02-advanced-lifetimes.html"><strong>19.2.</strong> Advanced Lifetimes</a></li><li><a href="ch19-03-advanced-traits.html"><strong>19.3.</strong> Advanced Traits</a></li><li><a href="ch19-04-advanced-types.html"><strong>19.4.</strong> Advanced Types</a></li><li><a href="ch19-05-advanced-functions-and-closures.html"><strong>19.5.</strong> Advanced Functions & Closures</a></li></ul></li><li><a href="ch20-00-final-project-a-web-server.html"><strong>20.</strong> Final Project: Building a Multithreaded Web Server</a></li><li><ul class="section"><li><a href="ch20-01-single-threaded.html"><strong>20.1.</strong> A Single Threaded Web Server</a></li><li><a href="ch20-02-slow-requests.html"><strong>20.2.</strong> How Slow Requests Affect Throughput</a></li><li><a href="ch20-03-designing-the-interface.html"><strong>20.3.</strong> Designing the Thread Pool Interface</a></li><li><a href="ch20-04-storing-threads.html"><strong>20.4.</strong> Creating the Thread Pool and Storing Threads</a></li><li><a href="ch20-05-sending-requests-via-channels.html"><strong>20.5.</strong> Sending Requests to Threads Via Channels</a></li><li><a href="ch20-06-graceful-shutdown-and-cleanup.html"><strong>20.6.</strong> Graceful Shutdown and Cleanup</a></li></ul></li><li><a href="appendix-00.html"><strong>21.</strong> Appendix</a></li><li><ul class="section"><li><a href="appendix-01-keywords.html"><strong>21.1.</strong> A - Keywords</a></li><li><a href="appendix-02-operators.html"><strong>21.2.</strong> B - Operators</a></li><li><strong>21.3.</strong> C - Derivable Traits</li><li><strong>21.4.</strong> D - Nightly Rust</li><li><strong>21.5.</strong> E - Macros</li><li><strong>21.6.</strong> F - Translations</li><li><a href="appendix-07-newest-features.html"><strong>21.7.</strong> G - Newest Features</a></li></ul></li></ul> </div> <div id="page-wrapper" class="page-wrapper has-warning"> <div class="page"> <header><p class="warning">You are reading a <strong>draft</strong> of the next edition of TRPL. For more, go <a href="../index.html">here</a>.</p></header> <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 Programming Language</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="ch14-02-publishing-to-crates-io.html#publishing-a-crate-to-cratesio" id="publishing-a-crate-to-cratesio"><h2>Publishing a Crate to Crates.io</h2></a> <p>We've added crates from crates.io as dependencies of our project. We can choose to share our code for other people to use as well. Crates.io distributes the source code of your packages, so it is primarily used to distribute code that's open source.</p> <p>Rust and Cargo have some features that can make your published package easier for people to find and use. We'll talk about some of those features, then cover how to publish a package.</p> <a class="header" href="ch14-02-publishing-to-crates-io.html#documentation-comments" id="documentation-comments"><h3>Documentation Comments</h3></a> <p>In Chapter 3, we saw comments in Rust that start with <code>//</code>. Rust also has a second kind of comment: the <em>documentation comment</em>. While comments can be useful if someone is reading your code, you can generate HTML documentation that displays the contents of documentation comments for public API items meant for someone who's interested in knowing how to <em>use</em> your crate, as opposed to how your crate is <em>implemented</em>. Note that documentation is only generated for library crates, since binary crates don't have a public API that people need to know how to use.</p> <p>Documentation comments use <code>///</code> instead of <code>//</code> and support Markdown notation inside. They go just before the item they are documenting. Here's documentation comments for an <code>add_one</code> function:</p> <p><span class="filename">Filename: src/lib.rs</span></p> <pre><pre class="playpen"><code class="language-rust"># #![allow(unused_variables)] #fn main() { /// Adds one to the number given. /// /// # Examples /// /// ``` /// let five = 5; /// /// assert_eq!(6, add_one(five)); /// ``` pub fn add_one(x: i32) -> i32 { x + 1 } #}</code></pre></pre> <p><span class="caption">Listing 14-1: A documentation comment for a function</span></p> <p><code>cargo doc</code> runs a tool distributed with Rust, <code>rustdoc</code>, to generate HTML documentation from these comments. To try this out locally, you can run <code>cargo doc --open</code>, which will build the documentation for your current crate (as well as all of your crate's dependencies) and open it in a web browser. Navigate to the <code>add_one</code> function and you'll see how the text in the documentation comments gets rendered.</p> <p>Adding examples in code blocks in your documentation comments is a way to clearly demonstrate how to use your library. There's an additional bonus reason to do this: <code>cargo test</code> will run the code examples in your documentation as tests! Nothing is better than documentation with examples. Nothing is worse than examples that don't actually work because the code has changed since the documentation has been written. Try running <code>cargo test</code> with the documentation for the <code>add_one</code> function in Listing 14-1; you'll see a section in the test results like this:</p> <pre><code class="language-test"> Doc-tests add-one running 1 test test add_one_0 ... ok test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured </code></pre> <p>Try changing the function or the example to see that <code>cargo test</code> will catch that the example no longer works!</p> <p>There's another style of doc comment, <code>//!</code>, to comment containing items (e.g. crates, modules or functions), instead of the items following it. These are typically used inside the crate root (lib.rs) or a module's root (mod.rs) to document the crate or the module as a whole, respectively. Here's the documentation within the <code>libstd</code> module that contains the entire standard library:</p> <pre><code>//! # The Rust Standard Library //! //! The Rust Standard Library provides the essential runtime //! functionality for building portable Rust software. </code></pre> <a class="header" href="ch14-02-publishing-to-crates-io.html#exporting-a-convenient-public-api-with-pub-use" id="exporting-a-convenient-public-api-with-pub-use"><h3>Exporting a Convenient Public API with <code>pub use</code></h3></a> <p>In Chapter 7, we covered how to organize our code into modules with the <code>mod</code> keyword, how to make items public with the <code>pub</code> keyword, and how to bring items into a scope with the <code>use</code> keyword. When publishing a crate for people unfamiliar with the implementation to use, it's worth taking time to consider if the structure of your crate that's useful for you as you're developing is what would be useful for people depending on your crate. If the structure isn't convenient to use from another library, you don't have to rearrange your internal organization: you can choose to re-export items to make a different public structure with <code>pub use</code>.</p> <p>For example, say that we made a library named <code>art</code> consisting of a <code>kinds</code> module containing two enums named <code>PrimaryColor</code> and <code>SecondaryColor</code>, and a <code>utils</code> module containing a function named <code>mix</code> as shown in Listing 14-2:</p> <p><span class="filename">Filename: src/lib.rs</span></p> <pre><code class="language-rust ignore">//! # Art //! //! A library for modeling artistic concepts. pub mod kinds { /// The primary colors according to the RYB color model. pub enum PrimaryColor { Red, Yellow, Blue, } /// The secondary colors according to the RYB color model. pub enum SecondaryColor { Orange, Green, Purple, } } pub mod utils { use kinds::*; /// Combines two primary colors in equal amounts to create /// a secondary color. pub fn mix(c1: PrimaryColor, c2: PrimaryColor) -> SecondaryColor { // ...snip... # SecondaryColor::Green } } </code></pre> <p><span class="caption">Listing 14-2: An <code>art</code> library with items organized into <code>kinds</code> and <code>utils</code> modules</span></p> <p>In order to use this library, another crate would have <code>use</code> statements as in Listing 14-3:</p> <p><span class="filename">Filename: src/main.rs</span></p> <pre><code class="language-rust ignore">extern crate art; use art::kinds::PrimaryColor; use art::utils::mix; fn main() { let red = PrimaryColor::Red; let yellow = PrimaryColor::Yellow; mix(red, yellow); } </code></pre> <p><span class="caption">Listing 14-3: A program using the <code>art</code> crate's items with its internal structure exported</span></p> <p>Users of this crate shouldn't need to know that <code>PrimaryColor</code> and <code>SecondaryColor</code> are in the <code>kinds</code> module, and <code>mix</code> is in the <code>utils</code> module; that structure might be useful for internal organization but doesn't have much meaning from the outside looking in.</p> <p>To change this, we can add the following <code>pub use</code> statements to the code from Listing 14-2 to re-export the types at the top level, as shown in Listing 14-4:</p> <p><span class="filename">Filename: src/lib.rs</span></p> <pre><code class="language-rust ignore">//! # Art //! //! A library for modeling artistic concepts. pub use kinds::PrimaryColor; pub use kinds::SecondaryColor; pub use utils::mix; pub mod kinds { // ...snip... </code></pre> <p><span class="caption">Listing 14-4: Adding <code>pub use</code> statements to re-export items</span></p> <!-- Will add ghosting in libreoffice /Carol --> <p>Re-exports are listed and linked on the front page of the crate's API documentation. Users of the <code>art</code> crate can still see and choose to use the internal structure as in Listing 14-3, or they can use the more convenient structure from Listing 14-4, as shown in Listing 14-5:</p> <p><span class="filename">Filename: src/main.rs</span></p> <pre><code class="language-rust ignore">extern crate art; use art::PrimaryColor; use art::mix; fn main() { // ...snip... } </code></pre> <p><span class="caption">Listing 14-5: Using the re-exported items from the <code>art</code> crate</span></p> <!-- Will add ghosting in libreoffice /Carol --> <p>Creating a useful public API structure is more of an art than a science. Choosing <code>pub use</code> gives you flexibility in how you expose your crate's internal structure to users. Take a look at some of the code of crates you've installed to see if their internal structure differs from their public API.</p> <a class="header" href="ch14-02-publishing-to-crates-io.html#before-your-first-publish" id="before-your-first-publish"><h3>Before Your First Publish</h3></a> <p>Before being able to publish any crates, you'll need to create an account on <a href="https://crates.io">crates.io</a> and get an API token. To do so, <a href="https://crates.io">visit the home page</a> and log in via a GitHub account. A GitHub account is a requirement for now, but the site might support other ways of creating an account in the future. Once you're logged in, visit your <a href="https://crates.io/me">Account Settings</a> page and run the <code>cargo login</code> command with the API key as the page specifies, which will look something like this:</p> <pre><code class="language-text">$ cargo login abcdefghijklmnopqrstuvwxyz012345 </code></pre> <p>This command will inform Cargo of your API token and store it locally in <em>~/.cargo/config</em>. Note that this token is a <strong>secret</strong> and should not be shared with anyone else. If it gets shared with anyone for any reason, you should regenerate it immediately.</p> <a class="header" href="ch14-02-publishing-to-crates-io.html#before-publishing-a-new-crate" id="before-publishing-a-new-crate"><h3>Before Publishing a New Crate</h3></a> <p>First, your crate will need a unique name. While you're working on a crate locally, you may name a crate whatever you'd like, but crate names on <a href="https://crates.io">crates.io</a> are allocated on a first-come-first- serve basis. Once a crate name is taken, it cannot be used for another crate, so check on the site that the name you'd like is available.</p> <p>If you try to publish a crate as generated by <code>cargo new</code>, you'll get a warning and then an error:</p> <pre><code class="language-text">$ cargo publish Updating registry `https://github.com/rust-lang/crates.io-index` warning: manifest has no description, license, license-file, documentation, homepage or repository. ...snip... error: api errors: missing or empty metadata fields: description, license. Please see http://doc.crates.io/manifest.html#package-metadata for how to upload metadata </code></pre> <p>We can include more information about our package in <em>Cargo.toml</em>. Some of these fields are optional, but a description and a license are required in order to publish so that people will know what your crate does and under what terms they may use it.</p> <p>The description appears with your crate in search results and on your crate's page. Descriptions are usually a sentence or two. The <code>license</code> field takes a license identifier value, and the possible values have been specified by the Linux Foundation's <a href="http://spdx.org/licenses/">Software Package Data Exchange (SPDX)</a>. If you would like to use a license that doesn't appear there, instead of the <code>license</code> key, you can use <code>license-file</code> to specify the name of a file in your project that contains the text of the license you want to use.</p> <p>Guidance on which license is right for your project is out of scope for this book. Many people in the Rust community choose to license their projects in the same way as Rust itself, with a dual license of <code>MIT/Apache-2.0</code>, which demonstrates that you can specify multiple license identifiers separated by a slash. So the <em>Cargo.toml</em> for a project that is ready to publish might look like this:</p> <pre><code class="language-toml">[package] name = "guessing_game" version = "0.1.0" authors = ["Your Name <you@example.com>"] description = "A fun game where you guess what number the computer has chosen." license = "MIT/Apache-2.0" [dependencies] </code></pre> <p>Be sure to check out the <a href="http://doc.crates.io/manifest.html#package-metadata">documentation on crates.io</a> that describes other metadata you can specify to ensure your crate can be discovered and used more easily!</p> <a class="header" href="ch14-02-publishing-to-crates-io.html#publishing-to-cratesio" id="publishing-to-cratesio"><h3>Publishing to Crates.io</h3></a> <p>Now that we've created an account, saved our API token, chosen a name for our crate, and specified the required metadata, we're ready to publish! Publishing a crate is when a specific version is uploaded to be hosted on crates.io.</p> <p>Take care when publishing a crate, because a publish is <strong>permanent</strong>. The version can never be overwritten, and the code cannot be deleted. However, there is no limit to the number of versions which can be published.</p> <p>Let's run the <code>cargo publish</code> command, which should succeed this time since we've now specified the required metadata:</p> <pre><code class="language-text">$ cargo publish Updating registry `https://github.com/rust-lang/crates.io-index` Packaging guessing_game v0.1.0 (file:///projects/guessing_game) Verifying guessing_game v0.1.0 (file:///projects/guessing_game) Compiling guessing_game v0.1.0 (file:///projects/guessing_game/target/package/guessing_game-0.1.0) Finished dev [unoptimized + debuginfo] target(s) in 0.19 secs Uploading guessing_game v0.1.0 (file:///projects/guessing_game) </code></pre> <p>Congratulations! You've now shared your code with the Rust community, and anyone can easily add your crate as a dependency to their project.</p> <a class="header" href="ch14-02-publishing-to-crates-io.html#publishing-a-new-version-of-an-existing-crate" id="publishing-a-new-version-of-an-existing-crate"><h3>Publishing a New Version of an Existing Crate</h3></a> <p>When you've made changes to your crate and are ready to release a new version, change the <code>version</code> value specified in your <em>Cargo.toml</em>. Use the <a href="http://semver.org/">Semantic Versioning rules</a> to decide what an appropriate next version number is based on the kinds of changes you've made. Then run <code>cargo publish</code> to upload the new version.</p> <a class="header" href="ch14-02-publishing-to-crates-io.html#removing-versions-from-cratesio-with-cargo-yank" id="removing-versions-from-cratesio-with-cargo-yank"><h3>Removing Versions from Crates.io with <code>cargo yank</code></h3></a> <p>Occasions may arise where you publish a version of a crate that actually ends up being broken for one reason or another, such as a syntax error or forgetting to include a file. For situations such as this, Cargo supports <em>yanking</em> a version of a crate.</p> <p>Marking a version of a crate as yanked means that no projects will be able to start depending on that version, but all existing projects that depend on that version will continue to be allowed to download and depend on that version. One of the major goals of crates.io is to act as a permanent archive of code so that builds of all projects will continue to work, and allowing deletion of a version would go against this goal. Essentially, a yank means that all projects with a <em>Cargo.lock</em> will not break, while any future <em>Cargo.lock</em> files generated will not use the yanked version.</p> <p>A yank <strong>does not</strong> delete any code. The yank feature is not intended for deleting accidentally uploaded secrets, for example. If that happens, you must reset those secrets immediately.</p> <p>To yank a version of a crate, run <code>cargo yank</code> and specify which version you want to yank:</p> <pre><code class="language-text">$ cargo yank --vers 1.0.1 </code></pre> <p>You can also undo a yank, and allow projects to start depending on a version again, by adding <code>--undo</code> to the command:</p> <pre><code class="language-text">$ cargo yank --vers 1.0.1 --undo </code></pre> </div> <!-- Mobile navigation buttons --> <a href="ch14-01-release-profiles.html" class="mobile-nav-chapters previous"> <i class="fa fa-angle-left"></i> </a> <a href="ch14-03-cargo-workspaces.html" class="mobile-nav-chapters next"> <i class="fa fa-angle-right"></i> </a> </div> <a href="ch14-01-release-profiles.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="ch14-03-cargo-workspaces.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>