# Type annotation

**URL:** <https://users.rust-lang.org/t/type-annotation/106044>\
**Category:** code review\
**Created:** [January 29, 2024, 7:05pm UTC](https://users.rust-lang.org/t/type-annotation/106044 "2024-01-29T19:05:48Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![sayyedkamran](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/sayyedkamran/32/12741_2.png) [@sayyedkamran](https://users.rust-lang.org/u/sayyedkamran)\
**Post date:** [January 29, 2024, 7:05pm UTC](https://users.rust-lang.org/t/type-annotation/106044/1 "2024-01-29T19:05:48Z")

</div>

Here is a line of code that calculate sum of square of first n integers:

`(1..=n).sum::<u32>().pow(2)`

Can any one explain, why we are using sum::?

What I know is "::" is used for name space, so what is the purpose of using this operator here? Why we can not write is like?

`(1..=n).sum<u32>().pow(2)`

---

<div class="post-metadata">

**Author:** ![jwodder](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/jwodder/32/27866_2.png) [@jwodder](https://users.rust-lang.org/u/jwodder)\
**Post date:** [January 29, 2024, 7:51pm UTC](https://users.rust-lang.org/t/type-annotation/106044/2 "2024-01-29T19:51:47Z")

</div>

FYI, it seems you wrote `sum::<u32>()`, but the `<u32>` was interpreted as an HTML tag and doesn't show up. Enclose your code in backticks so it'll display properly.

As to your question, `::<>` is the "turbofish operator," here used to specify the type of the generic parameter to the `sum<S>()` method. Without it, Rust wouldn't be able to tell what type `sum()` should return, as there could be multiple `S`'s that implement `Sum<u32>` (assuming that `n` has already been explicitly annotated as `u32`, that is, otherwise it gets even more ambiguous).

---

<div class="post-metadata">

**Author:** ![sayyedkamran](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/sayyedkamran/32/12741_2.png) [@sayyedkamran](https://users.rust-lang.org/u/sayyedkamran)\
**Post date:** [January 29, 2024, 7:59pm UTC](https://users.rust-lang.org/t/type-annotation/106044/3 "2024-01-29T19:59:36Z")

</div>

thanks @jwodder, let me edit my post as well

---

<div class="post-metadata">

**Author:** ![sayyedkamran](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/sayyedkamran/32/12741_2.png) [@sayyedkamran](https://users.rust-lang.org/u/sayyedkamran)\
**Post date:** [January 29, 2024, 8:01pm UTC](https://users.rust-lang.org/t/type-annotation/106044/4 "2024-01-29T20:01:53Z")

</div>

But dnt we use type annotation like?:

`(1..=n).sum<u32>().pow(2)`

---

<div class="post-metadata">

**Author:** ![jwodder](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/jwodder/32/27866_2.png) [@jwodder](https://users.rust-lang.org/u/jwodder)\
**Post date:** [January 29, 2024, 8:02pm UTC](https://users.rust-lang.org/t/type-annotation/106044/5 "2024-01-29T20:02:43Z")

</div>

No, that's invalid. The `::` are needed so that the compiler can tell the difference between a turbofish and a less-than operation chained with a greater-than.

---

<div class="post-metadata">

**Author:** ![geeklint](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/geeklint/32/14833_2.png) [@geeklint](https://users.rust-lang.org/u/geeklint)\
**Post date:** [January 29, 2024, 8:14pm UTC](https://users.rust-lang.org/t/type-annotation/106044/6 "2024-01-29T20:14:14Z")

</div>

See this file for an example where the turbofish is required for syntax disambiguity:

[https://github.com/rust-lang/rust/blob/master/tests/ui/parser/bastion-of-the-turbofish.rs#L42](https://github.com/rust-lang/rust/blob/master/tests/ui/parser/bastion-of-the-turbofish.rs#L42)

---

<div class="post-metadata">

**Author:** ![quinedot](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/quinedot/32/17693_2.png) [@quinedot](https://users.rust-lang.org/u/quinedot)\
**Post date:** [January 29, 2024, 8:37pm UTC](https://users.rust-lang.org/t/type-annotation/106044/7 "2024-01-29T20:37:17Z")

</div>

> [@sayyedkamran](#):
>
> But dnt we use type annotation like?:

Turbofish (leading `::`) is used in expressions. In type specific context, it is not.

Given how the biggest push to allow eliding the colons in expressions went (follow the links in the bastion comment), the turbofish is unlikely to ever go away.

Some say the turbofish is Ferris' (🦀) friend or sidekick.

---

<div class="post-metadata">

**Author:** ![khimru](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/khimru/32/31344_2.png) [@khimru](https://users.rust-lang.org/u/khimru)\
**Post date:** [January 29, 2024, 9:03pm UTC](https://users.rust-lang.org/t/type-annotation/106044/8 "2024-01-29T21:03:00Z")

</div>

Because that expression already has a meaning:

- `(1..=n).sum < u32` means: _take `sum` field and `u32` variable and compare them_\[1\]
- `(1..=n).sum < u32 >()` means: _tale value that you have on previous step and compare it with `()`_\[2\]
- `(1..=n).sum < u32 >().pow(2)` means: _take that comparison result and call pow now_\[3\]

You may say that it's not what you have meant, but then, if you would involve types in the syntax parsing, you [get nasty surprises](https://en.wikipedia.org/wiki/Most_vexing_parse) and it becomes **literally impossible** to answer the question: does that thing that I have is a syntactically valid Rust code or not (it may depend on target platform and even then [it's impossible to answer that question 100% of time](https://blog.reverberate.org/2013/08/parsing-c-is-literally-undecidable.html)).

Rust uses turbofish to avoid all that insanity. And it also avoid proliferation of `typename` and `template` tags in generics (which are needed because even with full compiler on your hand you **still** couldn't parse `sum<u32>()` properly in C++ is sum [nested class](https://en.cppreference.com/w/cpp/language/nested_types).

Instead of spending hundreds of man-years and making every user learn bazillion complicated rules to save couple of characters Rust decided to use turbofish operator instead.

This was, obviously, the right thing to do.

* * *

1. And yes, variable named `u32` is perfectly valid in Rust 

2. Again: nothing illegal there. 

3. Again: totally no problems there.

---

<div class="post-metadata">

**Author:** ![drmason13](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/drmason13/32/20629_2.png) [@drmason13](https://users.rust-lang.org/u/drmason13)\
**Post date:** [January 29, 2024, 10:26pm UTC](https://users.rust-lang.org/t/type-annotation/106044/9 "2024-01-29T22:26:14Z")

</div>

That is beautiful!

All hail the turbofish 🙂

[https://turbo.fish/](https://turbo.fish/)

We learn to love our turbofish, I'm glad it found a fun name.

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/system/32/51372_2.png) [@system](https://users.rust-lang.org/u/system)\
**Post date:** [April 28, 2024, 10:27pm UTC](https://users.rust-lang.org/t/type-annotation/106044/10 "2024-04-28T22:27:03Z")

</div>

Closed on April 28, 2024, 10:27pm UTC

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.
