11
::<> ... the story of the turbofish in rust
(turbo.fish)
submitted
4 months ago* (last edited 4 months ago)
by
maegul@lemmy.ml
to
c/learningrustandlemmy@lemmy.ml
The about
section of the linked page has links and a brief explanation of the story.
In my own words ...
Description of the Turbo-fish syntax
- Whenever a generic type (see ch 10 of The Book) is involved in calling a function/method or constructing/handling a struct/enum, you may need to specify that concrete type for that generic.
- This type is provided with the
turbo fish
syntax:::<>
... where the type goes between the angle brackets. - EG:
let v = Vec::new();
let v2 = Vec::<i32>::new()
- The elements of
v
have an undefined type, which rust will infer once an element is pushed into it. Butv2
has a defined element type,i32
, defined using theturbo fish
.
The Story
- This syntax wasn't always liked, and the double colons (
::
) thought redundant. - But it stuck
- And was given the name "turbo-fish" by Anna Harren (u/deadstone, Reddit) ... who sadly passed away in 2021.
- It turns out that the double colons are pretty vital to preventing ambiguity.
- This is enshrined in the Bastion of the Turbofish which stands as a memorial to Anna Harren ...
- a test in the language's test suite that ensures that the double-colons syntax isn't removed by directly using the ambiguous syntax that'd arise without it.
- See if you can understand the test (it took me a while! ... this HN post might help):
let (the, guardian, stands, resolute) = ("the", "Turbofish", "remains", "undefeated");
let _: (bool, bool) = (the<guardian, stands>(resolute));
hint: what are angle brackets normally used for and how are bool
types related to that?
Haha … you have to squint maybe! Coming up with the name turbofish was probably low key genius!