mirror of https://github.com/sunface/rust-course
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.
450 lines
71 KiB
450 lines
71 KiB
1 month ago
|
<!DOCTYPE HTML>
|
||
|
<html lang="zh-CN" class="light" dir="ltr">
|
||
|
<head>
|
||
|
<!-- Book generated using mdBook -->
|
||
|
<meta charset="UTF-8">
|
||
|
<title>可变借用失败引发的深入思考 - 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/compiler/pitfalls/multiple-mutable-references.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="代码重构导致的可变借用错误"><a class="header" href="#代码重构导致的可变借用错误">代码重构导致的可变借用错误</a></h1>
|
||
|
<p>相信大家都听说过<strong>重构一时爽,一直重构一直爽</strong>的说法,私以为这种说法是很有道理的,不然技术团队绩效从何而来?但是,在 Rust 中,重构可能就不是那么爽快的事了,不信?咱们来看看。</p>
|
||
|
<h2 id="欣赏下报错"><a class="header" href="#欣赏下报错">欣赏下报错</a></h2>
|
||
|
<p>很多时候,错误也是一种美,但是当这种错误每天都能见到时(呕):</p>
|
||
|
<pre><code class="language-css">error[e0499]: cannot borrow ` * self` as mutable more than once at a time;
|
||
|
</code></pre>
|
||
|
<p>虽然这一类错误长得一样,但是我这里的错误可能并不是大家常遇到的那些妖艳错误,废话不多说,一起来看看。</p>
|
||
|
<h2 id="重构前的正确代码"><a class="header" href="#重构前的正确代码">重构前的正确代码</a></h2>
|
||
|
<pre><pre class="playground"><code class="language-rust edition2021"><span class="boring">#![allow(unused)]
|
||
|
</span><span class="boring">fn main() {
|
||
|
</span>struct Test {
|
||
|
a : u32,
|
||
|
b : u32
|
||
|
}
|
||
|
|
||
|
impl Test {
|
||
|
fn increase(&mut self) {
|
||
|
let mut a = &mut self.a;
|
||
|
let mut b = &mut self.b;
|
||
|
*b += 1;
|
||
|
*a += 1;
|
||
|
}
|
||
|
}
|
||
|
<span class="boring">}</span></code></pre></pre>
|
||
|
<p>这段代码是可以正常编译的,也许有读者会有疑问,<code>self</code>在这里被两个变量以可变的方式借用了,明明违反了 Rust 的所有权规则,为何它不会报错?</p>
|
||
|
<p>答案要从很久很久之前开始(啊哒~~~由于我太啰嗦,被正义群众来了一下,那咱现在开始长话短说,直接进入主题)。</p>
|
||
|
<h4 id="正确代码为何不报错"><a class="header" href="#正确代码为何不报错">正确代码为何不报错?</a></h4>
|
||
|
<p>虽然从表面来看,<code>a</code>和<code>b</code>都可变引用了<code>self</code>,但是 Rust 的编译器在很多时候都足够聪明,它发现我们其实仅仅引用了同一个结构体中的不同字段,因此完全可以将其的借用权分离开来。</p>
|
||
|
<p>因此,虽然我们不能同时对整个结构体进行可变引用,但是我们可以分别对结构体中的不同字段进行可变引用,当然,一个字段至多也只能存在一个可变引用,这个最基本的所有权规则还是不能违反的。变量<code>a</code>引用结构体字段<code>a</code>,变量<code>b</code>引用结构体字段<code>b</code>,从底层来说,这种方式也不会造成两个可变引用指向了同一块内存。</p>
|
||
|
<p>至此,正确代码我们已经挖掘完毕,再来看看重构后的错误代码。</p>
|
||
|
<h2 id="重构后的错误代码"><a class="header" href="#重构后的错误代码">重构后的错误代码</a></h2>
|
||
|
<p>由于领导说我们这个函数没办法复用,那就敷衍一下呗:</p>
|
||
|
<pre><pre class="playground"><code class="language-rust edition2021"><span class="boring">#![allow(unused)]
|
||
|
</span><span class="boring">fn main() {
|
||
|
</span>struct Test {
|
||
|
a : u32,
|
||
|
b : u32
|
||
|
}
|
||
|
|
||
|
impl Test {
|
||
|
|
||
|
fn increase_a (&mut self) {
|
||
|
self.a += 1;
|
||
|
}
|
||
|
|
||
|
fn increase(&mut self) {
|
||
|
let b = &mut self.b;
|
||
|
self.increase_a();
|
||
|
*b += 1;
|
||
|
}
|
||
|
}
|
||
|
<span class="boring">}</span></code></pre></pre>
|
||
|
<p>既然领导说了,咱照做,反正他也没说怎么个复用法,咱就来个简单的,把<code>a</code>的递增部分复用下。</p>
|
||
|
<p>代码说实话。。。更丑了,但是更强了吗?</p>
|
||
|
<pre><code class="language-console">error[E0499]: cannot borrow `*self` as mutable more than once at a time
|
||
|
--> src/main.rs:14:9
|
||
|
|
|
||
|
13 | let b = &mut self.b;
|
||
|
| ----------- first mutable borrow occurs here
|
||
|
14 | self.increase_a();
|
||
|
| ^^^^ second mutable borrow occurs here
|
||
|
15 | *b += 1;
|
||
|
| ------- first borrow later used here
|
||
|
</code></pre>
|
||
|
<p>嗯,最开始提到的错误,它终于出现了。</p>
|
||
|
<h2 id="大聪明编译器"><a class="header" href="#大聪明编译器">大聪明编译器</a></h2>
|
||
|
<p>为什么?明明之前还是正确的代码,就因为放入函数中就报错了?我们先从一个简单的理解谈起,当然这个理解也是浮于表面的,等会会深入分析真实的原因。</p>
|
||
|
<p>之前讲到 Rust 编译器挺聪明,可以识别到引用到不同的结构体字段,因此不会报错。但是现在这种情况下,编译器又不够聪明了,一旦放入函数中,编译器将无法理解我们对<code>self</code>的使用:它仅仅用到了一个字段,而不是整个结构体。</p>
|
||
|
<p>因此它会简单的认为,这个结构体作为一个整体被可变借用了,产生两个可变引用,一个引用整个结构体,一个引用了结构体字段<code>b</code>,这两个引用存在重叠的部分,最终导致编译错误。</p>
|
||
|
<h2 id="被冤枉的编译器"><a class="header" href="#被冤枉的编译器">被冤枉的编译器</a></h2>
|
||
|
<p>在工作生活中,我们无法理解甚至错误的理解一件事,有时是因为层次不够导致的。同样,对于本文来说,也是因为我们对编译器的所知不够,才冤枉了它,还给它起了一个屈辱的“大聪明”外号。</p>
|
||
|
<h4 id="深入分析"><a class="header" href="#深入分析">深入分析</a></h4>
|
||
|
<blockquote>
|
||
|
<p>如果只改变相关函数的实现而不改变它的签名,那么不会影响编译的结果</p>
|
||
|
</blockquote>
|
||
|
<p>何为相关函数?当函数<code>a</code>调用了函数<code>b</code>,那么<code>b</code>就是<code>a</code>的相关函数。</p>
|
||
|
<p>上面这句是一条非常重要的编译准则,意思是,对于编译器来说,只要函数签名没有变,那么任何函数实现的修改都不会影响已有的编译结果(前提是函数实现没有错误- , -)。</p>
|
||
|
<p>以前面的代码为例:</p>
|
||
|
<pre><pre class="playground"><code class="language-rust edition2021"><span class="boring">#![allow(unused)]
|
||
|
</span><span class="boring">fn main() {
|
||
|
</span>fn increase_a (&mut self) {
|
||
|
self.a += 1;
|
||
|
}
|
||
|
|
||
|
fn increase(&mut self) {
|
||
|
let b = &mut self.b;
|
||
|
self.increase_a();
|
||
|
*b += 1;
|
||
|
}
|
||
|
<span class="boring">}</span></code></pre></pre>
|
||
|
<p>虽然<code>increase_a</code>在函数实现中没有访问<code>self.b</code>字段,但是它的签名允许它访问<code>b</code>,因此违背了借用规则。事实上,该函数有没有访问<code>b</code>不重要,<strong>因为编译器在这里只关心签名,签名存在可能性,那么就立刻报出错误</strong>。</p>
|
||
|
<p>为何会有这种编译器行为,主要有两个原因:</p>
|
||
|
<ol>
|
||
|
<li>一般来说,我们希望编译器有能力独立的编译每个函数,而无需深入到相关函数的内部实现,因为这样做会带来快得多的编译速度。</li>
|
||
|
<li>如果没有这种保证,那么在实际项目开发中,我们会特别容易遇到各种错误。 假设我们要求编译器不仅仅关注相关函数的签名,还要深入其内部关注实现,那么由于 Rust 严苛的编译规则,当你修改了某个函数内部实现的代码后,可能会引起使用该函数的其它函数的各种错误!对于大型项目来说,这几乎是不可接受的!</li>
|
||
|
</ol>
|
||
|
<p>然后,我们的借用类型这么简单,编译器有没有可能针对这种场景,在现有的借用规则之外增加特殊规则?答案是否定的,由于 Rust 语言的设计哲学:特殊规则的加入需要慎之又慎,而我们的这种情况其实还蛮好解决的,因此编译器不会为此新增规则。</p>
|
||
|
<h2 id="解决办法"><a class="header" href="#解决办法">解决办法</a></h2>
|
||
|
<p>在深入分析中,我们提到一条重要的规则,要影响编译行为,就需要更改相关函数的签名,因此可以修改<code>increase_a</code>的签名:</p>
|
||
|
<pre><pre class="playground"><code class="language-rust edition2021"><span class="boring">#![allow(unused)]
|
||
|
</span><span class="boring">fn main() {
|
||
|
</span>fn increase_a (a :&mut u32) {
|
||
|
*a += 1;
|
||
|
}
|
||
|
|
||
|
fn increase(&mut self) {
|
||
|
let b = &mut self.b;
|
||
|
Test::increase_a(&mut self.a);
|
||
|
*b += 1;
|
||
|
}
|
||
|
<span class="boring">}</span></code></pre></pre>
|
||
|
<p>此时,<code>increase_a</code>这个相关函数,不再使用<code>&mut self</code>作为签名,而是获取了结构体中的字段<code>a</code>,此时编译器又可以清晰的知道:函数<code>increase_a</code>和变量<code>b</code>分别引用了结构体中的不同字段,因此可以编译通过。</p>
|
||
|
<p>当然,除了修改相关函数的签名,你还可以修改调用者的实现:</p>
|
||
|
<pre><pre class="playground"><code class="language-rust edition2021"><span class="boring">#![allow(unused)]
|
||
|
</span><span class="boring">fn main() {
|
||
|
</span>fn increase(&mut self) {
|
||
|
self.increase_a();
|
||
|
self.b += 1;
|
||
|
}
|
||
|
<span class="boring">}</span></code></pre></pre>
|
||
|
<p>在这里,我们不再单独声明变量<code>b</code>,而是直接调用<code>self.b+=1</code>进行递增,根据借用生命周期<a href="https://course.rs/advance/lifetime/advance.html#nllnon-lexical-lifetime">NLL</a>的规则,第一个可变借用<code>self.increase_a()</code>的生命周期随着方法调用的结束而结束,那么就不会影响<code>self.b += 1</code>中的借用。</p>
|
||
|
<h2 id="闭包中的例子"><a class="header" href="#闭包中的例子">闭包中的例子</a></h2>
|
||
|
<p>再来看一个使用了闭包的例子:</p>
|
||
|
<pre><pre class="playground"><code class="language-rust edition2021"><span class="boring">#![allow(unused)]
|
||
|
</span><span class="boring">fn main() {
|
||
|
</span>use tokio::runtime::Runtime;
|
||
|
|
||
|
struct Server {
|
||
|
number_of_connections : u64
|
||
|
}
|
||
|
|
||
|
impl Server {
|
||
|
pub fn new() -> Self {
|
||
|
Server { number_of_connections : 0}
|
||
|
}
|
||
|
|
||
|
pub fn increase_connections_count(&mut self) {
|
||
|
self.number_of_connections += 1;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
struct ServerRuntime {
|
||
|
runtime: Runtime,
|
||
|
server: Server
|
||
|
}
|
||
|
|
||
|
impl ServerRuntime {
|
||
|
pub fn new(runtime: Runtime, server: Server) -> Self {
|
||
|
ServerRuntime { runtime, server }
|
||
|
}
|
||
|
|
||
|
pub fn increase_connections_count(&mut self) {
|
||
|
self.runtime.block_on(async {
|
||
|
self.server.increase_connections_count()
|
||
|
})
|
||
|
}
|
||
|
}
|
||
|
<span class="boring">}</span></code></pre></pre>
|
||
|
<p>代码中使用了<code>tokio</code>,在<code>increase_connections_count</code>函数中启动了一个异步任务,并且等待它的完成。这个函数中分别引用了<code>self</code>中的不同字段:<code>runtime</code>和<code>server</code>,但是可能因为闭包的原因,编译器没有像本文最开始的例子中那样聪明,并不能识别这两个引用仅仅引用了同一个结构体的不同部分,因此报错了:</p>
|
||
|
<pre><code class="language-console">error[E0501]: cannot borrow `self.runtime` as mutable because previous closure requires unique access
|
||
|
--> the_little_things\src\main.rs:28:9
|
||
|
|
|
||
|
28 | self.runtime.block_on(async {
|
||
|
| __________^____________--------_______-
|
||
|
| | | |
|
||
|
| | _________| first borrow later used by call
|
||
|
| ||
|
||
|
29 | || self.server.increase_connections_count()
|
||
|
| || ---- first borrow occurs due to use of `self` in generator
|
||
|
30 | || })
|
||
|
| ||_________-^ second borrow occurs here
|
||
|
| |__________|
|
||
|
| generator construction occurs here
|
||
|
</code></pre>
|
||
|
<h4 id="解决办法-1"><a class="header" href="#解决办法-1">解决办法</a></h4>
|
||
|
<p>解决办法很粗暴,既然编译器不能理解闭包中的引用是不同的,那么我们就主动告诉它:</p>
|
||
|
<pre><pre class="playground"><code class="language-rust edition2021"><span class="boring">#![allow(unused)]
|
||
|
</span><span class="boring">fn main() {
|
||
|
</span>pub fn increase_connections_count(&mut self) {
|
||
|
let runtime = &mut self.runtime;
|
||
|
let server = &mut self.server;
|
||
|
runtime.block_on(async {
|
||
|
server.increase_connections_count()
|
||
|
})
|
||
|
}
|
||
|
<span class="boring">}</span></code></pre></pre>
|
||
|
<p>上面通过变量声明的方式,在闭包外声明了两个变量分别引用结构体<code>self</code>的不同字段,这样一来,编译器就不会那么笨,编译顺利通过。</p>
|
||
|
<p>你也可以这么写:</p>
|
||
|
<pre><pre class="playground"><code class="language-rust edition2021"><span class="boring">#![allow(unused)]
|
||
|
</span><span class="boring">fn main() {
|
||
|
</span>pub fn increase_connections_count(&mut self) {
|
||
|
let ServerRuntime { runtime, server } = self;
|
||
|
runtime.block_on(async {
|
||
|
server.increase_connections_count()
|
||
|
})
|
||
|
}
|
||
|
<span class="boring">}</span></code></pre></pre>
|
||
|
<p>当然,如果难以解决,还有一个笨办法,那就是将<code>server</code>和<code>runtime</code>分离开来,不要放在一个结构体中。</p>
|
||
|
<h2 id="总结"><a class="header" href="#总结">总结</a></h2>
|
||
|
<p>心中有剑,手中无剑,是武学至高境界。</p>
|
||
|
<p>本文列出的那条编译规则,在未来就将是大家心中的那把剑,当这些心剑招式足够多时,量变产生质变,终将天下无敌。</p>
|
||
|
|
||
|
<div id="giscus-container"></div>
|
||
|
</main>
|
||
|
|
||
|
<nav class="nav-wrapper" aria-label="Page navigation">
|
||
|
<!-- Mobile navigation buttons -->
|
||
|
<a rel="prev" href="../../compiler/pitfalls/the-disabled-mutability.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="../../compiler/pitfalls/lazy-iterators.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="../../compiler/pitfalls/the-disabled-mutability.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="../../compiler/pitfalls/lazy-iterators.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 = "compiler/pitfalls/multiple-mutable-references.md"
|
||
|
</script>
|
||
|
|
||
|
|
||
|
<!-- Custom JS scripts -->
|
||
|
<script src="../../assets/custom.js"></script>
|
||
|
<script src="../../assets/bigPicture.js"></script>
|
||
|
|
||
|
|
||
|
</div>
|
||
|
</body>
|
||
|
</html>
|