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.

403 lines
68 KiB

1 month ago
<!DOCTYPE HTML>
<html lang="zh-CN" class="light" dir="ltr">
<head>
<!-- Book generated using mdBook -->
<meta charset="UTF-8">
<title>通过 config.toml 对 Cargo 进行配置 - 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/configuration.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="通过-configtoml-对-cargo-进行配置"><a class="header" href="#通过-configtoml-对-cargo-进行配置">通过 config.toml 对 Cargo 进行配置</a></h1>
<p>Cargo 相关的配置有两种,第一种是对自身进行配置,第二种是对指定的项目进行配置,关于后者请查看 <a href="https://course.rs/cargo/reference/manifest.html">Cargo.toml 清单</a>。对于普通用户而言第二种才是我们最常使用的。</p>
<p>本文讲述的是如何对 Cargo 相关的工具进行配置,该配置中的部分内容可能会覆盖掉 <code>Cargo.toml</code> 中对应的部分,例如关于 <code>profile</code> 的内容。</p>
<h2 id="层级结构"><a class="header" href="#层级结构">层级结构</a></h2>
<p>在前面我们已经见识过如何为 Cargo 进行全局配置:<code>$HOME/.cargo/config.toml</code>,事实上,还支持在一个 <code>package</code> 内对它进行配置。</p>
<p>总体原则是:<code>Cargo</code> 会顺着当前目录往上查找,直到找到目标配置文件。例如我们在目录 <code>/projects/foo/bar/baz</code> 下调用 Cargo 命令,那查找路径如下所示:</p>
<ul>
<li><code>/projects/foo/bar/baz/.cargo/config.toml</code></li>
<li><code>/projects/foo/bar/.cargo/config.toml</code></li>
<li><code>/projects/foo/.cargo/config.toml</code></li>
<li><code>/projects/.cargo/config.toml</code></li>
<li><code>/.cargo/config.toml</code></li>
<li><code>$CARGO_HOME/config.toml</code> 默认是 :
<ul>
<li>Windows: <code>%USERPROFILE%\.cargo\config.toml</code></li>
<li>Unix: <code>$HOME/.cargo/config.toml</code></li>
</ul>
</li>
</ul>
<p>有了这种机制,我们既可以在全局中设置默认的配置,又可以每个包都设定独立的配置,甚至还能做版本控制。</p>
<p>如果一个 <code>key</code> 在多个配置中出现,那这些 <code>key</code> 只会保留一个:最靠近 Cargo 执行目录的配置文件中的 key 的值将被最终使用(因此, $HOME 下的都是最低优先级)。<strong>需要注意的是,如果 <code>key</code> 的值是数组,那相应的值将被合并( join )</strong></p>
<p>对于工作空间而言,<code>Cargo</code> 的搜索策略是从 root 开始,对于内部成员中包含的 <code>.cargo.toml</code> 会自动忽略。例如一个工作空间拥有两个成员,每个成员都有配置文件: <code>/projects/foo/bar/baz/mylib/.cargo/config.toml</code><code>/projects/foo/bar/baz/mybin/.cargo/config.toml</code>,但是 <code>Cargo</code> 并不会读取它们而是从工作空间的根( <code>/projects/foo/bar/baz/</code> )开始往上查找。</p>
<blockquote>
<p>注意Cargo 还支持没有 <code>.toml</code> 后缀的 <code>.cargo/config</code> 文件。对于 <code>.toml</code> 的支持是从 Rust 1.39 版本开始,同时也是目前最推荐的方式。<strong>但若同时存在有后缀和无后缀的文件Cargo 将使用无后缀的!</strong></p>
</blockquote>
<h2 id="配置文件概览"><a class="header" href="#配置文件概览">配置文件概览</a></h2>
<p>下面是一个完整的配置文件,并对<strong>常用的选项</strong>进行了翻译,大家可以参考下:</p>
<pre><code class="language-toml">paths = [&quot;/path/to/override&quot;] # 覆盖 `Cargo.toml` 中通过 path 引入的本地依赖
[alias] # 命令别名
b = &quot;build&quot;
c = &quot;check&quot;
t = &quot;test&quot;
r = &quot;run&quot;
rr = &quot;run --release&quot;
space_example = [&quot;run&quot;, &quot;--release&quot;, &quot;--&quot;, &quot;\&quot;command list\&quot;&quot;]
[build]
jobs = 1 # 并行构建任务的数量,默认等于 CPU 的核心数
rustc = &quot;rustc&quot; # rust 编译器
rustc-wrapper = &quot;&quot; # 使用该 wrapper 来替代 rustc
rustc-workspace-wrapper = &quot;&quot; # 为工作空间的成员使用 该 wrapper 来替代 rustc
rustdoc = &quot;rustdoc&quot; # 文档生成工具
target = &quot;triple&quot; # 为 target triple 构建 ( `cargo install` 会忽略该选项)
target-dir = &quot;target&quot; # 存放编译输出结果的目录
rustflags = [&quot;&quot;, &quot;&quot;] # 自定义flags会传递给所有的编译器命令调用
rustdocflags = [&quot;&quot;, &quot;&quot;] # 自定义flags传递给 rustdoc
incremental = true # 是否开启增量编译
dep-info-basedir = &quot;&quot; # path for the base directory for targets in depfiles
pipelining = true # rustc pipelining
[doc]
browser = &quot;chromium&quot; # `cargo doc --open` 使用的浏览器,
# 可以通过 `BROWSER` 环境变量进行重写
[env]
# Set ENV_VAR_NAME=value for any process run by Cargo
ENV_VAR_NAME = &quot;value&quot;
# Set even if already present in environment
ENV_VAR_NAME_2 = { value = &quot;value&quot;, force = true }
# Value is relative to .cargo directory containing `config.toml`, make absolute
ENV_VAR_NAME_3 = { value = &quot;relative/path&quot;, relative = true }
[cargo-new]
vcs = &quot;none&quot; # 所使用的 VCS ('git', 'hg', 'pijul', 'fossil', 'none')
[http]
debug = false # HTTP debugging
proxy = &quot;host:port&quot; # HTTP 代理libcurl 格式
ssl-version = &quot;tlsv1.3&quot; # TLS version to use
ssl-version.max = &quot;tlsv1.3&quot; # 最高支持的 TLS 版本
ssl-version.min = &quot;tlsv1.1&quot; # 最小支持的 TLS 版本
timeout = 30 # HTTP 请求的超时时间,秒
low-speed-limit = 10 # 网络超时阈值 (bytes/sec)
cainfo = &quot;cert.pem&quot; # path to Certificate Authority (CA) bundle
check-revoke = true # check for SSL certificate revocation
multiplexing = true # HTTP/2 multiplexing
user-agent = &quot;&quot; # the user-agent header
[install]
root = &quot;/some/path&quot; # `cargo install` 安装到的目标目录
[net]
retry = 2 # 网络重试次数
git-fetch-with-cli = true # 是否使用 `git` 命令来执行 git 操作
offline = true # 不能访问网络
[patch.&lt;registry&gt;]
# Same keys as for [patch] in Cargo.toml
[profile.&lt;name&gt;] # profile 配置,详情见&quot;如何在 Cargo.toml 中配置 profile&quot; : https://course.rs/cargo/reference/profiles.html#profile设置
opt-level = 0
debug = true
split-debuginfo = '...'
debug-assertions = true
overflow-checks = true
lto = false
panic = 'unwind'
incremental = true
codegen-units = 16
rpath = false
[profile.&lt;name&gt;.build-override]
[profile.&lt;name&gt;.package.&lt;name&gt;]
[registries.&lt;name&gt;] # 设置其它的注册服务: https://course.rs/cargo/reference/specify-deps.html#从其它注册服务引入依赖包
index = &quot;&quot; # 注册服务索引列表的 URL
token = &quot;&quot; # 连接注册服务所需的鉴权 token
[registry]
default = &quot;&quot; # 默认的注册服务名称: crates.io
token = &quot;&quot;
[source.&lt;name&gt;] # 注册服务源和替换source definition and replacement
replace-with = &quot;&quot; # 使用给定的 source 来替换当前的 source例如使用科大源来替换crates.io源以提升国内的下载速度[source.crates-io] replace-with = 'ustc'
directory = &quot;&quot; # path to a directory source
registry = &quot;&quot; # 注册源的 URL ,例如科大源: [source.ustc] registry = &quot;git://mirrors.ustc.edu.cn/crates.io-index&quot;
local-registry = &quot;&quot; # path to a local registry source
git = &quot;&quot; # URL of a git repository source
branch = &quot;&quot; # branch name for the git repository
tag = &quot;&quot; # tag name for the git repository
rev = &quot;&quot; # revision for the git repository
[target.&lt;triple&gt;]
linker = &quot;&quot; # linker to use
runner = &quot;&quot; # wrapper to run executables
rustflags = [&quot;&quot;, &quot;&quot;] # custom flags for `rustc`
[target.&lt;cfg&gt;]
runner = &quot;&quot; # wrapper to run executables
rustflags = [&quot;&quot;, &quot;&quot;] # custom flags for `rustc`
[target.&lt;triple&gt;.&lt;links&gt;] # `links` build script override
rustc-link-lib = [&quot;foo&quot;]
rustc-link-search = [&quot;/path/to/foo&quot;]
rustc-flags = [&quot;-L&quot;, &quot;/some/path&quot;]
rustc-cfg = ['key=&quot;value&quot;']
rustc-env = {key = &quot;value&quot;}
rustc-cdylib-link-arg = [&quot;&quot;]
metadata_key1 = &quot;value&quot;
metadata_key2 = &quot;value&quot;
[term]
verbose = false # whether cargo provides verbose output
color = 'auto' # whether cargo colorizes output
progress.when = 'auto' # whether cargo shows progress bar
progress.width = 80 # width of progress bar
</code></pre>
<h2 id="环境变量"><a class="header" href="#环境变量">环境变量</a></h2>
<p>除了 <code>config.toml</code> 配置文件,我们还可以使用环境变量的方式对 Cargo 进行配置。</p>
<p>配置文件的中的 key <code>foo.bar</code> 对应的环境变量形式为 <code>CARGO_FOO_BAR</code>,其中的<code>.</code><code>-</code> 被转换成 <code>_</code>,且字母都变成大写的。例如,<code>target.x86_64-unknown-linux-gnu.runner</code> key 转换成环境变量后变成 <code>CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_RUNNER</code></p>
<p>就优先级而言环境变量是比配置文件更高的。除了上面的机制Cargo 还支持一些<a href="https://doc.rust-lang.org/stable/cargo/reference/environment-variables.html">预定义的环境变量</a></p>
<blockquote>
<p>官方 Cargo Book 中本文的内容还有<a href="https://doc.rust-lang.org/stable/cargo/reference/config.html#configuration-keys">很多</a>,但是剩余内容对于绝大多数用户都用不到,因此我们并没有涵盖其中。</p>
</blockquote>
<div id="giscus-container"></div>
</main>
<nav class="nav-wrapper" aria-label="Page navigation">
<!-- Mobile navigation buttons -->
<a rel="prev" href="../../cargo/reference/profiles.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/publishing-on-crates.io.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/profiles.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/publishing-on-crates.io.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/configuration.md"
</script>
<!-- Custom JS scripts -->
<script src="../../assets/custom.js"></script>
<script src="../../assets/bigPicture.js"></script>
</div>
</body>
</html>