Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between Trait::<T> and <Trait<T>>?

Tags:

generics

rust

I have a type whose method I can access through

SomeTrait::<T>::method()

But I don't understand the difference between that and

<SomeTrait<T>>::method()

In C++ I would expect this:

SomeTrait<T>::method()

Are these two different? They both seem to be calling the <T> specialisation of method on SomeTrait.

like image 540
Phil H Avatar asked Dec 02 '19 14:12

Phil H


2 Answers

The C++ syntax cannot be used because it is an ambiguous syntax in Rust: in SomeTrait<T>::method(), is the first < a lesser-than operator, or the beginning of a generic parameters list?

The two methods you refer to are used to disambiguate this:

  • <SomeTrait<T>> is called the fully qualified syntax
  • SomeTrait::<T> is called the turbofish notation (unofficial name).
like image 171
Boiethios Avatar answered Nov 04 '22 11:11

Boiethios


SomeTrait::<T>::method() and <SomeTrait<T>>::method() are the same thing in Rust.

Just a style choice.

like image 42
Shawn Tabrizi Avatar answered Nov 04 '22 09:11

Shawn Tabrizi