<feed xmlns="http://www.w3.org/2005/Atom"> <id>https://nihil.cc/</id><title>Nihil</title><subtitle>Nichts Hsu 的个人博客</subtitle> <updated>2025-08-23T12:50:21+08:00</updated> <author> <name>Nichts Hsu</name> <uri>https://nihil.cc/</uri> </author><link rel="self" type="application/atom+xml" href="https://nihil.cc/feed.xml"/><link rel="alternate" type="text/html" hreflang="zh-CN" href="https://nihil.cc/"/> <generator uri="https://jekyllrb.com/" version="4.4.1">Jekyll</generator> <rights> © 2025 Nichts Hsu </rights> <icon>/assets/img/favicons/favicon.ico</icon> <logo>/assets/img/favicons/favicon-96x96.png</logo> <entry><title>[Rust] 幽灵索引类型与匿名结构体</title><link href="https://nihil.cc/posts/phantom_index_type/" rel="alternate" type="text/html" title="[Rust] 幽灵索引类型与匿名结构体" /><published>2024-04-26T17:16:35+08:00</published> <updated>2024-04-26T17:29:53+08:00</updated> <id>https://nihil.cc/posts/phantom_index_type/</id> <content type="text/html" src="https://nihil.cc/posts/phantom_index_type/" /> <author> <name>Nichts Hsu</name> </author> <category term="杂记" /> <category term="Rust" /> <summary>幽灵索引类型 假设我们有一个这样的类型： #[derive(Debug, Clone, Copy)] struct Pair&amp;amp;lt;T, U&amp;amp;gt;(T, U); 并且，该类型保证 T 与 U 始终不会是相同的类型。那么，我们要如何设计一个统一的 get() 方法，使得下面的代码可以实现： let pair = Pair(1, &amp;quot;hello&amp;quot;); let first: i32 = pair.get(); // 得到 1 let second: &amp;amp;amp;&amp;#39;static str = pair.get(); // 得到 &amp;quot;hello&amp;quot; 不妨先停下来尝试一下再往下看。 有一种技术可以达成这个需求，它在 “frunk” 中被大量应用，被 “frunk” 称为幽灵索引类型（Phantom Index Type）。 具体来说，它大概长这样： stru...</summary> </entry> <entry><title>Rust 不透明类型上的生命周期</title><link href="https://nihil.cc/posts/lifetime_on_opaque_type/" rel="alternate" type="text/html" title="Rust 不透明类型上的生命周期" /><published>2024-02-18T18:24:47+08:00</published> <updated>2024-03-27T14:02:17+08:00</updated> <id>https://nihil.cc/posts/lifetime_on_opaque_type/</id> <content type="text/html" src="https://nihil.cc/posts/lifetime_on_opaque_type/" /> <author> <name>Nichts Hsu</name> </author> <category term="杂记" /> <category term="Rust" /> <summary>此文撰写于 Rust stable 1.76.0（即 nightly 1.78.0）版本，由于 nightly 特性不受到 Rust 开发团队的保证，请谨慎甄别本文内容是否仍然适用。 抛出问题 在最前面，我们首先抛出一个问题，为什么下面的代码无法编译？ use std::fmt::Debug; fn do_sth&amp;amp;lt;F, Ret&amp;amp;gt;(f: F) where F: Fn(&amp;amp;amp;i32) -&amp;amp;gt; Ret, Ret: Debug, { let a = 1; f(&amp;amp;amp;a); } fn foo(t: &amp;amp;amp;i32) -&amp;amp;gt; impl Debug + &amp;#39;_ { t } fn do_foo() { do_sth(foo); } 尝试编译得到报错： error[E0308]: mismatched ty...</summary> </entry> <entry><title>从另一个视角看 Rust HRTBs</title><link href="https://nihil.cc/posts/rust_hrtbs_essence/" rel="alternate" type="text/html" title="从另一个视角看 Rust HRTBs" /><published>2024-02-02T16:58:04+08:00</published> <updated>2024-04-18T18:07:09+08:00</updated> <id>https://nihil.cc/posts/rust_hrtbs_essence/</id> <content type="text/html" src="https://nihil.cc/posts/rust_hrtbs_essence/" /> <author> <name>Nichts Hsu</name> </author> <category term="杂记" /> <category term="Rust" /> <summary>HRTBs，即高阶 Trait 约束（Higher-Rank Trait Bounds），在 Rust 中是令很多初学者感到莫名其妙的概念，一手 for&amp;amp;lt;&amp;#39;a&amp;amp;gt; S&amp;amp;lt;&amp;#39;a&amp;amp;gt; 的语法更是使得原本就复杂的生命周期更加吓人。 但是，如果从另一个角度对 HRTBs 进行解剖，或许我们能看到不一样的东西。 首先，让我们考虑一个泛型和闭包的应用： use std::fmt::Display; fn print&amp;amp;lt;T, U&amp;amp;gt;(x: T, y: U) where T: Display, U: Display, { println!(&amp;quot;{x}, {y}&amp;quot;) } fn curry&amp;amp;lt;F, T, U, R&amp;amp;gt;(f: F, a: T) -&amp;amp;gt; impl Fn(U) -&amp;amp;gt; R where F: Fn(T, U) -&amp;amp;...</summary> </entry> <entry><title>于 Rust 1.76 稳定的 trait upcasting coercion</title><link href="https://nihil.cc/posts/rust_trait_upcasting/" rel="alternate" type="text/html" title="于 Rust 1.76 稳定的 trait upcasting coercion" /><published>2024-01-16T10:30:46+08:00</published> <updated>2024-01-16T10:30:46+08:00</updated> <id>https://nihil.cc/posts/rust_trait_upcasting/</id> <content type="text/html" src="https://nihil.cc/posts/rust_trait_upcasting/" /> <author> <name>Nichts Hsu</name> </author> <category term="教程" /> <category term="Rust" /> <summary>由于在 Rust 1.76 发布前夕，trait upcasting coercion 突然发现了健全性问题，因此该特性已被取消稳定，目前还没有下一步的稳定计划。 先提出一个问题，在 Rust 中如何把一个 &amp;amp;amp;dyn MyTrait 转换为 &amp;amp;amp;MyStruct？ trait MyTrait {} struct Foo; struct Bar; impl MyTrait for Foo {} impl MyTrait for Bar {} fn rtti(obj: &amp;amp;amp;dyn MyTrait) { // How to convert `obj` to `&amp;amp;amp;Foo` or `&amp;amp;amp;Bar` } 很自然地，我们可以想到借助 Any，rust 在 dyn Any 类型上实现了 is() 和 downcast_ref() 可以很好地帮助...</summary> </entry> <entry><title>[译] 改变 Rust 的规则</title><link href="https://nihil.cc/posts/translate_rust_rules/" rel="alternate" type="text/html" title="[译] 改变 Rust 的规则" /><published>2024-01-12T11:42:34+08:00</published> <updated>2024-01-12T11:42:34+08:00</updated> <id>https://nihil.cc/posts/translate_rust_rules/</id> <content type="text/html" src="https://nihil.cc/posts/translate_rust_rules/" /> <author> <name>withoutboats</name> </author> <category term="翻译" /> <category term="Rust" /> <summary>免责声明 本文是对原博文《Changing the rules of Rust》的无授权翻译转载，不享受任何著作权利，不用于任何商业目的，不以任何许可证进行授权，不对任何转载行为尤其是商业转载行为负责。一切权利均由原作者 “Without boats, dreams dry up” 保有。 本文中出现的所有第一人称均指代原作者 “Without boats, dreams dry up” 而非译者本人。本文中对一些术语会额外附加英文原文注释，以帮助读者搜索相关概念。 前言 在 Rust 中，某些关于什么是健全的（Sound）、什么是不健全的 API 决策会影响所有 Rust 代码。也就是说，若已经决策允许或不允许具有某些安全要求的类型，那么现在所有用户都要遵守该决策。他们甚至无法仅是使用一个具有不同规则的 API：所有 API 都必须符合这些规则。 这些规则是通过某些标记（...</summary> </entry> </feed>
