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.

266 lines
17 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<!DOCTYPE HTML>
<html lang="zh-CN" class="light sidebar-visible" dir="ltr">
<head>
<!-- Book generated using mdBook -->
<meta charset="UTF-8">
<title>1.84 - 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" id="highlight-css" href="../../highlight.css">
<link rel="stylesheet" id="tomorrow-night-css" href="../../tomorrow-night.css">
<link rel="stylesheet" id="ayu-highlight-css" href="../../ayu-highlight.css">
<!-- Custom theme stylesheets -->
<link rel="stylesheet" href="../../theme/style.css">
<!-- Provide site root and default themes to javascript -->
<script>
const path_to_root = "../../";
const default_light_theme = "light";
const default_dark_theme = "navy";
</script>
<!-- Start loading toc.js asap -->
<script src="../../toc.js"></script>
</head>
<body>
<div id="body-container">
<!-- Work around some values being stored in localStorage wrapped in quotes -->
<script>
try {
let theme = localStorage.getItem('mdbook-theme');
let 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>
const default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? default_dark_theme : default_light_theme;
let theme;
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
if (theme === null || theme === undefined) { theme = default_theme; }
const html = document.documentElement;
html.classList.remove('light')
html.classList.add(theme);
html.classList.add("js");
</script>
<input type="checkbox" id="sidebar-toggle-anchor" class="hidden">
<!-- Hide / unhide sidebar before it is displayed -->
<script>
let sidebar = null;
const 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';
html.classList.remove('sidebar-visible');
html.classList.add("sidebar-" + sidebar);
</script>
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
<!-- populated by js -->
<mdbook-sidebar-scrollbox class="sidebar-scrollbox"></mdbook-sidebar-scrollbox>
<noscript>
<iframe class="sidebar-iframe-outer" src="../../toc.html"></iframe>
</noscript>
<div id="sidebar-resize-handle" class="sidebar-resize-handle">
<div class="sidebar-resize-indicator"></div>
</div>
</nav>
<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="default_theme">Auto</button></li>
<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/appendix/rust-versions/1.84.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">
<main>
<h1 id="rust-新版解读--184--const-能力拓展"><a class="header" href="#rust-新版解读--184--const-能力拓展">Rust 新版解读 | 1.84 | Const 能力拓展</a></h1>
<blockquote>
<p>Rust 1.84 官方 release doc: <a href="https://blog.rust-lang.org/2025/01/09/Rust-1.84.0.html">Announcing Rust 1.84.0 | Rust Blog</a></p>
</blockquote>
<p>通过 <a href="https://www.rust-lang.org/tools/install">rustup</a> 安装的同学可以使用以下命令升级到 1.84 版本:</p>
<pre><code class="language-shell">$ rustup update stable
</code></pre>
<h2 id="cargo-在依赖版本选择中考虑-rust-版本"><a class="header" href="#cargo-在依赖版本选择中考虑-rust-版本">Cargo 在依赖版本选择中考虑 Rust 版本</a></h2>
<p>1.84.0 稳定了支持最低 Rust 版本MSRV, minimum supported Rust version的解析器该解析器优先选择与项目声明的 MSRV <code>Cargo.toml</code> 里的 <code>package.rust_version</code> 兼容的依赖版本。通过支持 MSRV 的版本选择,维护者可以减少支持旧工具链的工作量,因为不再需要为每个依赖手动选择旧版本。</p>
<p>你可以通过 <code>.cargo/config.toml</code> 启用支持 MSRV 的解析器:</p>
<pre><code class="language-toml">[resolver]
incompatible-rust-versions = "fallback"
</code></pre>
<p>然后在添加依赖时:</p>
<pre><code class="language-bash">$ cargo add clap
Updating crates.io index
warning: ignoring clap@4.5.23 (which requires rustc 1.74) to maintain demo's rust-version of 1.60
Adding clap v4.0.32 to dependencies
Updating crates.io index
Locking 33 packages to latest Rust 1.60 compatible versions
Adding clap v4.0.32 (available: v4.5.23, requires Rust 1.74)
</code></pre>
<p>在 CI 中<a href="https://doc.rust-lang.org/cargo/guide/continuous-integration.html#verifying-latest-dependencies">验证最新依赖</a>时,你可以覆盖此行为:</p>
<pre><code class="language-bash">$ CARGO_RESOLVER_INCOMPATIBLE_RUST_VERSIONS=allow cargo update
Updating crates.io index
Locking 12 packages to latest compatible versions
Updating clap v4.0.32 -&gt; v4.5.23
</code></pre>
<p>你也可以通过在 <code>Cargo.toml</code> 清单文件中设置 <a href="https://doc.rust-lang.org/cargo/reference/resolver.html#resolver-versions"><code>package.resolver = "3"</code></a> 来启用此功能,但这需要将 MSRV 提升到 1.84。对于使用 Rust 2024 的项目(将在 1.85 中稳定),新的解析器将默认启用。</p>
<p>这为库作者在决定采用新 Rust 工具链功能的策略时提供了更大的灵活性。以前,库采用新 Rust 工具链的功能会迫使使用旧 Rust 版本的下游用户要么升级工具链,要么手动选择与工具链兼容的旧版本库(并避免运行 <code>cargo update</code>)。现在,这些用户将能够自动使用与其旧工具链兼容的旧库版本。</p>
<p>有关决定 MSRV 策略时的更多注意事项,请参阅<a href="https://doc.rust-lang.org/cargo/reference/rust-version.html#setting-and-updating-rust-version">文档</a></p>
<h2 id="新-trait-求解器的迁移开始"><a class="header" href="#新-trait-求解器的迁移开始">新 trait 求解器的迁移开始</a></h2>
<p>Rust 编译器正在迁移到新的 trait 求解器实现。下一代 trait 求解器是 Rust 类型系统核心组件的重新实现。它不仅负责检查 trait 边界(例如 <code>Vec&lt;T&gt;: Clone</code>)是否成立,还被类型系统的许多其他部分使用,例如规范化(确定 <code>&lt;Vec&lt;T&gt; as IntoIterator&gt;::Item</code> 的基础类型)和类型等价(检查 <code>T</code><code>U</code> 是否相同)。</p>
<p>在 1.84 中,新求解器用于检查 trait 实现的一致性。从高层次来看,一致性负责确保在考虑其他 crate 中尚未编写或不可见的代码时,给定类型的 trait 实现最多只有一个。</p>
<p>此稳定化修复了旧实现中的一些主要理论上的正确性问题导致可能会报告以前未报告的“trait 冲突实现”错误。根据 <a href="https://github.com/rust-lang/crater/">Crater</a> 对可用代码的评估,我们预计受影响的模式非常罕见。此次稳定版本还提高了我们证明实现不重叠的能力,在某些情况下允许编写更多代码。</p>
<p>有关更多详细信息,请参阅之前的<a href="https://blog.rust-lang.org/inside-rust/2024/12/04/trait-system-refactor-initiative.html">博客文章</a><a href="https://github.com/rust-lang/rust/pull/130654">稳定报告</a></p>
<h2 id="严格来源-api"><a class="header" href="#严格来源-api">严格来源 API</a></h2>
<p>在 Rust 中,<a href="https://rust-lang.github.io/rfcs/3559-rust-has-provenance.html">指针不仅仅是“整数”或“地址”</a>。例如,“释放后使用”是未定义行为,即使你“幸运”并且在读取/写入之前重新分配了释放的内存。另一个例子是,通过从 <code>&amp;i32</code> 引用派生的指针写入是未定义行为,即使通过不同指针写入同一地址是合法的。这里的底层模式是,指针的计算方式很重要,而不仅仅是计算结果的地址。因此,我们说指针具有<strong>来源</strong>:要完全描述 Rust 中与指针相关的未定义行为,我们不仅需要知道指针指向的地址,还需要跟踪它是从哪些其他指针“派生”的。</p>
<p>大多数情况下程序员不需要过多担心来源指针的派生方式非常清晰。然而当将指针转换为整数并返回时结果指针的来源是不明确的。在此版本中Rust 添加了一组 API可以在许多情况下替代整数指针转换的使用从而避免此类转换固有的歧义。特别是现在可以在不将指针转换为整数(或反方向将整数转换成指针)的情况下实现使用对齐指针的最低有效位存储额外信息的模式。这使得代码更易于推理,更易于编译器分析,并且还有益于像 <a href="https://github.com/rust-lang/miri">Miri</a> 这样的工具和像 <a href="https://www.cl.cam.ac.uk/research/security/ctsrd/cheri/">CHERI</a> 这样的架构,旨在检测和诊断指针滥用。</p>
<p>有关更多详细信息,请参阅标准库中关于<a href="https://doc.rust-lang.org/std/ptr/index.html#provenance">来源</a>的文档。</p>
<h2 id="others"><a class="header" href="#others">Others</a></h2>
<p>其它更新细节,和稳定的 API 列表,包括上述 <code>provenance</code> 相关 API参考<a href="https://blog.rust-lang.org/2025/01/09/Rust-1.84.0.html#stabilized-apis">原Blog</a></p>
</main>
<nav class="nav-wrapper" aria-label="Page navigation">
<!-- Mobile navigation buttons -->
<a rel="prev" href="../../appendix/rust-versions/1.83.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="../../appendix/rust-versions/1.85.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="../../appendix/rust-versions/1.83.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="../../appendix/rust-versions/1.85.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>
<!-- Custom JS scripts -->
<script src="../../assets/custom2.js"></script>
<script src="../../assets/bigPicture.js"></script>
</div>
</body>
</html>