Тёмный

Hands-on With Dynamic Dispatch Traits in Rust 🦀 Rust Tutorial for Developers 

Trevor Sullivan
Подписаться 14 тыс.
Просмотров 4,5 тыс.
50% 1

Rust provides a language construct called dynamic dispatch for trait objects, which allows you to write generic code. Functionally, dynamic dispatch provides similar benefits to writing functions with "generic" arguments, which we covered in an earlier video. However, dynamic dispatch results in smaller binary sizes, with a potential cost at runtime. In this video, we'll take a hands-on approach to exploring dynamic dispatch, and how it differs from generic implementations. We'll also take a brief look at supertraits, and how they encapsulate behaviors from multiple child traits! Get your Rust IDE fired up and follow along!
🤯 Rust Programming Playlist 🦀 • Rust Programming Tutor...
📖 Dynamic Dispatch Docs 📦➡️ doc.rust-lang.org/reference/t...
📖 Returning Traits from Functions 📦➡️ doc.rust-lang.org/rust-by-exa...
📖 Supertraits Docs 📦➡️ doc.rust-lang.org/rust-by-exa...
Visual Studio Code ➡️ code.visualstudio.com
Rust Website ➡️ rust-lang.org
Rustup Installer ➡️ rustup.rs
Rust Docs ➡️ doc.rust-lang.org/book
Please follow me on these other social channels!
➡️ trevorsullivan.net
➡️ github.com/pcgeek86
➡️ / pcgeek86
➡️ / trevorsullivan
➡️ / trevorsoftware
➡️ tiktok.com/pcgeek86
All trademarks, logos and brand names are the property of their respective owners. All company, product and service names used in this website are for identification purposes only. Use of these names,trademarks and brands does not imply endorsement.
#rustlang #rust #rustdev #opensource #software #linux #devops #programming #rusty #dev #coding #codinglife #code #coder #ubuntu #ubuntulinux #appdev #developer

Наука

Опубликовано:

 

13 сен 2023

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 17   
@austinperrine23
@austinperrine23 7 дней назад
This is so good man! Love this video and your content. I personally appreciate that you have those articles of some very root sources present in you video.
@1____-____1
@1____-____1 8 месяцев назад
Just found your channel last Friday. I'm still learning. Right now I can't escape the hell of having my match arms types that don't match. I'm taking a break to watch this.
@EbonySeraphim
@EbonySeraphim 4 месяца назад
We need to always qualify the runtime cost of dynamic dispatch or v-table calls as miniscule, and for 99% of engineers, ignorable. If your code isn't running in a tight loop over millions of objects that need to be processed in realtime, then don't worry about it. The performance hit is CPU and a vast majority of code written isn't bound or bottlenecked by the CPU.
@SergiuTalnaci
@SergiuTalnaci 6 месяцев назад
I have 10yoe but new to learning rust, thanks for making so much content, we are writing our AWS backend with lambdas in rust. I think you could get way more views if you rename your videos to other programming concepts. Like interfaces, oop and mocking in rust. As a newbie in rust, I don’t search for the correct rust terms because I don’t even know them yet. Just something to think about.
@Dygear
@Dygear 6 месяцев назад
This is actually really good feedback.
@shailendrajadhav8603
@shailendrajadhav8603 8 месяцев назад
IMO a graphical illustration of the Dynamic Dispatch might be easier to understand. I am still getting my head around this concept :)
@TrevorSullivan
@TrevorSullivan 8 месяцев назад
I agree, that's a good idea to visualize it. Try to think of it in terms of behaviors instead of objects. Even though the term "dynamic dispatch" is confusing in and of itself, try to ignore the terminology and focus on the simple concept that it actually is! You'll get it, just practice a few times with different concepts. 🙂
@helxsz
@helxsz 4 месяца назад
great tutorial !!!
@farzadmf
@farzadmf 8 месяцев назад
Great video, thank you! I'm pretty sure I'm missing something, but I'm confused as to why we need to use this feature; is it only to reduce the binary size? Because seems like the "normal" way of doing things seems more intuitive 🤔
@TrevorSullivan
@TrevorSullivan 8 месяцев назад
Great question! I'm still trying to figure that out for myself. Maybe you can find he best answer!
@JoQeZzZ
@JoQeZzZ 14 дней назад
The example at 8:38 shows a good reason to use this. Sometimes you and the compiler don't know what trait object will be returned and so it's literally impossible to choose the correct implementation of that trait at compile time. Remind yourself that with monomorphism two different structs that implement the same trait are totally different at compile time, so returning some 'Number' that has a function of 'square()' would be akin to e.g. returning either a u8 or f64, they have totally different sizes so can't both be allocated on the stack and furthermore they have a completely different implementation to square. So now you must return a heap reference with a dynamic trait (Box) in order to dynamically choose the correct squaring implementation at runtime. Basically, the dyn keywords makes it at all possible to have polymorphism, which can be incredibly useful even with Rust's emphasis on monomorphism, traits and enums. (another way to write the above example would be to make an enum Number{Float(f64), Int(u8)} and then return the enum and make a function square(number: Number) => Number that matches all the variants and squares their contents, but this requires a match statement and therefore requires you to change your code everywhere once you add another type of number)
@CliveStewart-bq3od
@CliveStewart-bq3od Месяц назад
very good tutorial but there seems to be some flaw that can do with a bit of refinement : The get_animal() function definition should be more general ..were instead of having to change the content for the function definition we can simply supply the function with a parameter of the animal object eg: get_animal(animal_name). so that the function definition looks like :::... fn get_animal(animal_name)->Box{ Box::from(animal_name ) } I just dont know how to do it... is it possible ??
@TrevorSullivan
@TrevorSullivan Месяц назад
Thanks, and that's a good question. I am not sure how that would be accomplished. Generics are somewhat limited in Rust, and are still being developed, as I understand it.
@CliveStewart-bq3od
@CliveStewart-bq3od Месяц назад
@@TrevorSullivan thank you.
Далее
REALLY LOVES CHIPS
00:19
Просмотров 3,8 млн
Two Ways To Do Dynamic Dispatch
19:54
Просмотров 69 тыс.
Generic Traits, Impls, and Slices in Rustlang
18:05
Просмотров 10 тыс.
Rust Powered Polymorphism ⚡️ With Traits
9:55
Просмотров 89 тыс.
Rust Generics
22:10
Просмотров 21 тыс.
Карточка Зарядка 📱 ( @ArshSoni )
0:23
Полезные программы для Windows
0:56