<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>2.5 理解型变、引用与重借用 on 编程那些事儿</title><link>https://before80.github.io/prgms/Rust/rustLearning/borrow-errors/variance-references-reborrows/</link><description>Recent content in 2.5 理解型变、引用与重借用 on 编程那些事儿</description><generator>Hugo</generator><language>zh-cn</language><lastBuildDate>Sun, 23 Aug 2026 21:05:58 +0800</lastBuildDate><atom:link href="https://before80.github.io/prgms/Rust/rustLearning/borrow-errors/variance-references-reborrows/index.xml" rel="self" type="application/rss+xml"/><item><title>2.5.1 心智模型的种子</title><link>https://before80.github.io/prgms/Rust/rustLearning/borrow-errors/variance-references-reborrows/2.5.1-mental-model-seed/</link><pubDate>Sun, 23 Aug 2026 10:16:00 +0800</pubDate><guid>https://before80.github.io/prgms/Rust/rustLearning/borrow-errors/variance-references-reborrows/2.5.1-mental-model-seed/</guid><description>&lt;blockquote&gt;
&lt;p&gt;译文 · 基于 &lt;a href="https://quinedot.github.io/rust-learning/"&gt;Learning Rust&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h1 id="mental-model-seed"&gt;心智模型的种子&lt;/h1&gt;
&lt;blockquote&gt;
&lt;p&gt;原文链接: &lt;a href="https://quinedot.github.io/rust-learning/st-model.html"&gt;https://quinedot.github.io/rust-learning/st-model.html&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;有些人觉得把共享引用（&lt;code&gt;&amp;amp;T&lt;/code&gt;）和独占引用（&lt;code&gt;&amp;amp;mut T&lt;/code&gt;）想成下面这样会有帮助：&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;&amp;amp;T&lt;/code&gt; 相当于经编译器检查的 &lt;code&gt;RwLockReadGuard&lt;/code&gt;
&lt;ul&gt;
&lt;li&gt;你可以同时拥有任意多个&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;code&gt;&amp;amp;mut T&lt;/code&gt; 相当于经编译器检查的 &lt;code&gt;RwLockWriteGuard&lt;/code&gt;
&lt;ul&gt;
&lt;li&gt;同一时刻只能有一个，且存在时不能再有 &lt;code&gt;RwLockReadGuard&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;独占性是关键。&lt;/p&gt;</description></item><item><title>2.5.2 引用类型</title><link>https://before80.github.io/prgms/Rust/rustLearning/borrow-errors/variance-references-reborrows/2.5.2-reference-types/</link><pubDate>Sun, 23 Aug 2026 10:16:00 +0800</pubDate><guid>https://before80.github.io/prgms/Rust/rustLearning/borrow-errors/variance-references-reborrows/2.5.2-reference-types/</guid><description>&lt;blockquote&gt;
&lt;p&gt;译文 · 基于 &lt;a href="https://quinedot.github.io/rust-learning/"&gt;Learning Rust&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h1 id="reference-types"&gt;引用类型&lt;/h1&gt;
&lt;blockquote&gt;
&lt;p&gt;原文链接: &lt;a href="https://quinedot.github.io/rust-learning/st-types.html"&gt;https://quinedot.github.io/rust-learning/st-types.html&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;先从一个问题开始：&lt;code&gt;&amp;amp;str&lt;/code&gt; 是一种类型吗？&lt;/p&gt;
&lt;p&gt;在不咬文嚼字、不追求形式严谨时，几乎所有人都会说是，&lt;code&gt;&amp;amp;str&lt;/code&gt; 是一种类型。然而从技术上讲，它是一个由泛型生命周期参数参数化的&lt;em&gt;类型构造器&lt;/em&gt;。因此严格来说 &lt;code&gt;&amp;amp;str&lt;/code&gt; 不是类型，某个具体生命周期的 &lt;code&gt;&amp;amp;'a str&lt;/code&gt; 才是类型。&lt;code&gt;&amp;amp;'a str&lt;/code&gt; 与 &lt;code&gt;&amp;amp;'b str&lt;/code&gt; 不是同一类型，除非 &lt;code&gt;'a == 'b&lt;/code&gt;。&lt;/p&gt;</description></item><item><title>2.5.3 生命周期约束</title><link>https://before80.github.io/prgms/Rust/rustLearning/borrow-errors/variance-references-reborrows/2.5.3-lifetime-bounds/</link><pubDate>Sun, 23 Aug 2026 10:16:00 +0800</pubDate><guid>https://before80.github.io/prgms/Rust/rustLearning/borrow-errors/variance-references-reborrows/2.5.3-lifetime-bounds/</guid><description>&lt;blockquote&gt;
&lt;p&gt;译文 · 基于 &lt;a href="https://quinedot.github.io/rust-learning/"&gt;Learning Rust&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h1 id="lifetime-bounds"&gt;生命周期约束&lt;/h1&gt;
&lt;blockquote&gt;
&lt;p&gt;原文链接: &lt;a href="https://quinedot.github.io/rust-learning/st-bounds.html"&gt;https://quinedot.github.io/rust-learning/st-bounds.html&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;下面简要介绍你可能在 &lt;code&gt;fn&lt;/code&gt; 声明和 &lt;code&gt;impl&lt;/code&gt; 块中见到的生命周期约束。&lt;/p&gt;
&lt;h2 id="bounds-between-lifetimes"&gt;生命周期之间的约束&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;'a: 'b&lt;/code&gt; 约束大致意味着 &lt;code&gt;'long: 'short&lt;/code&gt;。
它常被读作「&lt;code&gt;'a&lt;/code&gt; 比 &lt;code&gt;'b&lt;/code&gt; 活得久」，有时也称为「outlives 约束」或「outlives 关系」。&lt;/p&gt;</description></item><item><title>2.5.4 引用生命周期</title><link>https://before80.github.io/prgms/Rust/rustLearning/borrow-errors/variance-references-reborrows/2.5.4-reference-lifetimes/</link><pubDate>Sun, 23 Aug 2026 10:16:00 +0800</pubDate><guid>https://before80.github.io/prgms/Rust/rustLearning/borrow-errors/variance-references-reborrows/2.5.4-reference-lifetimes/</guid><description>&lt;blockquote&gt;
&lt;p&gt;译文 · 基于 &lt;a href="https://quinedot.github.io/rust-learning/"&gt;Learning Rust&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h1 id="reference-lifetimes"&gt;引用生命周期&lt;/h1&gt;
&lt;blockquote&gt;
&lt;p&gt;原文链接: &lt;a href="https://quinedot.github.io/rust-learning/st-references.html"&gt;https://quinedot.github.io/rust-learning/st-references.html&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;下面这点你在 Rust 中会经常用到，甚至无需多想：&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;&amp;amp;'long T&lt;/code&gt; 可强制转换为 &lt;code&gt;&amp;amp;'short T&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;&amp;amp;'long mut T&lt;/code&gt; 可强制转换为 &lt;code&gt;&amp;amp;'short mut T&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;技术术语是「在生命周期上协变（covariant）」，但实用的直觉是「引用的（外层）生命周期可以缩短」。&lt;/p&gt;</description></item><item><title>2.5.5 Copy 与重借用</title><link>https://before80.github.io/prgms/Rust/rustLearning/borrow-errors/variance-references-reborrows/2.5.5-copy-and-reborrows/</link><pubDate>Sun, 23 Aug 2026 10:16:00 +0800</pubDate><guid>https://before80.github.io/prgms/Rust/rustLearning/borrow-errors/variance-references-reborrows/2.5.5-copy-and-reborrows/</guid><description>&lt;blockquote&gt;
&lt;p&gt;译文 · 基于 &lt;a href="https://quinedot.github.io/rust-learning/"&gt;Learning Rust&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h1 id="copy-and-reborrows"&gt;Copy 与重借用&lt;/h1&gt;
&lt;blockquote&gt;
&lt;p&gt;原文链接: &lt;a href="https://quinedot.github.io/rust-learning/st-reborrow.html"&gt;https://quinedot.github.io/rust-learning/st-reborrow.html&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;共享引用（&lt;code&gt;&amp;amp;T&lt;/code&gt;）实现了 &lt;code&gt;Copy&lt;/code&gt;，这使它们非常灵活。一旦拥有一个，你就可以拥有任意多个；一旦对外暴露了一个，你就无法追踪还有多少个。&lt;/p&gt;</description></item><item><title>2.5.6 嵌套借用与不变性</title><link>https://before80.github.io/prgms/Rust/rustLearning/borrow-errors/variance-references-reborrows/2.5.6-nested-borrows-invariance/</link><pubDate>Sun, 23 Aug 2026 10:16:00 +0800</pubDate><guid>https://before80.github.io/prgms/Rust/rustLearning/borrow-errors/variance-references-reborrows/2.5.6-nested-borrows-invariance/</guid><description>&lt;blockquote&gt;
&lt;p&gt;译文 · 基于 &lt;a href="https://quinedot.github.io/rust-learning/"&gt;Learning Rust&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h1 id="nested-borrows-invariance"&gt;嵌套借用与不变性&lt;/h1&gt;
&lt;blockquote&gt;
&lt;p&gt;原文链接: &lt;a href="https://quinedot.github.io/rust-learning/st-invariance.html"&gt;https://quinedot.github.io/rust-learning/st-invariance.html&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;现在让我们考虑嵌套引用：&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;&amp;amp;'medium &amp;amp;'long U&lt;/code&gt; 可以强制转换为 &lt;code&gt;&amp;amp;'short &amp;amp;'short U&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;&amp;amp;'medium mut &amp;amp;'long mut U&lt;/code&gt; 可以强制转换为 &lt;code&gt;&amp;amp;'short mut &amp;amp;'long mut U&lt;/code&gt;……
&lt;ul&gt;
&lt;li&gt;……但&lt;strong&gt;不能&lt;/strong&gt;转换为 &lt;code&gt;&amp;amp;'short mut &amp;amp;'short mut U&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;我们说 &lt;code&gt;&amp;amp;mut T&lt;/code&gt; 在 &lt;code&gt;T&lt;/code&gt; 上是&lt;em&gt;不变的&lt;/em&gt;（invariant），这意味着 &lt;code&gt;T&lt;/code&gt; 中的任何生命周期都完全不能改变（变长或变短）。在上面的例子中，&lt;code&gt;T&lt;/code&gt; 是 &lt;code&gt;&amp;amp;'long mut U&lt;/code&gt;，而 &lt;code&gt;'long&lt;/code&gt; 不能被改变。&lt;/p&gt;</description></item><item><title>2.5.7 其他场景的不变性</title><link>https://before80.github.io/prgms/Rust/rustLearning/borrow-errors/variance-references-reborrows/2.5.7-invariance-elsewhere/</link><pubDate>Sun, 23 Aug 2026 10:16:00 +0800</pubDate><guid>https://before80.github.io/prgms/Rust/rustLearning/borrow-errors/variance-references-reborrows/2.5.7-invariance-elsewhere/</guid><description>&lt;blockquote&gt;
&lt;p&gt;译文 · 基于 &lt;a href="https://quinedot.github.io/rust-learning/"&gt;Learning Rust&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h1 id="invariance-elsewhere"&gt;其他场景的不变性&lt;/h1&gt;
&lt;blockquote&gt;
&lt;p&gt;原文链接: &lt;a href="https://quinedot.github.io/rust-learning/st-more-invariance.html"&gt;https://quinedot.github.io/rust-learning/st-more-invariance.html&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;虽然 &lt;code&gt;&amp;amp;mut&lt;/code&gt; 背后是最常首次遇到不变性的地方，但它也出现在其他地方。&lt;/p&gt;
&lt;p&gt;&lt;code&gt;Cell&amp;lt;T&amp;gt;&lt;/code&gt; 和 &lt;code&gt;RefCell&amp;lt;T&amp;gt;&lt;/code&gt; 在 &lt;code&gt;T&lt;/code&gt; 上同样是不变的。&lt;/p&gt;
&lt;p&gt;trait 参数也是不变的。因此，带生命周期参数的 trait 可能很难使用。&lt;/p&gt;</description></item><item><title>2.5.8 非引用类型</title><link>https://before80.github.io/prgms/Rust/rustLearning/borrow-errors/variance-references-reborrows/2.5.8-non-references/</link><pubDate>Sun, 23 Aug 2026 10:16:00 +0800</pubDate><guid>https://before80.github.io/prgms/Rust/rustLearning/borrow-errors/variance-references-reborrows/2.5.8-non-references/</guid><description>&lt;blockquote&gt;
&lt;p&gt;译文 · 基于 &lt;a href="https://quinedot.github.io/rust-learning/"&gt;Learning Rust&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h1 id="non-references"&gt;非引用类型&lt;/h1&gt;
&lt;blockquote&gt;
&lt;p&gt;原文链接: &lt;a href="https://quinedot.github.io/rust-learning/st-non-references.html"&gt;https://quinedot.github.io/rust-learning/st-non-references.html&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;你自己定义的 &lt;code&gt;struct&lt;/code&gt; 的生命周期和类型参数的型变，会根据你在字段中如何使用这些参数自动推断。例如，如果你有：&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div style="color:#b0c4de;background-color:#282c34;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;
&lt;table style="border-spacing:0;padding:0;margin:0;border:0;"&gt;&lt;tr&gt;&lt;td style="vertical-align:top;padding:0;margin:0;border:0;"&gt;
&lt;pre tabindex="0" style="color:#b0c4de;background-color:#282c34;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code&gt;&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#58626f"&gt;1
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%"&gt;
&lt;pre tabindex="0" style="color:#b0c4de;background-color:#282c34;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-rust" data-lang="rust"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#76a9f9"&gt;struct&lt;/span&gt; &lt;span style="color:#ca72ff"&gt;AllMine&lt;/span&gt;&lt;span style="color:#54b1c7"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#cebc3a"&gt;&amp;#39;a&lt;/span&gt;&lt;span style="color:#abb2bf"&gt;,&lt;/span&gt; &lt;span style="color:#aa89ea"&gt;T&lt;/span&gt;&lt;span style="color:#54b1c7"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#abb2bf"&gt;(&lt;/span&gt;&lt;span style="color:#54b1c7"&gt;&amp;amp;&lt;/span&gt;&lt;span style="color:#cebc3a"&gt;&amp;#39;a&lt;/span&gt; &lt;span style="color:#76a9f9"&gt;mut&lt;/span&gt; &lt;span style="color:#aa89ea"&gt;T&lt;/span&gt;&lt;span style="color:#abb2bf"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;那么 &lt;code&gt;AllMine&lt;/code&gt; 在 &lt;code&gt;'a&lt;/code&gt; 上是协变的，在 &lt;code&gt;T&lt;/code&gt; 上是不变的，就像 &lt;code&gt;&amp;amp;'a mut T&lt;/code&gt; 一样。&lt;/p&gt;</description></item></channel></rss>