Turbo Fish Syntax Explained

  Рет қаралды 34,932

Let's Get Rusty

Let's Get Rusty

Жыл бұрын

Today we are going over the turbo fish syntax in Rust!
#rust #programming #tutorial

Пікірлер: 88
@letsgetrusty
@letsgetrusty Жыл бұрын
📝Get your *FREE Rust cheat sheet* : www.letsgetrusty.com/cheatsheet
@AssemblyWizard
@AssemblyWizard Жыл бұрын
I love that the examples here start from "you don't need it here but you can", to "using it here is better", to "you must use it here". Perfect learning trajectory IMO 👌
@franciscocb94
@franciscocb94 Жыл бұрын
The first guy who saw a fish in :: and added a turbo on it, must have been high AF
@mathiassven
@mathiassven Жыл бұрын
I thought it was a fireship video with that thumbnail 😂
@alonelyproducer663
@alonelyproducer663 Жыл бұрын
same
@ARS-fi5dp
@ARS-fi5dp Жыл бұрын
Same 😂
@sohn7767
@sohn7767 Жыл бұрын
Same, I was thinking “Wait did he always sound like this?”
@tos3378
@tos3378 Жыл бұрын
Same
@sps014
@sps014 Жыл бұрын
Nope Fireship doesn't explore other languages in that deep, don't expect anything except hello world from him in non JS tech stack.
@scheimong
@scheimong Жыл бұрын
It's quite an elegant solution considering how prickly problem is.
@DougFort
@DougFort Жыл бұрын
This is a good one. Really clear on a subject I didn't know about.
@dieguitto3528
@dieguitto3528 Жыл бұрын
I f* love this "Rust Bites" section. So helpful. Thank u so much!
@anderdrache8504
@anderdrache8504 Жыл бұрын
For people asking why this syntax is needed but not for type annotations: When rust is parsing a type, a '
@square444
@square444 Жыл бұрын
You missed the trait-implementation version: example ::write_all(&mut stream, &msg_bytes)
@Tigregalis
@Tigregalis Жыл бұрын
i wanted to mention this one!
@mistamunsta
@mistamunsta Жыл бұрын
You should do a Collab with fireship
@thirstypooch
@thirstypooch Ай бұрын
this is my favorite Rust channel
@gabinfourcault8974
@gabinfourcault8974 Жыл бұрын
Very clear, as usual! Thank you for your work
@mcgameboy2188
@mcgameboy2188 Жыл бұрын
Thx for the info, I've already seen that before, but didn't know it has a cute name as Turbofish and was usable in all these case! Great video
@tourdesource
@tourdesource Жыл бұрын
Great lesson Bogdan, thank you!
@rodelias9378
@rodelias9378 Жыл бұрын
Nice video. Thanks!!
@peterhayman
@peterhayman Жыл бұрын
thanks friend! was just looking for how to do this
@josemanuelberrios7390
@josemanuelberrios7390 Жыл бұрын
Thank you!! 👏
@frittex
@frittex Жыл бұрын
Amazing video
@AssemblyWizard
@AssemblyWizard Жыл бұрын
Elvis operator (?:), spaceship operator (), walrus operator (:=), and now turbo-fish (::). Did I miss any? Notable mention: PHP's "paamayim nekudotayim" (::)
@AshtonSnapp
@AshtonSnapp Жыл бұрын
Were the PHP devs drunk when they named it??
@Rice7th
@Rice7th Жыл бұрын
@@AshtonSnapp lmao
@chri-k
@chri-k Жыл бұрын
@@AshtonSnapp it means “double colon” in ( transliterated ) Hebrew. It was introduced first in the Zend Engine, originally developed in Israel )
@everyhandletaken
@everyhandletaken Жыл бұрын
You might not be wearing pants, but you sure been paying attention to the details ☺️
@mageprometheus
@mageprometheus Жыл бұрын
Thanks, Bogdan.
@uwuzote
@uwuzote Жыл бұрын
Nice video! As a example for last case can provde std::any::TypeId::of function; Also, for first example it can additionally be rewritten as let students = ::new();
@0xccd
@0xccd Жыл бұрын
Cristal clear!!
@everyhandletaken
@everyhandletaken Жыл бұрын
I hope there is a turbo crab, in future releases 🦀
@DavidEngelen
@DavidEngelen Жыл бұрын
#1 in Rust videos
@apidas
@apidas Жыл бұрын
so it's just a way to specify a generic to a function. thanks!
@zperk13
@zperk13 Жыл бұрын
I was confused because I thought you were talking about the chess engine at first lol
@AsyncVoid
@AsyncVoid Жыл бұрын
Waiting on a video of a sample project How would you create a config that can be accessed by multiple threads? `Arc` or `static mut` if you know it will only be modified at start. Perhaps thread local?
@elijahshadbolt7334
@elijahshadbolt7334 Жыл бұрын
If it's a config cached at the start, just pass &Config to each thread (borrow the immutable config, that implements Sync and Send)
@elijahshadbolt7334
@elijahshadbolt7334 Жыл бұрын
If it's to be loaded from file for each request, create a static function that reads the file. No mutexes needed.
@Ether_Void
@Ether_Void Жыл бұрын
`static mut`: Would require some initialization (lazy static) or a MaybeUninitialized. It's also important that this is thread safe (Send + Sync) and therefor likely also in a Mutex or RwLock since you want it to be modified across threads. You aren't really overcoming the fact that you are working across threads here. The `Arc` is better if you want to avoid the MaybeUninitialized and global variables. This is the method I would choose tbh. Now if the Config has no issues with cross thread manipulation (Send + Sync) you wouldn't need a Mutex in the Arc. I assume you want to avoid copies. Otherwise implementing `clone` would be an option where the copied version can be sent to the other thread and the entire Arc/Mutex wouldn't be required, therefor, I'm inferring that the suggestion of opening the config for each request is not solving the issue. Thread local is also not solving you issue because it creates a separate instance for each thread, it can not be used to send across threads.
@opeyemiadeyemi5048
@opeyemiadeyemi5048 Жыл бұрын
Cool...
@3dilson
@3dilson Жыл бұрын
You are like Web Dev Simplified but for Rust
@marcomarek7734
@marcomarek7734 Жыл бұрын
Perfect. But who came up with that name? Genius lol.
@iliya-malecki
@iliya-malecki Жыл бұрын
please make a video on how multiple inheritance could be done with traits. If i have a family of objects that can be shown on a screen and also stored in a database, in conventional programming languages i would have to inherit from two classes and have a huge headache. Is this still a headache with traits or do they help?
@Tigregalis
@Tigregalis Жыл бұрын
traits solve this exact problem! that is, composition over inheritance. you would have two separate traits for each behaviour e.g. StoreInDatabase and DisplayOnScreen, with associated types and methods relevant to getting those two different things done. then, you could implement these traits for any number of types. you could then use either of those traits as interfaces to get that behaviour working in a generic way.
@iliya-malecki
@iliya-malecki Жыл бұрын
@@Tigregalis thanks so much for the explanation!
@Tigregalis
@Tigregalis Жыл бұрын
@@iliya-malecki kzbin.info/aero/PL03RlXG3y8N8R1F1qsRQCXsJs9fM4QoQP I made a playlist with great videos I found about Rust traits
@jay-tbl
@jay-tbl Жыл бұрын
most confusing thing about this is the use of '::'. I usually interpret :: to mean "inside of", like module_name::struct_name means the struct thats inside of that module. But turbofish syntax doesnt represent something inside of something else. Imo it would make more sense to look like function_name(param) without the ::
@hypnogri5457
@hypnogri5457 Жыл бұрын
Look up "Bastion of the Turbofish" to see why that is impossible. Basically: fn main() { let (the, guardian, stands, resolute) = ("the", "Turbofish", "remains", "undefeated"); let _: (bool, bool) = (the(resolute)); } this is valid syntax because it compares the two strings with < and > but the compiler can't tell if its a turbofish or if its a comparison. (it could be a function called the with the resolute argument passed as a parameter
@do0nv
@do0nv 9 ай бұрын
​@@hypnogri5457Yes, but you could technically check if `the` is a function or not and use that to determine the syntax. but its not worth the effort
@sunofabeach9424
@sunofabeach9424 4 ай бұрын
somehow Java and C# managed to get it right by omitting :: before angle brackets...
@TheZdannar
@TheZdannar Жыл бұрын
The fish and are friends, but how did it get it's name?
@letsgetrusty
@letsgetrusty Жыл бұрын
No idea why they chose turbo fish. They syntax looks like a fish though.
@Yotanido
@Yotanido Жыл бұрын
@@letsgetrusty Someone on reddit decided to just call it that and it stuck. They didn't even realise their name stuck around until much later.
@PrimeGoose
@PrimeGoose Жыл бұрын
var num = "22".parse() as Int32 vs turbofish
@WouterStudioHD
@WouterStudioHD Жыл бұрын
var?
@PrimeGoose
@PrimeGoose Жыл бұрын
@@WouterStudioHD it’s from swift
@WouterStudioHD
@WouterStudioHD Жыл бұрын
@@PrimeGoose ah right! Doesn’t make as much sense to me tbh, as it seems to be casting a parsing result, instead of parsing a type directly. Semantically not as clear I would say.
@AssemblyWizard
@AssemblyWizard Жыл бұрын
Casting an intermediate value (like with .collect() from the video) will be awful since you need brackets around the entire expression up until there. Even worse if the expression needs multiple fish
@WouterStudioHD
@WouterStudioHD Жыл бұрын
@@AssemblyWizard Very good point
@raffimolero64
@raffimolero64 Жыл бұрын
generics are just functions with optional arguments executed at compile-time. the difference between Rust generics and Zig "generics" is that Rust's allows builtin type inference.
@sobanya_228
@sobanya_228 Жыл бұрын
But why is it so much more verbose than in other languages? You can just use without :: in TypeScript for example.
@dyslexicsteak897
@dyslexicsteak897 Жыл бұрын
This is just differentiation between generics on types and generics on methods.
@remrevo3944
@remrevo3944 Жыл бұрын
The reason is mostly parser related. size_of < T > () Would be hard to differentiate from smaller than and bigger than.
@djpeterson7479
@djpeterson7479 Жыл бұрын
@@remrevo3944 Ok, I was wondering about this as many other languages simply allow you to type size_of() to get what you want. Effectively, it's the same thing? Instead we right size_of::() in Rust, correct? Still wrapping my brain around this.
@remrevo3944
@remrevo3944 Жыл бұрын
@@djpeterson7479 Yes, completely correct.
@WouterStudioHD
@WouterStudioHD Жыл бұрын
@@dyslexicsteak897 no?
@netify6582
@netify6582 Жыл бұрын
Rust really doesn't have the nicest syntax in the world. Lifetime operators, turbofish operator, question mark operator syntax (although very handy), they all look quite ugly to me. On the other hand closure syntax without any kind of an arrow? Rust goes often against major syntax trends I would say.
@WouterStudioHD
@WouterStudioHD Жыл бұрын
I’m sorry, what? Most languages don’t have an arrow in closures. C++, Swift, Python & Clojure all don’t have it. And what is a lifetime operator?
@netify6582
@netify6582 Жыл бұрын
​@@WouterStudioHD Java, Scala, JS, C# (lambda) are using arrow syntax which makes code more readable in my opinion. But you're right it's not a trend, there are two big camps on this. Lifetime operator, I was referring lifetime annotations aka &'a str syntax, why not at least &str'a ?
@WouterStudioHD
@WouterStudioHD Жыл бұрын
@@netify6582 it doesn’t if you ask me. Rust’s closures are the most readable of any language I know. It’s &’a str because it’s the lifetime of the reference, not the lifetime of the str itself.
@netify6582
@netify6582 Жыл бұрын
​@@WouterStudioHD Or you can say lifetime is not lifetime of just some unknown reference but reference of a specific type. So it would make sense to have it like &str'a and reading like: Reference to a string slice with lifetime a VS (currently) some reference with lifetime a... of a string slice. Not big deal but more logical, nicer, more readable and even bit shorter. It's a subjective matter, though... not war needed. Closures. I don't like using pipe symbol here (as it it already used for bitwise or and logical or) but I understand why they probably had nothing better so at least to separate it little bit with arrow like |x|=>f(x||y) vs |x,y|f(x||y); would be nice. For closures returning something current syntax little bit better |x,y|->z{f(x|y)} even though I would still prefer |x,y|->z=>{f(x|y)}. I guess this is lot about from what languages are you migrating to Rust.If you like current syntax, good for you.
@KeyError
@KeyError Жыл бұрын
But why does it need the :: ?
@hypnogri5457
@hypnogri5457 Жыл бұрын
Look up "Bastion of the Turbofish" to see why that is impossible. Basically: fn main() { let (the, guardian, stands, resolute) = ("the", "Turbofish", "remains", "undefeated"); let _: (bool, bool) = (the(resolute)); } this is valid syntax because it compares the two strings with < and > but the compiler can't tell if its a turbofish or if its a comparison. (it could be a function called the with the resolute argument passed as a parameter
@KeyError
@KeyError Жыл бұрын
@@hypnogri5457 Ahhhh, ok, thanks for the explanation
@igor-couto
@igor-couto Жыл бұрын
it's really ugly syntax though 😔
@zes7215
@zes7215 Жыл бұрын
no such thing as affection x etc about it or concise or etc, no nerx, say infix any nmw and any s perfx
@dragonmax2000
@dragonmax2000 Жыл бұрын
I bet you do not even read messages from here at all.
@letsgetrusty
@letsgetrusty Жыл бұрын
I don't
@alexskoropletov6146
@alexskoropletov6146 Жыл бұрын
Yeah, pretty much always using turbo fish with .collect()
MUST know Rust database libraries
5:50
Let's Get Rusty
Рет қаралды 39 М.
Strings in Rust FINALLY EXPLAINED!
21:40
Let's Get Rusty
Рет қаралды 74 М.
Кәріс өшін алды...| Synyptas 3 | 10 серия
24:51
kak budto
Рет қаралды 1,3 МЛН
Китайка и Пчелка 4 серия😂😆
00:19
KITAYKA
Рет қаралды 3,7 МЛН
5 traits your Rust types must implement
10:39
Let's Get Rusty
Рет қаралды 55 М.
Generic Associated Types: A Practical Introduction
8:53
chris biscardi
Рет қаралды 11 М.
Simple error handling in Rust
23:46
Let's Get Rusty
Рет қаралды 30 М.
Generic Traits, Impls, and Slices in Rustlang
18:05
chris biscardi
Рет қаралды 10 М.
Impl Trait aka Look ma’, no generics! by Jon Gjengset
1:09:05
Copenhagen Rust Community
Рет қаралды 26 М.
The Rust Survival Guide
12:34
Let's Get Rusty
Рет қаралды 129 М.
Unlocking your CPU cores in Python (multiprocessing)
12:16
mCoding
Рет қаралды 293 М.
Rust Functions Are Weird (But Be Glad)
19:52
Logan Smith
Рет қаралды 127 М.
Rust Data Modelling Without Classes
11:25
No Boilerplate
Рет қаралды 162 М.
Rust on Rails
11:34
No Boilerplate
Рет қаралды 130 М.
APPLE совершила РЕВОЛЮЦИЮ!
0:39
ÉЖИ АКСЁНОВ
Рет қаралды 2,1 МЛН
ТОП-5 культовых телефонов‼️
1:00
Pedant.ru
Рет қаралды 19 М.
TOP-18 ФИШЕК iOS 18
17:09
Wylsacom
Рет қаралды 817 М.