# Default trait implementation and PhantomData

**URL:** <https://internals.rust-lang.org/t/default-trait-implementation-and-phantomdata/7429>\
**Category:** compiler\
**Created:** [May 1, 2018, 4:53am UTC](https://internals.rust-lang.org/t/default-trait-implementation-and-phantomdata/7429 "2018-05-01T04:53:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mersinvald](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/mersinvald/32/4083_2.png) [@mersinvald](https://internals.rust-lang.org/u/mersinvald)\
**Post date:** [May 1, 2018, 4:53am UTC](https://internals.rust-lang.org/t/default-trait-implementation-and-phantomdata/7429/1 "2018-05-01T04:53:21Z")

</div>

Not sure if it is the right place to ask, but I’ll try 🙂

I’m struggling with default trait implementation in type parameters of structure, which stores it as PhantomData.

Here is the snippet that illustrates the problem:

```rust
use std::marker::PhantomData;

trait Trait {}
struct Struct;

impl Trait for Struct {}

struct GenericStruct<T: Trait = Struct> {
    _marker: PhantomData<T>
}

impl<T: Trait> GenericStruct<T> {
    fn new() -> Self {
        GenericStruct {
            _marker: PhantomData
        }
    }
}

fn main() {
    GenericStruct::new();
}

```

[https://play.rust-lang.org/?gist=7b3c32d39600a2de0df08593ced9134d&version=stable&mode=debug](https://play.rust-lang.org/?gist=7b3c32d39600a2de0df08593ced9134d&version=stable&mode=debug)

Rustc says that it cannot infer the type. Why so? And what can I do in this situation to not to have an API that asks the user to make explicit annotation for the default implementation of the trait I ship within the crate?

Thanks!

---

<div class="post-metadata">

**Author:** ![tmccombs](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/tmccombs/32/1845_2.png) [@tmccombs](https://internals.rust-lang.org/u/tmccombs)\
**Post date:** [May 1, 2018, 7:32am UTC](https://internals.rust-lang.org/t/default-trait-implementation-and-phantomdata/7429/2 "2018-05-01T07:32:45Z")

</div>

If you change the line in the main function to:

```rust
<GenericStruct<>>::new();

```

it compiles and runs. I think the `GenericStruct` by itself isn’t considered a concrete type, because it is generic, even though its only argument is defaulted. You need to use the “\<\>” syntax to actually instantiate a concrete `GenericStruct<Struct>`.

---

<div class="post-metadata">

**Author:** ![DDOtten](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ddotten/32/4334_2.png) [@DDOtten](https://internals.rust-lang.org/u/DDOtten)\
**Post date:** [May 1, 2018, 9:28am UTC](https://internals.rust-lang.org/t/default-trait-implementation-and-phantomdata/7429/3 "2018-05-01T09:28:10Z")

</div>

Yes I noticed this as well in one of my projects and to be honest I don’t really get why it is this way. Would there be any problem with allowing you to leave out the `<>` when all generic types have a default?

---

<div class="post-metadata">

**Author:** ![petrochenkov](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/petrochenkov/32/2784_2.png) [@petrochenkov](https://internals.rust-lang.org/u/petrochenkov)\
**Post date:** [May 1, 2018, 11:24am UTC](https://internals.rust-lang.org/t/default-trait-implementation-and-phantomdata/7429/4 "2018-05-01T11:24:25Z")

</div>

> [@tmccombs](#):
>
> \<GenericStruct\<\>\>::new()

`<GenericStruct>::new()` is enough, the empty angle brackets are not necessary.  
It moves `GenericStruct` from "value context" where inference is used to "type context" where defaults for type parameters are used.  
Ideally, type inference should fall back to using default type parameters somehow, but it's not clear how exactly this should be done (the recent iteration can be found in [https://github.com/rust-lang/rfcs/pull/2321](https://github.com/rust-lang/rfcs/pull/2321)).

---

<div class="post-metadata">

**Author:** ![mersinvald](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/mersinvald/32/4083_2.png) [@mersinvald](https://internals.rust-lang.org/u/mersinvald)\
**Post date:** [May 1, 2018, 12:29pm UTC](https://internals.rust-lang.org/t/default-trait-implementation-and-phantomdata/7429/5 "2018-05-01T12:29:44Z")

</div>

Thanks! Unfortunately the `<GenericStruct>::new()` is not working in my case, I have 4 type parameters actually, one default and 3 infered from context.

---

<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:** [March 25, 2019, 8:30am UTC](https://internals.rust-lang.org/t/default-trait-implementation-and-phantomdata/7429/6 "2019-03-25T08:30:08Z")

</div>

Closed on March 25, 2019, 8:30am UTC

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