How Rust Changed Me As A Developer
本文为原文前 6,000 字符的节选翻译,完整内容请查看原文。
How Rust Changed Me As A Developer
How Rust Changed Me As A Developer By Martijn Faassen • 2026-11-16 • Tags: programming, rust How Rust Changed Me As A Developer The programming language you use affects how you think as a software developer 1. That may seem surprising to you; you may have picked up JavaScript pretty quickly knowing Python, and it didn’t really affect how you think or develop. That’s because those languages are pretty similar. The more different a programming language is from what you’re used to, the more it can change you. Rust was quite different to me, so it did.
你所使用的编程语言会影响你作为软件开发者的思维方式。这听起来可能让你感到惊讶;你可能在掌握 Python 后很快就学会了 JavaScript,而它并没有真正改变你的思维或开发方式。这是因为这些语言非常相似。一门编程语言与你习惯的语言差异越大,它就越能改变你。Rust 对我来说非常不同,所以它确实改变了我。
I’ve been developing with Rust for over 6 years; more than 4 years professionally now. One of the reasons I really wanted to become more proficient with Rust is because I knew it would challenge me, and hopefully change me for the better as a developer. I think it did. What I learned and am going to discuss here is not exclusive to Rust. You can encounter many of these concepts in other contexts, and indeed I had encountered quite a few of them already. But in this case, Rust helped bring them into better focus.
我使用 Rust 进行开发已经超过 6 年了;其中专业开发经验超过 4 年。我非常想精通 Rust 的原因之一是,我知道它会挑战我,并希望它能让我成为一名更好的开发者。我认为它确实做到了。我在这里学到并打算讨论的内容并非 Rust 所独有。你可以在其他语境中遇到许多这些概念,事实上我已经遇到过不少。但在这种情况下,Rust 帮助我更清晰地聚焦于这些概念。
Where were enums all my life? We’re all familiar with product types, though not usually under that name: struct Human { first_name: String, last_name: String, } The fields of the Human struct 2 can vary independently from each other, and we have to deal with all possible combinations: all possible first names and last names, combined. The product of their combinations, to to speak. But it turns out there’s another type thing that’s been around forever but that the programming community has mostly been been ignoring 3: the sum type.
枚举(enums)在我生命中缺失的那些年都去哪了?我们都熟悉积类型(product types),尽管通常不使用这个名称:struct Human { first_name: String, last_name: String, }。Human 结构体的字段可以独立变化,我们必须处理所有可能的组合:所有可能的名和姓的组合。可以说,这是它们组合的积。但事实证明,还有另一种存在已久却一直被编程社区所忽视的类型:和类型(sum type)。
A sum type expresses variants: something is either one thing, or another, out of a bunch of variants: enum Person { Human { first_name: String, last_name: String }, Cat { name: String, cuteness: Cuteness } } A Person is either a human or a cat: that’s the sum of the possiblities. Each variant can have data associated with it, and this can be a product type, but the Person type is just these options (two in this case). In code where you need to deal with a person, you need to handle each variant by match in on the object. The Rust compiler enforces this.
和类型表达的是变体:某样东西要么是这一种,要么是那一种,从一系列变体中选择:enum Person { Human { first_name: String, last_name: String }, Cat { name: String, cuteness: Cuteness } }。一个人要么是人类,要么是猫:这就是可能性的总和。每个变体都可以关联数据,这可以是积类型,但 Person 类型仅仅是这些选项(本例中为两个)。在需要处理人的代码中,你需要通过对对象进行匹配(match)来处理每个变体。Rust 编译器会强制执行这一点。
match some_person { Human { first_name, last_name } => { // do a thing with first_name and last_name }, Cat { name, cuteness } => { // do a thing with name and cutenes } } When you handle the human variant, you only need to worry about first_name and last_name. You don’t have to worry about name or cuteness at all. These cases are distinct from each other. It turns out that it’s really helpful to model stuff using sum types. People using functional programming languages already knew this. You can model optionality (null) this way: you got either some value, or nothing. You can also model errors: it’s either okay (with whatever value you return in case of success), or an error (with a value describing the error). Any language without sum types now feels impoverished to me. I know I can simulate it with some type field, but the job of a programming language is to help me handle basic stuff, and this is basic stuff.
match some_person { Human { first_name, last_name } => { // 对 first_name 和 last_name 做些处理 }, Cat { name, cuteness } => { // 对 name 和 cuteness 做些处理 } } 当你处理人类变体时,你只需要关心 first_name 和 last_name。你根本不需要担心 name 或 cuteness。这些情况彼此独立。事实证明,使用和类型来建模是非常有帮助的。使用函数式编程语言的人早就知道了这一点。你可以用这种方式建模可选性(null):你要么得到某个值,要么什么都没有。你也可以建模错误:要么是成功(带有成功时返回的任何值),要么是错误(带有描述错误的数值)。现在,任何没有和类型的语言对我来说都显得贫乏。我知道我可以用某种类型字段来模拟它,但编程语言的工作是帮助我处理基础事物,而这就是基础事物。
Enums and abstraction In an object oriented programming language, you handle variants using dynamic dispatch and an interface (or inheritance). You do this: interface Person { def do_one_thing() def do_another_thing } class Human implements Person { … } class Cat implements Person { … } The idea is that you can now use Person anywhere without worrying about the details inside: it’s loosely coupled. You only use their interface. This is indeed a great idea, but Rust taught me that this has tradeoffs I may not want to pay for. You pay two things: performance, and the need to create the interface.
枚举与抽象。在面向对象编程语言中,你使用动态分发和接口(或继承)来处理变体。你会这样做:interface Person { def do_one_thing() def do_another_thing } class Human implements Person { … } class Cat implements Person { … } 这种想法是,你现在可以在任何地方使用 Person 而不必担心内部细节:它是松耦合的。你只使用它们的接口。这确实是一个好主意,但 Rust 教会我,这有我可能不想付出的代价。你需要付出两样东西:性能,以及创建接口的需求。
Let’s talk about the need to create the interface. You need to devise an interface that actually makes sense for your application: how are you using humans and cats everywhere? Doing this work of abstraction may be extremely worthwhile to do, especially if you anticipate unknown future persons that you need to be able to plug in later without having to modify the code that uses them — you want your code use the open/closed principle. But what if you are developing a single domain, and inside that you just want to be able to handle cats and humans and that’s it? There’s just no need to plug in other types of persons, and certainly not from outside.
让我们谈谈创建接口的需求。你需要设计一个对你的应用程序真正有意义的接口:你在各处是如何使用人类和猫的?进行这种抽象工作可能非常有价值,特别是如果你预见到未来会有未知的“人”类型,你需要能够在以后插入它们而无需修改使用它们的代码——你希望你的代码遵循开闭原则。但如果你只是在开发一个单一领域,并且在其中你只想处理猫和人,仅此而已呢?根本没有必要插入其他类型的人,更不用说从外部插入了。
Or let’s say you do want to add a new variant once every while. Say you want to add Dog. Maybe you’re actually okay going through the code that handles Human and Cat and add handling for dogs too? Maybe it actually is helpful to see those other handlers right there too when you do so? The compiler is going to ensure you don’t miss any spots! Or what if the abstraction you are trying to devise is the variance and that’s more or less what you can say about it? For error handling, the abstraction is either okay with some value, or some error value. In this case you may be paying the price of creating an interface without it being worthwhile. enum may be all you actually need — low ceremony, and statically checked. Finally I’ll note that enums don’t lock you out from creating that abstraction - you can add methods to an enum too, just like on a struct. Enums made me a better programmer as now I have another elementary, simple, easy to understand language construct for abstraction in my mental toolbox.
或者假设你确实想偶尔添加一个新变体。比如你想添加 Dog。也许你其实并不介意检查处理 Human 和 Cat 的代码并添加对狗的处理?也许当你这样做时,能直接看到其他处理器实际上是有帮助的?编译器会确保你不会遗漏任何地方!或者,如果你试图设计的抽象仅仅是变体本身,而这就是你能说的全部内容呢?对于错误处理,抽象要么是带有某个值的成功,要么是某个错误值。在这种情况下,你可能在付出创建接口的代价,却得不到相应的回报。枚举可能就是你真正需要的全部——低仪式感,且经过静态检查。最后我要指出,枚举并不会阻止你创建抽象——你也可以像在结构体上一样为枚举添加方法。枚举使我成为了一名更好的程序员,因为现在我的思维工具箱中有了另一种基础、简单、易于理解的抽象语言结构。
Dispatch galore In many languages there are two types of dispatch: dynamic dispatch, which we’ve discussed in case of our Person interface, and conditional dispatch, like this: if something { do_this_thing() } else do_another() } Rust has dynamic dispatch, if you ask for it. It also has the powered up version of conditional dispatch I already discussed: match on enums. It has conditional dispatch too - that’s just [a special case][https://docs.rs/bool/latest/bool/enum.Boolean.html]. There’s another type of dispatch that Rust makes you think about: dispatch that’s statically compiled. What if you’re dealing either with
分发盛宴。在许多语言中,有两种类型的分发:动态分发(我们在 Person 接口案例中讨论过)和条件分发,像这样:if something { do_this_thing() } else do_another() }。如果你需要,Rust 拥有动态分发。它还拥有我之前讨论过的条件分发的增强版本:枚举上的 match。它也有条件分发——那只是[一种特殊情况][https://docs.rs/bool/latest/bool/enum.Boolean.html]。还有另一种 Rust 让你思考的分发类型:静态编译的分发。如果你正在处理……