x-i18n: generated_at: “2026-03-01T15:00:37Z” model: gemini-3-flash-preview provider: google-gemini-cli source_hash: f544ee583a8c59cfaad510536590be8723547c299c39b44a7bad22b7eb1acdea source_path: ch20-02-advanced-traits.md workflow: 16
高级特征 (Advanced Traits)
我们在第 10 章的“使用特征定义共享行为”部分首次介绍了特征,但我们没有讨论更高级的细节。既然你对 Rust 有了更多了解,我们可以深入研究一下。
We first covered traits in the “Defining Shared Behavior with Traits” section in Chapter 10, but we didn’t discuss the more advanced details. Now that you know more about Rust, we can get into the nitty-gritty.
使用关联类型定义特征 (Defining Traits with Associated Types)
“关联类型 (Associated types)”将类型占位符与特征连接起来,使得特征方法定义可以在其签名中使用这些占位符类型。特征的实现者将指定用于替换特定实现的占位符的具体类型。这样,我们可以定义一个使用某些类型的特征,而无需在实现该特征之前确切地知道这些类型是什么。
Associated types connect a type placeholder with a trait such that the trait method definitions can use these placeholder types in their signatures. The implementor of a trait will specify the concrete type to be used instead of the placeholder type for the particular implementation. That way, we can define a trait that uses some types without needing to know exactly what those types are until the trait is implemented.
我们已经将本章中的大多数高级特性描述为很少需要。关联类型处于中间位置:它们的使用比本书其余部分解释的特性要少,但比本章讨论的许多其他特性要多。
We’ve described most of the advanced features in this chapter as being rarely needed. Associated types are somewhere in the middle: They’re used more rarely than features explained in the rest of the book but more commonly than many of the other features discussed in this chapter.
一个带有关联类型的特征示例是标准库提供的 Iterator 特征。关联类型名为 Item ,它代表实现 Iterator 特征的类型正在迭代的值的类型。 Iterator 特征的定义如示例 20-13 所示。
One example of a trait with an associated type is the Iterator trait that the
standard library provides. The associated type is named Item and stands in
for the type of the values the type implementing the Iterator trait is
iterating over. The definition of the Iterator trait is as shown in Listing
20-13.
{{#rustdoc_include ../listings/ch20-advanced-features/listing-20-13/src/lib.rs}}
类型 Item 是一个占位符, next 方法的定义表明它将返回 Option<Self::Item> 类型的值。 Iterator 特征的实现者将为 Item 指定具体类型,并且 next 方法将返回一个包含该具体类型值的 Option 。
The type Item is a placeholder, and the next method’s definition shows that
it will return values of type Option<Self::Item>. Implementors of the
Iterator trait will specify the concrete type for Item, and the next
method will return an Option containing a value of that concrete type.
关联类型可能看起来与泛型类似,因为后者允许我们定义一个函数而不指定它可以处理哪些类型。为了研究这两个概念之间的区别,我们将看看在名为 Counter 的类型上实现的 Iterator 特征,该实现指定 Item 类型为 u32 :
Associated types might seem like a similar concept to generics, in that the
latter allow us to define a function without specifying what types it can
handle. To examine the difference between the two concepts, we’ll look at an
implementation of the Iterator trait on a type named Counter that specifies
the Item type is u32:
{{#rustdoc_include ../listings/ch20-advanced-features/no-listing-22-iterator-on-counter/src/lib.rs:ch19}}
这种语法似乎与泛型的语法相当。那么,为什么不直接用泛型来定义 Iterator 特征,如示例 20-14 所示呢?
This syntax seems comparable to that of generics. So, why not just define the
Iterator trait with generics, as shown in Listing 20-14?
{{#rustdoc_include ../listings/ch20-advanced-features/listing-20-14/src/lib.rs}}
区别在于,当使用泛型时(如示例 20-14),我们必须在每个实现中标注类型;因为我们还可以为 Counter 实现 Iterator<String> 或任何其他类型,所以我们可以为 Counter 实现多个 Iterator 。换句话说,当特征具有泛型参数时,它可以为同一个类型实现多次,每次更改泛型类型参数的具体类型。当我们在 Counter 上使用 next 方法时,我们将不得不提供类型注解,以指明我们想要使用哪个 Iterator 实现。
The difference is that when using generics, as in Listing 20-14, we must
annotate the types in each implementation; because we can also implement
Iterator<String> for Counter or any other type, we could have multiple
implementations of Iterator for Counter. In other words, when a trait has a
generic parameter, it can be implemented for a type multiple times, changing
the concrete types of the generic type parameters each time. When we use the
next method on Counter, we would have to provide type annotations to
indicate which implementation of Iterator we want to use.
使用关联类型,我们不需要标注类型,因为我们无法多次为一个类型实现同一个特征。在示例 20-13 的关联类型定义中,我们只能为 Item 的类型选择一次,因为只能有一个 impl Iterator for Counter 。我们不必在每个调用 Counter 上的 next 的地方都指定我们想要一个 u32 值的迭代器。
With associated types, we don’t need to annotate types, because we can’t
implement a trait on a type multiple times. In Listing 20-13 with the
definition that uses associated types, we can choose what the type of Item
will be only once because there can be only one impl Iterator for Counter. We
don’t have to specify that we want an iterator of u32 values everywhere we
call next on Counter.
关联类型也成为了特征合同的一部分:特征的实现者必须提供一个类型来填补关联类型的占位符。关联类型通常有一个描述该类型将如何使用的名称,并在 API 文档中记录关联类型是一个良好的实践。
Associated types also become part of the trait’s contract: Implementors of the trait must provide a type to stand in for the associated type placeholder. Associated types often have a name that describes how the type will be used, and documenting the associated type in the API documentation is a good practice.
使用默认泛型参数和运算符重载 (Using Default Generic Parameters and Operator Overloading)
当我们使用泛型类型参数时,我们可以为泛型类型指定一个默认的具体类型。这消除了特征实现者在默认类型可行时指定具体类型的需要。你在使用 <PlaceholderType=ConcreteType> 语法声明泛型类型时指定默认类型。
When we use generic type parameters, we can specify a default concrete type for
the generic type. This eliminates the need for implementors of the trait to
specify a concrete type if the default type works. You specify a default type
when declaring a generic type with the <PlaceholderType=ConcreteType> syntax.
这种技术非常有用的一个绝佳例子是“运算符重载 (operator overloading)”,即你在特定情况下自定义运算符(如 + )的行为。
A great example of a situation where this technique is useful is with operator
overloading, in which you customize the behavior of an operator (such as +)
in particular situations.
Rust 不允许你创建自己的运算符或重载任意运算符。但你可以通过实现与运算符关联的特征来重载 std::ops 中列出的操作和相应的特征。例如,在示例 20-15 中,我们重载了 + 运算符以将两个 Point 实例相加。我们通过在 Point 结构体上实现 Add 特征来做到这一点。
Rust doesn’t allow you to create your own operators or overload arbitrary
operators. But you can overload the operations and corresponding traits listed
in std::ops by implementing the traits associated with the operator. For
example, in Listing 20-15, we overload the + operator to add two Point
instances together. We do this by implementing the Add trait on a Point
struct.
#![allow(unused)]
fn main() {
{{#rustdoc_include ../listings/ch20-advanced-features/listing-20-15/src/main.rs}}
}
add 方法将两个 Point 实例的 x 值相加,以及两个 Point 实例的 y 值相加,以创建一个新的 Point 。 Add 特征具有一个名为 Output 的关联类型,它决定了从 add 方法返回的类型。
The add method adds the x values of two Point instances and the y
values of two Point instances to create a new Point. The Add trait has an
associated type named Output that determines the type returned from the add
method.
此代码中的默认泛型类型位于 Add 特征内。以下是其定义:
The default generic type in this code is within the Add trait. Here is its
definition:
#![allow(unused)]
fn main() {
trait Add<Rhs=Self> {
type Output;
fn add(self, rhs: Rhs) -> Self::Output;
}
}
这段代码看起来应该大致熟悉:一个带有一个方法和一个关联类型的特征。新部分是 Rhs=Self :这种语法被称为“默认类型参数 (default type parameters)”。 Rhs 泛型类型参数(“right-hand side” 的缩写)定义了 add 方法中 rhs 参数的类型。如果我们实现 Add 特征时没有为 Rhs 指定具体类型, Rhs 的类型将默认为 Self ,即我们正在实现 Add 的那个类型。
This code should look generally familiar: a trait with one method and an
associated type. The new part is Rhs=Self: This syntax is called default
type parameters. The Rhs generic type parameter (short for “right-hand
side”) defines the type of the rhs parameter in the add method. If we don’t
specify a concrete type for Rhs when we implement the Add trait, the type
of Rhs will default to Self, which will be the type we’re implementing
Add on.
当我们为 Point 实现 Add 时,我们使用了 Rhs 的默认值,因为我们想将两个 Point 实例相加。让我们看一个实现 Add 特征的例子,其中我们想要自定义 Rhs 类型而不是使用默认值。
When we implemented Add for Point, we used the default for Rhs because we
wanted to add two Point instances. Let’s look at an example of implementing
the Add trait where we want to customize the Rhs type rather than using the
default.
我们有两个结构体, Millimeters 和 Meters ,持有不同单位的值。这种将现有类型薄薄地包装在另一个结构体中的做法被称为“newtype 模式”,我们将在“使用 newtype 模式实现外部特征”一节中进行更详细的描述。我们想要将以毫米为单位的值加到以米为单位的值上,并让 Add 的实现正确执行转换。我们可以为 Millimeters 实现 Add 并将 Meters 作为 Rhs ,如示例 20-16 所示。
We have two structs, Millimeters and Meters, holding values in different
units. This thin wrapping of an existing type in another struct is known as the
newtype pattern, which we describe in more detail in the “Implementing
External Traits with the Newtype Pattern” section. We
want to add values in millimeters to values in meters and have the
implementation of Add do the conversion correctly. We can implement Add for
Millimeters with Meters as the Rhs, as shown in Listing 20-16.
{{#rustdoc_include ../listings/ch20-advanced-features/listing-20-16/src/lib.rs}}
为了将 Millimeters 和 Meters 相加,我们指定 impl Add<Meters> 来设置 Rhs 类型参数的值,而不是使用默认的 Self 。
To add Millimeters and Meters, we specify impl Add<Meters> to set the
value of the Rhs type parameter instead of using the default of Self.
你将主要以两种方式使用默认类型参数:
- 扩展一个类型而不破坏现有代码
- 在大多数用户不需要的特定情况下允许定制
You’ll use default type parameters in two main ways:
- To extend a type without breaking existing code
- To allow customization in specific cases most users won’t need
标准库的 Add 特征是第二个目的的示例:通常情况下,你会将两个相似的类型相加,但 Add 特征提供了除此之外的定制能力。在 Add 特征定义中使用默认类型参数意味着你大多数时候不必指定额外的参数。换句话说,不需要一点实现样板,使得特征更容易使用。
The standard library’s Add trait is an example of the second purpose:
Usually, you’ll add two like types, but the Add trait provides the ability to
customize beyond that. Using a default type parameter in the Add trait
definition means you don’t have to specify the extra parameter most of the
time. In other words, a bit of implementation boilerplate isn’t needed, making
it easier to use the trait.
第一个目的与第二个类似但相反:如果你想向现有特征添加类型参数,你可以给它一个默认值,以允许扩展该特征的功能而不破坏现有的实现代码。
The first purpose is similar to the second but in reverse: If you want to add a type parameter to an existing trait, you can give it a default to allow extension of the functionality of the trait without breaking the existing implementation code.
消除同名方法之间的歧义 (Disambiguating Between Identically Named Methods)
Disambiguating Between Identically Named Methods
Rust 并不禁止一个特征拥有与另一个特征的方法同名的方法,也不禁止你在一个类型上实现这两个特征。直接在类型上实现与特征方法同名的方法也是可能的。
Nothing in Rust prevents a trait from having a method with the same name as another trait’s method, nor does Rust prevent you from implementing both traits on one type. It’s also possible to implement a method directly on the type with the same name as methods from traits.
当调用具有相同名称的方法时,你需要告诉 Rust 你想使用哪一个。考虑示例 20-17 中的代码,我们定义了两个特征 Pilot 和 Wizard ,它们都带有一个名为 fly 的方法。然后我们在一个已经实现了名为 fly 的方法的 Human 类型上实现了这两个特征。每个 fly 方法执行不同的操作。
When calling methods with the same name, you’ll need to tell Rust which one you
want to use. Consider the code in Listing 20-17 where we’ve defined two traits,
Pilot and Wizard, that both have a method called fly. We then implement
both traits on a type Human that already has a method named fly implemented
on it. Each fly method does something different.
#![allow(unused)]
fn main() {
{{#rustdoc_include ../listings/ch20-advanced-features/listing-20-17/src/main.rs:here}}
}
当我们在 Human 实例上调用 fly 时,编译器默认调用直接在类型上实现的方法,如示例 20-18 所示。
When we call fly on an instance of Human, the compiler defaults to calling
the method that is directly implemented on the type, as shown in Listing 20-18.
#![allow(unused)]
fn main() {
{{#rustdoc_include ../listings/ch20-advanced-features/listing-20-18/src/main.rs:here}}
}
运行这段代码将打印 *waving arms furiously* ,表明 Rust 直接调用了在 Human 上实现的方法。
Running this code will print *waving arms furiously*, showing that Rust
called the fly method implemented on Human directly.
要从 Pilot 特征或 Wizard 特征中调用 fly 方法,我们需要使用更显式的语法来指定我们要的是哪一个 fly 方法。示例 20-19 演示了这种语法。
To call the fly methods from either the Pilot trait or the Wizard trait,
we need to use more explicit syntax to specify which fly method we mean.
Listing 20-19 demonstrates this syntax.
#![allow(unused)]
fn main() {
{{#rustdoc_include ../listings/ch20-advanced-features/listing-20-19/src/main.rs:here}}
}
在方法名称之前指定特征名称向 Rust 澄清了我们要调用哪个 fly 实现。我们也可以写成 Human::fly(&person) ,这与我们在示例 20-19 中使用的 person.fly() 是等效的,但如果不需要消除歧义,这种写法写起来有点长。
Specifying the trait name before the method name clarifies to Rust which
implementation of fly we want to call. We could also write
Human::fly(&person), which is equivalent to the person.fly() that we used
in Listing 20-19, but this is a bit longer to write if we don’t need to
disambiguate.
运行这段代码会打印以下内容:
{{#include ../listings/ch20-advanced-features/listing-20-19/output.txt}}
因为 fly 方法接收一个 self 参数,如果我们有两个都实现了“同一个特征”的“类型”,Rust 可以根据 self 的类型弄清楚该使用特征的哪个实现。
Because the fly method takes a self parameter, if we had two types that
both implement one trait, Rust could figure out which implementation of a
trait to use based on the type of self.
然而,非方法的关联函数没有 self 参数。当有多个类型或特征定义了具有相同函数名的非方法函数时,除非你使用“完全限定语法 (fully qualified syntax)”,否则 Rust 并不总是知道你指的是哪种类型。例如,在示例 20-20 中,我们为一个动物收容所创建了一个特征,该收容所希望给所有幼犬命名为 Spot。我们创建了一个带有非方法关联函数 baby_name 的 Animal 特征。该 Animal 特征在 Dog 结构体上实现,我们也直接在 Dog 上提供了一个非方法关联函数 baby_name 。
However, associated functions that are not methods don’t have a self
parameter. When there are multiple types or traits that define non-method
functions with the same function name, Rust doesn’t always know which type you
mean unless you use fully qualified syntax. For example, in Listing 20-20, we
create a trait for an animal shelter that wants to name all baby dogs Spot. We
make an Animal trait with an associated non-method function baby_name. The
Animal trait is implemented for the struct Dog, on which we also provide an
associated non-method function baby_name directly.
#![allow(unused)]
fn main() {
{{#rustdoc_include ../listings/ch20-advanced-features/listing-20-20/src/main.rs}}
}
我们在定义于 Dog 上的 baby_name 关联函数中实现了给所有小狗命名为 Spot 的代码。 Dog 类型也实现了 Animal 特征,该特征描述了所有动物都具有的特征。幼犬被称为 puppies,这在 Dog 上的 Animal 特征实现的 baby_name 函数(与 Animal 特征关联)中有所体现。
We implement the code for naming all puppies Spot in the baby_name associated
function that is defined on Dog. The Dog type also implements the trait
Animal, which describes characteristics that all animals have. Baby dogs are
called puppies, and that is expressed in the implementation of the Animal
trait on Dog in the baby_name function associated with the Animal trait.
在 main 中,我们调用 Dog::baby_name 函数,它直接调用定义在 Dog 上的关联函数。这段代码打印以下内容:
{{#include ../listings/ch20-advanced-features/listing-20-20/output.txt}}
此输出并不是我们想要的。我们想要调用作为我们在 Dog 上实现的 Animal 特征一部分的 baby_name 函数,以便代码打印出 A baby dog is called a puppy 。我们在示例 20-19 中使用的指定特征名称的技术在这里没有帮助;如果我们把 main 改为示例 20-21 中的代码,我们将得到一个编译错误。
This output isn’t what we wanted. We want to call the baby_name function that
is part of the Animal trait that we implemented on Dog so that the code
prints A baby dog is called a puppy. The technique of specifying the trait
name that we used in Listing 20-19 doesn’t help here; if we change main to
the code in Listing 20-21, we’ll get a compilation error.
{{#rustdoc_include ../listings/ch20-advanced-features/listing-20-21/src/main.rs:here}}
因为 Animal::baby_name 没有 self 参数,并且可能有其他类型实现了 Animal 特征,Rust 无法弄清楚我们要的是哪一个 Animal::baby_name 的实现。我们将得到这个编译器错误:
{{#include ../listings/ch20-advanced-features/listing-20-21/output.txt}}
为了消除歧义并告诉 Rust 我们想要使用针对 Dog 的 Animal 实现(而不是针对其他某种类型的 Animal 实现),我们需要使用完全限定语法。示例 20-22 演示了如何使用完全限定语法。
To disambiguate and tell Rust that we want to use the implementation of
Animal for Dog as opposed to the implementation of Animal for some other
type, we need to use fully qualified syntax. Listing 20-22 demonstrates how to
use fully qualified syntax.
#![allow(unused)]
fn main() {
{{#rustdoc_include ../listings/ch20-advanced-features/listing-20-22/src/main.rs:here}}
}
我们在尖括号内向 Rust 提供了一个类型标注,这表明我们想调用针对 Dog 实现的 Animal 特征中的 baby_name 方法,通过声明我们想在此函数调用中将 Dog 类型视为 Animal 。这段代码现在将打印出我们想要的内容:
We’re providing Rust with a type annotation within the angle brackets, which
indicates we want to call the baby_name method from the Animal trait as
implemented on Dog by saying that we want to treat the Dog type as an
Animal for this function call. This code will now print what we want:
{{#include ../listings/ch20-advanced-features/listing-20-22/output.txt}}
通常,完全限定语法的定义如下:
In general, fully qualified syntax is defined as follows:
<Type as Trait>::function(receiver_if_method, next_arg, ...);
对于不是方法的关联函数,将没有 receiver(接收者):只有其他参数列表。你可以在调用函数或方法的任何地方使用完全限定语法。然而,你被允许省略该语法的任何部分,只要 Rust 可以从程序中的其他信息中推断出来。只有在存在多个同名实现且 Rust 需要帮助来识别你想要调用的实现时,你才需要使用这种更冗长的语法。
For associated functions that aren’t methods, there would not be a receiver:
There would only be the list of other arguments. You could use fully qualified
syntax everywhere that you call functions or methods. However, you’re allowed
to omit any part of this syntax that Rust can figure out from other information
in the program. You only need to use this more verbose syntax in cases where
there are multiple implementations that use the same name and Rust needs help
to identify which implementation you want to call.
使用父特征 (Supertraits)
Using Supertraits
有时你可能会编写一个依赖于另一个特征的特征定义:为了让一个类型实现第一个特征,你希望要求该类型同时也实现第二个特征。你会这样做是为了让你的特征定义可以利用第二个特征的关联项。你的特征定义所依赖的那个特征被称为该特征的“父特征 (supertrait)”。
Sometimes you might write a trait definition that depends on another trait: For a type to implement the first trait, you want to require that type to also implement the second trait. You would do this so that your trait definition can make use of the associated items of the second trait. The trait your trait definition is relying on is called a supertrait of your trait.
例如,假设我们想制作一个带有 outline_print 方法的 OutlinePrint 特征,该方法将打印一个给定的值,其格式是被括在星号框中。也就是说,给定一个实现了标准库 Display 特征从而得出 (x, y) 的 Point 结构体,当我们对一个 x 为 1 且 y 为 3 的 Point 实例调用 outline_print 时,它应该打印以下内容:
For example, let’s say we want to make an OutlinePrint trait with an
outline_print method that will print a given value formatted so that it’s
framed in asterisks. That is, given a Point struct that implements the
standard library trait Display to result in (x, y), when we call
outline_print on a Point instance that has 1 for x and 3 for y, it
should print the following:
**********
* *
* (1, 3) *
* *
**********
在 outline_print 方法的实现中,我们想要使用 Display 特征的功能。因此,我们需要指定 OutlinePrint 特征仅适用于同样实现了 Display 且能提供 OutlinePrint 所需功能的类型。我们可以在特征定义中通过指定 OutlinePrint: Display 来做到这一点。这种技术类似于向特征添加特征约束。示例 20-23 展示了 OutlinePrint 特征的一个实现。
In the implementation of the outline_print method, we want to use the
Display trait’s functionality. Therefore, we need to specify that the
OutlinePrint trait will work only for types that also implement Display and
provide the functionality that OutlinePrint needs. We can do that in the
trait definition by specifying OutlinePrint: Display. This technique is
similar to adding a trait bound to the trait. Listing 20-23 shows an
implementation of the OutlinePrint trait.
#![allow(unused)]
fn main() {
{{#rustdoc_include ../listings/ch20-advanced-features/listing-20-23/src/main.rs:here}}
}
因为我们已经指定 OutlinePrint 需要 Display 特征,所以我们可以使用为任何实现了 Display 的类型自动实现的 to_string 函数。如果我们尝试在不添加冒号并在特征名称后指定 Display 特征的情况下使用 to_string ,我们将得到一个错误,指出在当前作用域内未找到类型 &Self 的名为 to_string 的方法。
Because we’ve specified that OutlinePrint requires the Display trait, we
can use the to_string function that is automatically implemented for any type
that implements Display. If we tried to use to_string without adding a
colon and specifying the Display trait after the trait name, we’d get an
error saying that no method named to_string was found for the type &Self in
the current scope.
让我们看看当我们尝试在一个未实现 Display 的类型(如 Point 结构体)上实现 OutlinePrint 时会发生什么:
Let’s see what happens when we try to implement OutlinePrint on a type that
doesn’t implement Display, such as the Point struct:
{{#rustdoc_include ../listings/ch20-advanced-features/no-listing-02-impl-outlineprint-for-point/src/main.rs:here}}
我们得到了一个错误,指出 Display 是必需的但未实现:
We get an error saying that Display is required but not implemented:
{{#include ../listings/ch20-advanced-features/no-listing-02-impl-outlineprint-for-point/output.txt}}
要修复此问题,我们在 Point 上实现 Display 并满足 OutlinePrint 要求的约束,如下所示:
To fix this, we implement Display on Point and satisfy the constraint that
OutlinePrint requires, like so:
#![allow(unused)]
fn main() {
{{#rustdoc_include ../listings/ch20-advanced-features/no-listing-03-impl-display-for-point/src/main.rs:here}}
}
然后,为 Point 实现 OutlinePrint 特征将可以成功编译,并且我们可以在 Point 实例上调用 outline_print 以在星号轮廓内显示它。
Then, implementing the OutlinePrint trait on Point will compile
successfully, and we can call outline_print on a Point instance to display
it within an outline of asterisks.
使用 newtype 模式实现外部特征 (Implementing External Traits with the Newtype Pattern)
Implementing External Traits with the Newtype Pattern
在第 10 章的“在类型上实现特征”一节中,我们提到了孤儿规则,该规则规定只有当特征或类型(或两者)对我们的 crate 是本地的时,我们才被允许在类型上实现该特征。可以使用 newtype 模式绕过此限制,该模式涉及在元组结构体中创建一个新类型。(我们在第 5 章的“使用元组结构体创建不同的类型”部分介绍过元组结构体。)元组结构体将有一个字段,并作为我们想要为其实现特征的类型的薄包装。然后,该包装器类型对我们的 crate 是本地的,我们就可以在包装器上实现该特征了。“Newtype” 是一个起源于 Haskell 编程语言的术语。使用此模式不会产生运行时性能损失,并且包装器类型在编译时会被消除。
In the “Implementing a Trait on a Type” section in Chapter 10, we mentioned the orphan rule that states we’re only allowed to implement a trait on a type if either the trait or the type, or both, are local to our crate. It’s possible to get around this restriction using the newtype pattern, which involves creating a new type in a tuple struct. (We covered tuple structs in the “Creating Different Types with Tuple Structs” section in Chapter 5.) The tuple struct will have one field and be a thin wrapper around the type for which we want to implement a trait. Then, the wrapper type is local to our crate, and we can implement the trait on the wrapper. Newtype is a term that originates from the Haskell programming language. There is no runtime performance penalty for using this pattern, and the wrapper type is elided at compile time.
作为一个例子,假设我们想在 Vec<T> 上实现 Display ,孤儿规则阻止我们直接这样做,因为 Display 特征和 Vec<T> 类型都定义在我们的 crate 之外。我们可以创建一个持有 Vec<T> 实例的 Wrapper 结构体;然后,我们就可以在 Wrapper 上实现 Display 并使用 Vec<T> 的值,如示例 20-24 所示。
As an example, let’s say we want to implement Display on Vec<T>, which the
orphan rule prevents us from doing directly because the Display trait and the
Vec<T> type are defined outside our crate. We can make a Wrapper struct
that holds an instance of Vec<T>; then, we can implement Display on
Wrapper and use the Vec<T> value, as shown in Listing 20-24.
#![allow(unused)]
fn main() {
{{#rustdoc_include ../listings/ch20-advanced-features/listing-20-24/src/main.rs}}
}
Display 的实现使用 self.0 来访问内部的 Vec<T> ,因为 Wrapper 是一个元组结构体,而 Vec<T> 是元组中索引为 0 的项。然后,我们就可以在 Wrapper 上使用 Display 特征的功能了。
The implementation of Display uses self.0 to access the inner Vec<T>
because Wrapper is a tuple struct and Vec<T> is the item at index 0 in the
tuple. Then, we can use the functionality of the Display trait on Wrapper.
使用这种技术的缺点是,由于 Wrapper 是一个新类型,它没有它所持有值的方法。我们将不得不直接在 Wrapper 上实现 Vec<T> 的所有方法,并使这些方法委托给 self.0 ,这将允许我们将 Wrapper 完全视为 Vec<T> 。如果我们希望新类型具有内部类型的每一个方法,那么在 Wrapper 上实现 Deref 特征以返回内部类型会是一个解决方案(我们在第 15 章“像普通引用一样处理智能指针”一节中讨论过实现 Deref 特征)。如果我们不希望 Wrapper 类型具有内部类型的所有方法(例如,为了限制 Wrapper 类型的行为),我们就必须手动实现我们确实想要的方法。
The downside of using this technique is that Wrapper is a new type, so it
doesn’t have the methods of the value it’s holding. We would have to implement
all the methods of Vec<T> directly on Wrapper such that the methods
delegate to self.0, which would allow us to treat Wrapper exactly like a
Vec<T>. If we wanted the new type to have every method the inner type has,
implementing the Deref trait on the Wrapper to return the inner type would
be a solution (we discussed implementing the Deref trait in the “Treating
Smart Pointers Like Regular References”
section in Chapter 15). If we didn’t want the Wrapper type to have all the
methods of the inner type—for example, to restrict the Wrapper type’s
behavior—we would have to implement just the methods we do want manually.
即使不涉及特征,这种 newtype 模式也是有用的。让我们切换焦点,看看与 Rust 类型系统交互的一些高级方式。
This newtype pattern is also useful even when traits are not involved. Let’s switch focus and look at some advanced ways to interact with Rust’s type system.