# impl\<T\> Drop for List\<T\> why need two T

**URL:** <https://internals.rust-lang.org/t/impl-t-drop-for-list-t-why-need-two-t/18479>\
**Category:** language design\
**Created:** [March 6, 2023, 3:01pm UTC](https://internals.rust-lang.org/t/impl-t-drop-for-list-t-why-need-two-t/18479 "2023-03-06T15:01:14Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![SevenThrees](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/seventhrees/32/10631_2.png) [@SevenThrees](https://internals.rust-lang.org/u/SevenThrees)\
**Post date:** [March 6, 2023, 3:01pm UTC](https://internals.rust-lang.org/t/impl-t-drop-for-list-t-why-need-two-t/18479/1 "2023-03-06T15:01:14Z")

</div>

在实现泛型功能的时候为什么需要传入两个 泛型标记，如果只是知道结构内存大小、结构名称以及函数名的具体实现一个T不就可以了？ Why do we need to pass in two generic tags when implementing the generic function? If we only know the structure memory size, structure name and the specific implementation of function name, a T is OK?

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/steffahn/32/13288_2.png) [@steffahn](https://internals.rust-lang.org/u/steffahn)\
**Post date:** [March 6, 2023, 3:24pm UTC](https://internals.rust-lang.org/t/impl-t-drop-for-list-t-why-need-two-t/18479/2 "2023-03-06T15:24:22Z")

</div>

If your question is why the Rust syntax

```rust
impl<T> Drop for List<T> {
    fn drop(&mut self) { … }
}

```

contains the letter `T` twice, namely

```plaintext
impl<T> Drop for List<T> {
     ^ ^
     | |
     +----------------+-- here 

```

that’s explained easily. The first `T` is _introducing_ the generic type parameter, and the second `T` is _using_ it.

The situation is comparable to why

```rust
fn f(x: i32) -> i32 { x + 1 }

```

contains the letter `x` twice, namely

```plaintext
fn f(x: i32) -> i32 { x + 1 }
     ^ ^
     | |
     +----------------+-- here 

```

In a plain English interpretation of the syntax, you could read these examples as something like

> “Define the function `f` to, given any integer `x`, return the integer `x + 1`”.

and

> For every type `T`, implement the trait `Drop` for the type `List<T>`.

Each of these plain English sentences also mention the letter `x` or `T` respectively twice. Once to introduce and explain what `x` or `T` is supposed to be, and once to make sensible use of the previously introduced variable.

Also note, that the choice of name of these variables is irrelevant. The fact that they’re single-letter names is irrelevant, and any name could have been chosen. Even names of other existing types would be permitted, albeit confusing, e.g.

```rust
struct Foo<T>(T);

impl<String> Drop for Foo<String> {
    fn drop(&mut self) {}
}

```

^^^ this has nothing to do with the standard library type `std::string::String`

Using the name only once would have a totally different meaning, and

```rust
impl Drop for Foo<String> {
    fn drop(&mut self) {}
}

```

would be writing the trait implementation only for the type `Foo<std::string::String>`, although in case of the special trait `Drop`, such an implementation would actually [be rejected](https://doc.rust-lang.org/stable/error_codes/E0366.html).

* * *

By the way, please note that questions like this are a better fit for [users.rust-lang.org](http://users.rust-lang.org). The [internals.rust-lang.org](http://internals.rust-lang.org) forum is mainly concerned with discussing/proposing things like language design, compiler implementation, or standard library API or implementation.

---

<div class="post-metadata">

**Author:** ![SevenThrees](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/seventhrees/32/10631_2.png) [@SevenThrees](https://internals.rust-lang.org/u/SevenThrees)\
**Post date:** [March 6, 2023, 3:59pm UTC](https://internals.rust-lang.org/t/impl-t-drop-for-list-t-why-need-two-t/18479/3 "2023-03-06T15:59:05Z")

</div>

OK, this is my first time to enter the community and ask questions, ^~^! Next time I will ask such questions at [users.trust-lang.org](http://users.trust-lang.org). Can you recommend a Rust book like C++\<(Inside the C++Object Model\>) to me? You just mean definition and use or declaration and use. I want to know more details. Why should we declare it this way, instead of making a generic T like C++, and how does Rust use T.

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/steffahn/32/13288_2.png) [@steffahn](https://internals.rust-lang.org/u/steffahn)\
**Post date:** [March 6, 2023, 4:14pm UTC](https://internals.rust-lang.org/t/impl-t-drop-for-list-t-why-need-two-t/18479/4 "2023-03-06T16:14:20Z")

</div>

For learning Rust, the most commonly recommended learning resource to learn the basics is the book “The Rust Programming Language” [https://doc.rust-lang.org/book](https://doc.rust-lang.org/book); there’s also a community-translated Chinese version [here](https://kaisery.github.io/trpl-zh-cn/).

The relevant chapter talking about generics is [chapter 10](https://doc.rust-lang.org/stable/book/ch10-00-generics.html) ([Chinese version](https://kaisery.github.io/trpl-zh-cn/ch10-00-generics.html)).

---

<div class="post-metadata">

**Author:** ![zirconium-n](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/zirconium-n/32/9078_2.png) [@zirconium-n](https://internals.rust-lang.org/u/zirconium-n)\
**Post date:** [March 6, 2023, 4:16pm UTC](https://internals.rust-lang.org/t/impl-t-drop-for-list-t-why-need-two-t/18479/5 "2023-03-06T16:16:23Z")

</div>

我觉得 @steffahn 的回答已经足够好了。我建议你仔细再读一遍。

C++里面类的定义和成员函数必须同时声明, 所以没这个需求。但是Rust里结构体的定义和成员函数的定义(`impl`块) 是分开的, 所以你需要消歧义。

或者具体来说, 你需要区分这两个情况:

```rust
struct T;
impl Foo<T> {} // #1
impl<T> Foo<T> {} // #2

```

TR:

I think @steffahn's answer is good enough. I suggest you read it again carefully.

Class definitions and member functions in C++ must be declared at the same time, so there is no such requirement. But in Rust the definition of the struct is separate from the definition of member functions (impl blocks), so you need disambiguation.

Or specifically, you need to differentiate between these two cases:

...

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/steffahn/32/13288_2.png) [@steffahn](https://internals.rust-lang.org/u/steffahn)\
**Post date:** [March 6, 2023, 4:29pm UTC](https://internals.rust-lang.org/t/impl-t-drop-for-list-t-why-need-two-t/18479/6 "2023-03-06T16:29:21Z")

</div>

I’m not too deeply familiar with C++, however the most similar language feature, C++ template, also features syntax where type parameters are introduced in one place and then used in another place. For example

```rust
template <typename T>
void my_drop_function(Foo<T>& this) { … }

```

introduces `T` in the first line and uses it in the second line.

Free standing functions in Rust work similarly syntax-wise, e.g. the above could be something as follows in Rust

```rust
fn my_drop_function<T>(this: &mut Foo<T>) { … }

```

* * *

Now, `impl` blocks and associated items are a feature a bit more unique to Rust (especially in how the syntax looks and how they are separate from `class` declarations) but essentially, a trait implementation like

```rust
impl<T> Drop for Foo<T> {
    fn drop(self: &mut Foo<T>) { … }
    // `self: &mut Foo<T>` here is a more explicit equivalent
    // alternative to writing `self: &mut Self` or just `&mut self` 
}

```

is somewhat comparable to a definition of a free-standing `drop` function, in particular in that the `T` parameter used in the `self` argument needs to be introduced _somewhere_. Just now there’s _two_ places where generic arguments can be introduced. One place is for the whole `impl` block, and the other place is on the individual function/method itself. The type arguments on the `impl` itself also play a role in trait resolution, and type arguments used in the `Self`-type need to be introduced on this outer level.

The reason why a struct definition syntax (and similarly a C++ templated class definition) can get away with _not_ needing to type out type parameters twice is because it’s less flexible. If you define a struct in Rust

```rust
struct Foo<T> {
    field1: Option<T>,
    field2: (u32, Box<T>),
}

```

is mostly simply just because the `struct` definition is _always_ fully generic over every possible type given as parameter, whereas `impl` blocks can be more restrictive. For a `struct Foo<A, B>(…);`, an impl block _can_ be fully generic

```rust
impl<A, B> Foo<A, B> { … }
// or
impl<A, B> SomeTrait for Foo<A, B> { … }

```

in which case, I _do_ admit the syntax looks slightly repetitive (though one gets used to it), but this explicitness allows more flexibility; we can restrict the `impl` if we want, e.g. to only a concrete type

```rust
impl Foo<String, bool> { … }

```

or only to instances where both types are the same

```rust
impl<A> Foo<A, A> { … }

```

or any other arbitrarily complex situation, e.g.

```rust
impl<X, Y, Z> Foo<Bar<X, Z>, Baz<Y, X>> { … }

```

---

<div class="post-metadata">

**Author:** ![CAD97](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cad97/32/3460_2.png) [@CAD97](https://internals.rust-lang.org/u/CAD97)\
**Post date:** [March 6, 2023, 6:25pm UTC](https://internals.rust-lang.org/t/impl-t-drop-for-list-t-why-need-two-t/18479/7 "2023-03-06T18:25:24Z")

</div>

> [@steffahn](#):
>
> I’m not too deeply familiar with C++, however the most similar language feature, C++ template, also features syntax where type parameters are introduced in one place and then used in another place.

Actually, C++ is a bit lax here. If you have something like

```cpp
template<typename T>
class Foo {
    bool operator+=(Foo other);
}

```

you don't need to provide template arguments to the current class type, and if you don't, it's implicitly understood to use the same template arguments as the primary template. The closest analogue to implementing a `trait` for some type in C++ would likely be adding a template specialization, e.g.

```cpp
template<typename T>
void std::swap(Foo<T>& x, Foo<T>& y);

```

which does require repeating the template parameters.

---

<div class="post-metadata">

**Author:** ![SevenThrees](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/seventhrees/32/10631_2.png) [@SevenThrees](https://internals.rust-lang.org/u/SevenThrees)\
**Post date:** [March 7, 2023, 12:50am UTC](https://internals.rust-lang.org/t/impl-t-drop-for-list-t-why-need-two-t/18479/9 "2023-03-07T00:50:49Z")

</div>

> [@steffahn](#):
>
> s _always_ fully generic over every possibl

理解了 👍

---

<div class="post-metadata">

**Author:** ![SevenThrees](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/seventhrees/32/10631_2.png) [@SevenThrees](https://internals.rust-lang.org/u/SevenThrees)\
**Post date:** [March 7, 2023, 12:51am UTC](https://internals.rust-lang.org/t/impl-t-drop-for-list-t-why-need-two-t/18479/10 "2023-03-07T00:51:36Z")

</div>

👍

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [June 5, 2023, 12:52am UTC](https://internals.rust-lang.org/t/impl-t-drop-for-list-t-why-need-two-t/18479/11 "2023-06-05T00:52:08Z")

</div>

Closed on June 5, 2023, 12:52am UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
