You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
rust-course/cargo/reference/publishing-on-crates.io.html

336 lines
67 KiB

1 month ago
<!DOCTYPE HTML>
<html lang="zh-CN" class="light" dir="ltr">
<head>
<!-- Book generated using mdBook -->
<meta charset="UTF-8">
<title>发布到 crates.io - Rust语言圣经(Rust Course)</title>
<!-- Custom HTML head -->
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#ffffff">
<link rel="icon" href="../../favicon.svg">
<link rel="shortcut icon" href="../../favicon.png">
<link rel="stylesheet" href="../../css/variables.css">
<link rel="stylesheet" href="../../css/general.css">
<link rel="stylesheet" href="../../css/chrome.css">
<link rel="stylesheet" href="../../css/print.css" media="print">
<!-- Fonts -->
<link rel="stylesheet" href="../../FontAwesome/css/font-awesome.css">
<link rel="stylesheet" href="../../fonts/fonts.css">
<!-- Highlight.js Stylesheets -->
<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="../../theme/style.css">
</head>
<body class="sidebar-visible no-js">
<div id="body-container">
<!-- Provide site root to javascript -->
<script>
var path_to_root = "../../";
var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";
</script>
<!-- Work around some values being stored in localStorage wrapped in quotes -->
<script>
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>
var theme;
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
if (theme === null || theme === undefined) { theme = default_theme; }
var html = document.querySelector('html');
html.classList.remove('light')
html.classList.add(theme);
var body = document.querySelector('body');
body.classList.remove('no-js')
body.classList.add('js');
</script>
<input type="checkbox" id="sidebar-toggle-anchor" class="hidden">
<!-- Hide / unhide sidebar before it is displayed -->
<script>
var body = document.querySelector('body');
var sidebar = null;
var sidebar_toggle = document.getElementById("sidebar-toggle-anchor");
if (document.body.clientWidth >= 1080) {
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
sidebar = sidebar || 'visible';
} else {
sidebar = 'hidden';
}
sidebar_toggle.checked = sidebar === 'visible';
body.classList.remove('sidebar-visible');
body.classList.add("sidebar-" + sidebar);
</script>
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
<div class="sidebar-scrollbox">
<ol class="chapter"><li class="chapter-item affix "><a href="../../about-book.html">关于本书</a></li><li class="chapter-item affix "><a href="../../into-rust.html">进入 Rust 编程世界</a></li><li class="chapter-item affix "><a href="../../first-try/sth-you-should-not-do.html">避免从入门到放弃</a></li><li class="chapter-item affix "><a href="../../community.html">社区和锈书</a></li><li class="spacer"></li><li class="chapter-item affix "><a href="../../some-thoughts.html">Xobserve: 一切皆可观测</a></li><li class="chapter-item affix "><a href="../../beat-ai.html">BeatAI: 工程师 AI 入门圣经</a></li><li class="chapter-item affix "><li class="part-title">Rust 语言基础学习</li><li class="spacer"></li><li class="chapter-item "><a href="../../first-try/intro.html"><strong aria-hidden="true">1.</strong> 寻找牛刀,以便小试</a><a class="toggle"><div></div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../first-try/installation.html"><strong aria-hidden="true">1.1.</strong> 安装 Rust 环境</a></li><li class="chapter-item "><a href="../../first-try/editor.html"><strong aria-hidden="true">1.2.</strong> 墙推 VSCode!</a></li><li class="chapter-item "><a href="../../first-try/cargo.html"><strong aria-hidden="true">1.3.</strong> 认识 Cargo</a></li><li class="chapter-item "><a href="../../first-try/hello-world.html"><strong aria-hidden="true">1.4.</strong> 不仅仅是 Hello world</a></li><li class="chapter-item "><a href="../../first-try/slowly-downloading.html"><strong aria-hidden="true">1.5.</strong> 下载依赖太慢了?</a></li></ol></li><li class="chapter-item "><a href="../../basic/intro.html"><strong aria-hidden="true">2.</strong> Rust 基础入门</a><a class="toggle"><div></div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../basic/variable.html"><strong aria-hidden="true">2.1.</strong> 变量绑定与解构</a></li><li class="chapter-item "><a href="../../basic/base-type/index.html"><strong aria-hidden="true">2.2.</strong> 基本类型</a><a class="toggle"><div></div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../basic/base-type/numbers.html"><strong aria-hidden="true">2.2.1.</strong> 数值类型</a></li><li class="chapter-item "><a href="../../basic/base-type/char-bool.html"><strong aria-hidden="true">2.2.2.</strong> 字符、布尔、单元类型</a></li><li class="chapter-item "><a href="../../basic/base-type/statement-expression.html"><strong aria-hidden="true">2.2.3.</strong> 语句与表达式</a></li><li class="chapter-item "><a href="../../basic/base-type/function.html"><strong aria-hidden="true">2.2.4.</strong> 函数</a></li></ol></li><li class="chapter-item "><a href="../../basic/ownership/index.html"><strong aria-hidden="true">2.3.</strong> 所有权和借用</a><a class="toggle"><div></div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../basic/ownership/ownership.html"><strong aria-hidden="true">2.3.1.</strong> 所有权</a></li><li class="chapter-item "><a href="../../basic/ownership/borrowing.html"><strong aria-hidden="true">2.3.2.</strong> 引用与借用</a></li></ol></li><li class="chapter-item "><a href="../../basic/compound-type/intro.html"><strong aria-hidden="true">2.4.</strong> 复合类型</a><a class="toggle"><div></div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../basic/compound-type/string-slice.html"><strong aria-hidden="true">2.4.1.</strong> 字符串与切片</a></li><li class="chapter-item "><a href="../../basic/compound-type/tuple.html"><strong aria-hidden="true">2.4.2.</strong> 元组</a></li><li class="chapter-item "><a href="../../basic/compound-type/struct.html"><strong aria-hidden="true">2.4.3.</strong> 结构体</a></li><li class="chapter-item "><a href="../../basic/compound-type/enum.html"><strong aria-hidden="true">2.4.4.</strong> 枚举</a></li><li class="chapter-item "><a href="../../basic/compound-type/array.html"><strong aria-hidden="true">2.4.5.</strong> 数组</a></l
</div>
<div id="sidebar-resize-handle" class="sidebar-resize-handle">
<div class="sidebar-resize-indicator"></div>
</div>
</nav>
<!-- Track and set sidebar scroll position -->
<script>
var sidebarScrollbox = document.querySelector('#sidebar .sidebar-scrollbox');
sidebarScrollbox.addEventListener('click', function(e) {
if (e.target.tagName === 'A') {
sessionStorage.setItem('sidebar-scroll', sidebarScrollbox.scrollTop);
}
}, { passive: true });
var sidebarScrollTop = sessionStorage.getItem('sidebar-scroll');
sessionStorage.removeItem('sidebar-scroll');
if (sidebarScrollTop) {
// preserve sidebar scroll position when navigating via links within sidebar
sidebarScrollbox.scrollTop = sidebarScrollTop;
} else {
// scroll sidebar to current active section when navigating via "next/previous chapter" buttons
var activeSection = document.querySelector('#sidebar .active');
if (activeSection) {
activeSection.scrollIntoView({ block: 'center' });
}
}
</script>
<div id="page-wrapper" class="page-wrapper">
<div class="page">
<div id="menu-bar-hover-placeholder"></div>
<div id="menu-bar" class="menu-bar sticky">
<div class="left-buttons">
<label id="sidebar-toggle" class="icon-button" for="sidebar-toggle-anchor" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
<i class="fa fa-bars"></i>
</label>
<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</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">Rust语言圣经(Rust Course)</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>
<a href="https://github.com/sunface/rust-course" title="Git repository" aria-label="Git repository">
<i id="git-repository-button" class="fa fa-github"></i>
</a>
<a href="https://github.com/sunface/rust-course/edit/main/src/cargo/reference/publishing-on-crates.io.md" title="Suggest an edit" aria-label="Suggest an edit">
<i id="git-edit-button" class="fa fa-edit"></i>
</a>
</div>
</div>
<div id="search-wrapper" class="hidden">
<form id="searchbar-outer" class="searchbar-outer">
<input type="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>
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">
<!-- Page table of contents -->
<div class="sidetoc"><nav class="pagetoc"></nav></div>
<main>
<h1 id="发布到-cratesio"><a class="header" href="#发布到-cratesio">发布到 crates.io</a></h1>
<p>如果你想要把自己的开源项目分享给全世界,那最好的办法自然是 GitHub。但如果是 Rust 的库,那除了发布到 GitHub 外,我们还可以将其发布到 <a href="https://crates.io">crates.io</a> 上,然后其它用户就可以很简单的对其进行引用。</p>
<blockquote>
<p>注意:发布包到 <code>crates.io</code> 后,特定的版本无法被覆盖,要发布就必须使用新的版本号,代码也无法被删除!</p>
</blockquote>
<h2 id="首次发布之前"><a class="header" href="#首次发布之前">首次发布之前</a></h2>
<p><strong>首先,我们需要一个账号</strong>:访问 crates.io 的<a href="https://crates.io">主页</a>,然后在右上角使用 GitHub 账户登陆,接着访问你的<a href="https://crates.io/settings/profile">账户设置</a>页面,进入到 API Tokens 标签页下,生成新的 Token并使用该 Token 在终端中进行登录:</p>
<pre><code class="language-shell">$ cargo login abcdefghijklmnopqrstuvwxyz012345
</code></pre>
<p>该命令将告诉 Cargo 你的 API Token然后将其存储在本地的 <code>~/.cargo/credentials.toml</code> 文件中。</p>
<blockquote>
<p>注意:你需要妥善保管好 API Token并且不要告诉任何人一旦泄漏请撤销( Revoke )并重新生成。</p>
</blockquote>
<h2 id="发布包之前"><a class="header" href="#发布包之前">发布包之前</a></h2>
<p><code>crates.io</code> 上的<strong>包名遵循先到先得</strong>的方式:一旦你想要的包名已经被使用,那么你就得换一个不同的包名。</p>
<p>在发布之前,<strong>确保</strong> <code>Cargo.toml</code> 中以下字段已经被设置:</p>
<ul>
<li><a href="https://course.rs/cargo/reference/manifest.html#license%E5%92%8Clicense-file">license 或 license-file</a></li>
<li><a href="https://course.rs/cargo/reference/manifest.html#description">description</a></li>
<li><a href="https://course.rs/cargo/reference/manifest.html#homepage">homepage</a></li>
<li><a href="https://course.rs/cargo/reference/manifest.html#documentation">documentation</a></li>
<li><a href="https://course.rs/cargo/reference/manifest.html#repository">repository</a></li>
<li><a href="https://course.rs/cargo/reference/manifest.html#readme">readme</a></li>
</ul>
<p>你还可以设置<a href="https://course.rs/cargo/reference/manifest.html#keywords">关键字</a><a href="https://course.rs/cargo/reference/manifest.html#categories">类别</a>等元信息,让包更容易被其他人搜索发现,虽然它们不是必须的。</p>
<p>如果你发布的是一个依赖库,那么你可能需要遵循相关的<a href="https://course.rs/practice/naming.html">命名规范</a><a href="https://rust-lang.github.io/api-guidelines/">API Guidlines</a>.</p>
<h2 id="打包"><a class="header" href="#打包">打包</a></h2>
<p>下一步就是将你的项目进行打包,然后上传到 <code>crates.io</code>。为了实现这个目的,我们可以使用 <code>cargo publish</code> 命令,该命令执行了以下步骤:</p>
<ol>
<li>对项目进行一些验证</li>
<li>将源代码压缩到 <code>.crate</code> 文件中</li>
<li><code>.crate</code> 文件解压并放入到临时的目录中,并验证解压出的代码可以顺利编译</li>
<li>上传 <code>.crate</code> 文件到 <code>crates.io</code></li>
<li>注册服务会对上传的包进行一些额外的验证,然后才会添加它到注册服务列表中</li>
</ol>
<p>在发布之前,我们推荐你先运行 <code>cargo publish --dry-run</code> (或 <a href="https://doc.rust-lang.org/stable/cargo/commands/cargo-package.html"><code>cargo package</code></a> ) 命令来确保代码没有 warning 或错误。</p>
<pre><code class="language-shell">$ cargo publish --dry-run
</code></pre>
<p>你可以在 <code>target/package</code> 目录下观察生成的 <code>.crate</code> 文件。例如,目前 <code>crates.io</code> 要求该文件的大小不能超过 10MB你可以通过手动检查该文件的大小来确保不会无意间打包进一些较大的资源文件比如测试数据、网站文档或生成的代码等。我们还可以使用以下命令来检查其中包含的文件:</p>
<pre><code class="language-shell">$ cargo package --list
</code></pre>
<p>当打包时Cargo 会自动根据版本控制系统的配置来忽略指定的文件,例如 <code>.gitignore</code>。除此之外,你还可以通过 <a href="https://course.rs/cargo/reference/manifest.html#exclude%E5%92%8Cinclude"><code>exclude</code></a> 来排除指定的文件:</p>
<pre><code class="language-toml">[package]
# ...
exclude = [
&quot;public/assets/*&quot;,
&quot;videos/*&quot;,
]
</code></pre>
<p>如果想要显式地将某些文件包含其中,可以使用 <code>include</code>,但是需要注意的是,这个 key 一旦设置,那 <code>exclude</code> 就将失效:</p>
<pre><code class="language-toml">[package]
# ...
include = [
&quot;**/*.rs&quot;,
&quot;Cargo.toml&quot;,
]
</code></pre>
<h2 id="上传包"><a class="header" href="#上传包">上传包</a></h2>
<p>准备好后,我们就可以正式来上传指定的包了,在根目录中运行:</p>
<pre><code class="language-shell">$ cargo publish
</code></pre>
<p>就是这么简单,恭喜你,完成了第一个包的发布!</p>
<h2 id="发布已上传包的新版本"><a class="header" href="#发布已上传包的新版本">发布已上传包的新版本</a></h2>
<p>绝大多数时候,我们并不是在发布新包,而是发布已经上传过的包的新版本。</p>
<p>为了实现这一点,只需修改 <code>Cargo.toml</code> 中的 <a href="https://course.rs/cargo/reference/manifest.html#version"><code>version</code></a> 字段 ,但需要注意:<strong>版本号需要遵循 <code>semver</code> 规则</strong></p>
<p>然后再次使用 <code>cargo publish</code> 就可以上传新的版本了。</p>
<h2 id="管理-cratesio-上的包"><a class="header" href="#管理-cratesio-上的包">管理 crates.io 上的包</a></h2>
<p>目前来说,管理包更多地是通过 <code>cargo</code> 命令而不是在线管理,下面是一些你可以使用的命令。</p>
<h4 id="cargo-yank"><a class="header" href="#cargo-yank">cargo yank</a></h4>
<p>有的时候你会遇到发布的包版本实际上并不可用(例如语法错误,或者忘记包含一个文件等)对于这种情况Cargo 提供了 yank 命令:</p>
<pre><code class="language-shell">$ cargo yank --vers 1.0.1
$ cargo yank --vers 1.0.1 --undo
</code></pre>
<p>该命令<strong>并不能删除任何代码</strong>,例如如果你上传了一段隐私内容,你需要的是立刻重置它们,而不是使用 <code>cargo yank</code></p>
<p><code>yank</code> 能做到的就是让其它人不能再使用这个版本作为依赖,但是现存的依赖依然可以继续工作。<code>crates.io</code> 的一个主要目标就是作为一个不会随着时间变化的永久性包存档,但<strong>删除某个版本显然违背了这个目标</strong></p>
<h4 id="cargo-owner"><a class="header" href="#cargo-owner">cargo owner</a></h4>
<p>一个包可能会有多个主要开发者,甚至维护者 maintainer 都会发生变更。目前来说,只有包的 owner 才能发布新的版本,但是一个 owner 可以指定其它的用户为 owner:</p>
<pre><code class="language-shell">$ cargo owner --add github-handle
$ cargo owner --remove github-handle
$ cargo owner --add github:rust-lang:owners
$ cargo owner --remove github:rust-lang:owners
</code></pre>
<p>命令中使用的 ownerID 必须是 GitHub 用户名或 Team 名。</p>
<p>一旦一个用户 <code>B</code> 通过 <code>--add</code> 被加入到 <code>owner</code> 列表中他将拥有该包相关的所有权利。例如发布新版本、yank 一个版本,还能增加和移除 owner包含添加 <code>B</code> 为 owner 的 <code>A</code> 都可以被移除!</p>
<p>因此,我们必须严肃的指出:<strong>不要将你不信任的人添加为 owner !</strong> 免得哪天反目成仇后,他把你移除了 - , -</p>
<p>但是对于 Team 又有所不同,通过 <code>-add</code> 添加的 GitHub Team owner只拥有受限的权利。它们可以发布或 yank 某个版本,但是他们<strong>不能添加或移除</strong> owner总之Team 除了可以很方便的管理所有者分组的同时,还能防止一些未知的恶意。</p>
<p>如果大家在添加 team 时遇到问题,可以看看官方的<a href="https://doc.rust-lang.org/stable/cargo/reference/publishing.html#github-permissions">相关文档</a>,由于绝大多数人都无需此功能,因此这里不再详细展开。</p>
<div id="giscus-container"></div>
</main>
<nav class="nav-wrapper" aria-label="Page navigation">
<!-- Mobile navigation buttons -->
<a rel="prev" href="../../cargo/reference/configuration.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 prefetch" href="../../cargo/reference/build-script/intro.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 rel="prev" href="../../cargo/reference/configuration.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="../../cargo/reference/build-script/intro.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>
window.playground_copyable = true;
</script>
<script src="../../ace.js"></script>
<script src="../../editor.js"></script>
<script src="../../mode-rust.js"></script>
<script src="../../theme-dawn.js"></script>
<script src="../../theme-tomorrow_night.js"></script>
<script src="../../elasticlunr.min.js"></script>
<script src="../../mark.min.js"></script>
<script src="../../searcher.js"></script>
<script src="../../clipboard.min.js"></script>
<script src="../../highlight.js"></script>
<script src="../../book.js"></script>
<script type="text/javascript" charset="utf-8">
var pagePath = "cargo/reference/publishing-on-crates.io.md"
</script>
<!-- Custom JS scripts -->
<script src="../../assets/custom.js"></script>
<script src="../../assets/bigPicture.js"></script>
</div>
</body>
</html>