5 traits your Rust types must implement

  Рет қаралды 55,768

Let's Get Rusty

Let's Get Rusty

Жыл бұрын

In this video we are discussing 5 Rust traits that your types should implement, especially if you are exposing your types as part of a public interface.
FREE Rust Cheat Sheet: letsgetrusty.com/cheatsheet
Code: github.com/letsgetrusty/commo...

Пікірлер: 108
@letsgetrusty
@letsgetrusty Жыл бұрын
📝Get your *FREE Rust cheat sheet* : www.letsgetrusty.com/cheatsheet
@jacquesfaba55
@jacquesfaba55 Жыл бұрын
No
@olivarra1
@olivarra1 Жыл бұрын
I'm genuinely curious - Why do you keep promoting the free cheat sheet on every. single. one. of. your. videos? Don't get me wrong, you make good content and your cheatsheet is really good, so I do appreciate that, but it's always those 5 seconds in your videos that I have to mentally ignore because I already know about it.
@g227x01
@g227x01 Жыл бұрын
@@olivarra1 To get to the cheat sheet, you need to give an email and name. He's getting a lead in return (that may be used for inbound marketing). You "pay" with your lead info.
@bread8176
@bread8176 11 ай бұрын
I'm not giving you my email address
@mrme5694
@mrme5694 Жыл бұрын
I loved the fact that he makes things that look intimidating to a new learner much easier by diving straight right into it. He doesnt beat around the bush, too, no unnecessary bs. Only what is needed.
@oriontvv
@oriontvv Жыл бұрын
8:20 Just wanted to notice that serde can skip field db because Db implements Default trait, otherwise would be compile error
@damonpalovaara4211
@damonpalovaara4211 Жыл бұрын
You can also configure serde to call a function that returns that type
@cozyGalvinism
@cozyGalvinism Жыл бұрын
Don't skip deserialization on a default value by default. It's bad practice, rather point the default parameter of the serde attribute to the default function. That way you can still deserialize a value if it's present but default when it's not.
@orterves
@orterves Жыл бұрын
The feature flag approach is beautiful - a real have your cake and eat it too moment
@IvanDeMarino
@IvanDeMarino Жыл бұрын
Thank you so much for this video: you made me add a bunch of feature flags and a new release for one of my crates, purely inspired by this content.
@londospark7813
@londospark7813 Жыл бұрын
Thank you, great video and I'm really glad you showed how to feature-gate serde.
@Amplifity
@Amplifity Жыл бұрын
Great video. Thanks for your efforts. I learn a lot from you.
@waynr
@waynr Жыл бұрын
Probably the only trait here that should always be implemented is Debug. Library authors can have good reasons for not implementing the others.
@Derik.
@Derik. 6 ай бұрын
The title is meant to catch your attention. It's hyperbole. Yes, library authors shouldn't implement the traits if they have good reason not to. Otherwise they should keep them in mind.
@viniciusataidedealbuquerqu2837
@viniciusataidedealbuquerqu2837 Жыл бұрын
amazing in a nutshell. this is incredible how I have less fear programming in rust traits now
@daveduvergier3412
@daveduvergier3412 Жыл бұрын
Very nice explanation of feature switches there
@TheTmLev
@TheTmLev Жыл бұрын
Your library shouldn't implement Send + Sync if it doesn't make sense. Users can wrap T in Arc if they really need it.
@user-ne1nw6hw2q
@user-ne1nw6hw2q Жыл бұрын
Also it's not always good to implement default trait, because sometimes a type is expected to always hold a non-trivial value. Users can use Option if they really need default value
@armancdev
@armancdev Ай бұрын
@@user-ne1nw6hw2q ​ @user-ne1nw6hw2q So for example, a http status code shouldn't implement default trait because 0 is not valid http status, am I correct?
@n0kodoko143
@n0kodoko143 Жыл бұрын
Noob here. Super cool use of cfg attributes and features! This languages get cooler everyday (that I learn more about it)
@nomadsome
@nomadsome 25 күн бұрын
Hey, this is nth video from you that I am watching and I just want to say thanks for all this content. I have been on my Rust learning journey for a while and only recently am I feeling productive enough in my project. There are still tons of things to learn but Rust makes getting started much easier than I would have expected from a systems language.
@letsgetrusty
@letsgetrusty 24 күн бұрын
Glad I could help!
@ishanksharma9051
@ishanksharma9051 Жыл бұрын
amazing video, keep it up. I was about to comment on the feature flag for serde!!
@sinaebr7337
@sinaebr7337 Жыл бұрын
love this type of videos. thanks
@v0xl
@v0xl Жыл бұрын
You should also derive Copy as well as Clone if the type you're exposing is supposed to be copied (like no heap allocated things like vecs or strings will be added to it in the future), and also should always add it to all basic enums that don't store any data. serde serialize and deserialize should probably be behind an optional feature, because not all people need serde
@Rangsk
@Rangsk Жыл бұрын
> serde serialize and deserialize should probably be behind an optional feature, because not all people need serde 8:34
@AdamGetchell1
@AdamGetchell1 5 ай бұрын
Super useful and helpful, thanks!
@a_maxed_out_handle_of_30_chars
@a_maxed_out_handle_of_30_chars 7 ай бұрын
this was awesome, thank you :)
@krisaor
@krisaor Жыл бұрын
Thanks! great explanations!
@CorentinWay
@CorentinWay Жыл бұрын
Always great vidéos as always... I see many usefull addine plugins on vs code or the terminal (autocomplete) can you give us some of them ? Or do a video about them.
@creativecraving
@creativecraving 4 ай бұрын
Using Serde to apply strict typing to JSON input strings so easily is BY FAR the greatest value I've received from this video. Way to bury the lead! 😅
@henrymaddocks984
@henrymaddocks984 Жыл бұрын
Excellent video 👍🏼
@MasterHigure
@MasterHigure Жыл бұрын
Just so you know: The J and Gj in Jon Gjengset are soft. It's pronounced more like "Yon Yengset". Not that I blame you, I don't expect people to know how to pronounce names from a language they don't know.
@linkernick5379
@linkernick5379 Жыл бұрын
Good to know, I didn't know that too.
@31redorange08
@31redorange08 Жыл бұрын
Well, he has access to the internet to look it up.
@MasterHigure
@MasterHigure Жыл бұрын
@@31redorange08 Names in particular aren't that easy to look up. Regular words you can find in dictionaries, and some dictionaries have pronunciation guides. But proper nouns is not so easy.
@GlobalYoung7
@GlobalYoung7 Жыл бұрын
thank you 👍
@user-gy9dh2ox6p
@user-gy9dh2ox6p 2 ай бұрын
Wow, I was implementing PartialEq manually with match cases
@hilal53426
@hilal53426 Жыл бұрын
You are really doing great work! do you have a Udemy course?
@after_alec
@after_alec 10 ай бұрын
Also, Copy instead of Clone if your type can implement it!
@kiransingh2935
@kiransingh2935 Жыл бұрын
I only just realized that serde is a portmaneau of serialize-deserialize.
@nirajgautam403
@nirajgautam403 3 ай бұрын
So basically I can have one struct and trait along with it implementation block. Now I can use this trait implementation on the other struct and override the base trait if I want by creating impl block and change the functionality in it. I'm new to rust and exploring how to have inheritance like functionality in rust via composition. I require some good references as I can't find many good example anywhere
@adibhanna
@adibhanna Жыл бұрын
I was working on a similar video :D
@jefffriedman6942
@jefffriedman6942 Жыл бұрын
Isn't it inefficient to use Arc where Rc would work? That should be mentioned, since this is not a free modification.
@goviczek
@goviczek 10 ай бұрын
I thought the same. I'm wonder if it could be implemented as feature. If feature e.g. thread_safe is active use Arc otherwise Rc. I think, its quite a common problem. Is a standard solution for that in Rust?
@goviczek
@goviczek 10 ай бұрын
O maybe just use generic type parameter for pointer type. With default Rc.
@normiemango
@normiemango Жыл бұрын
Rustacean army come in! 🦀🦀🦀
@MH_VOID
@MH_VOID Жыл бұрын
8:10 why not use a raw string literal (r#"..."#) for `user_str`?
@chrishabgood8900
@chrishabgood8900 Жыл бұрын
do you have to derive each time?
@le_moski
@le_moski Жыл бұрын
What extension/settings do you use to get errors typed like that while coding?
@prajwalchapagain
@prajwalchapagain Жыл бұрын
Error lens
@donizetevida2149
@donizetevida2149 Жыл бұрын
VSCode has an official Rust extension.
@le_moski
@le_moski Жыл бұрын
@@prajwalchapagain Thanks!
@not_herobrine3752
@not_herobrine3752 Жыл бұрын
unbelievable
@shahriarzaman4715
@shahriarzaman4715 8 ай бұрын
So, If you don't own Role struct, how can you implement debug/clone/xyz trait?
@ZekeFast
@ZekeFast Жыл бұрын
Bogdan, at 6:55 you some how abruptly cut you video and jumped to serde topic without showing that Arc fixed the issue or showing any consequences of changes from Rc to Arc which feels a bit weird and unfinished. Also you haven't present any solution from the John's book. JFYI. Also you touched very nice topic about non-sendable types. So, I have a question. What if you have a type which need to be Send-able, like your User to be used in e.g. Axum, but you don't own it and it contains Rc which makes it non-sendable. What would be a solution to make such type sendable?
@QuayTech
@QuayTech Жыл бұрын
8:27 Why does It show 2 users printing in the log?
@what42pizza
@what42pizza Жыл бұрын
I feel like it's better to have separate types for atomics since they're a bit slower. Example: pub struct User { ... db: Rc, } pub struct AtomicUser { ... db: Arc, }
@LimitedWard
@LimitedWard Жыл бұрын
Wouldn't you be losing even more speed when having to switch between types?
@what42pizza
@what42pizza Жыл бұрын
@@LimitedWard What do you mean by switching between types?
@donizetevida2149
@donizetevida2149 Жыл бұрын
​@@LimitedWardyou will use first or second struct. The way it was shown on video, you will be obligated to use Arc type, which is slow. If user don't need arc, it must simple use "User" type, as commented above, else it must use second type.
@modernkennnern
@modernkennnern Жыл бұрын
Now people are telling me to exposing my STDs.. smh Good video 👍
@hotfishdev
@hotfishdev Жыл бұрын
I’m not too wild about your use of the term “should” in this video. Fine otherwise, but Rust has so many targets and use cases where you definitely shouldn’t use ARC instead of Rc or waste time including Serde support (embedded comes to mind), that laying down these things as absolutes to newbs without caveat is, imo, kinda harmful.
@gabrielcox10
@gabrielcox10 Жыл бұрын
I NEED HELP! how do i use a code written in rust?
@1____-____1
@1____-____1 19 күн бұрын
YT is not a good forum to get help with code.
@chrisalexthomas
@chrisalexthomas Жыл бұрын
I'm still laughing at STD traits....I know what it means... but it's so funny anyway...
@lua-nya
@lua-nya Жыл бұрын
Somehow I understand what you mean but not why you find it funny.
@tbird81
@tbird81 Жыл бұрын
@@lua-nya STD used to stand for subscriber trunk dialling, which is how phone lines used to handle out of area calls. He's clearly laughing at the juxtaposition of the "old school" phone lines with the "new kid on the block" Rust.
@normiemango
@normiemango Жыл бұрын
​@@lua-nya I agree.
@lua-nya
@lua-nya Жыл бұрын
@@tbird81 Well thank you for the explanation, now I understand.
@electrolyteorb
@electrolyteorb Жыл бұрын
Sexually transmitted diseases
@mcpecommander5327
@mcpecommander5327 9 ай бұрын
Society when rust adds Debug to the list of default traits
@diadetediotedio6918
@diadetediotedio6918 Жыл бұрын
Is this really? I do remember I implemented a trait for a foreign type these days, for testing a concept that I had in mind about extension functions
@workflowinmind
@workflowinmind Жыл бұрын
You must have wrapped it as it's not possible
@diadetediotedio6918
@diadetediotedio6918 Жыл бұрын
@@workflowinmind No, I really did not wrapped it, really.
@diadetediotedio6918
@diadetediotedio6918 Жыл бұрын
@@workflowinmind You can try it for yourself: ```rust trait StringExt { fn print_it(&self); } impl StringExt for String { fn print_it(&self) { println!("It is: {}", self); } } fn main() { let msg = "Hello, World!".to_string(); msg.print_it(); } ```
@LawlessSentry
@LawlessSentry Жыл бұрын
@@diadetediotedio6918 I think he said foreign traits AND foreign types, if you have either of them locally defined you can implement it.
@diadetediotedio6918
@diadetediotedio6918 Жыл бұрын
@@LawlessSentry OOO, that makes sense, thanks! But why is this the case?
@linkernick5379
@linkernick5379 Жыл бұрын
Unfortunately, there is no easy way to have Debug trait implemented for a struct or enum, containing function type. JS, as opposite, does have it by default.
@JS-fd5oh
@JS-fd5oh Жыл бұрын
As a user of the lib, I'm concerned about code bloat that comes with libs. The author of the lib ASSUMED I will need it. What if I don't use any of these Debug, Sync etc in my code but they still get imported with the lib? Will rust compiler cut them out from my binary if my code doesn't use any of these derived traits?
@AshtonSnapp
@AshtonSnapp Жыл бұрын
Send & Sync are just marker traits as far as I am aware (no actual functionality, just flags that tell the compiler that certain multithreading things are safe). As for the others, the compiler is based on LLVM and _should_ automatically remove unused code from the compiled output (at least in release builds). So you should be good.
@JS-fd5oh
@JS-fd5oh Жыл бұрын
@@AshtonSnapp I'm not sure they are just marker traits. I think the macro literally embedds trait implementations into your code like any other macro does. So you end up with the implementation for Sync, Debug etc.
@CalebTerryRED
@CalebTerryRED Жыл бұрын
Sync is different from debug though, it doesn't have any associated methods to be added to a binary. The only place the trait could be used is by the compiler when type checking functions, it won't exist at any lower levels. Traits with associated functions or values like debug are a little bit different, but still should have their functions removed if they're unused
@JS-fd5oh
@JS-fd5oh Жыл бұрын
@@CalebTerryRED Thanks for explanation. One concern. Should? Or Will be removed? :)
@stalker32041
@stalker32041 Жыл бұрын
​@@JS-fd5oh should and will be removed.
@ErikBongers
@ErikBongers 23 күн бұрын
Drop the music.
@Christobanistan
@Christobanistan Ай бұрын
Adding unnecessary traits can really slow down compile times.
@MrEW1985
@MrEW1985 Жыл бұрын
It was a nice and easy video until you started with serialization code.
@SchmySeymour
@SchmySeymour Жыл бұрын
I know it's a convention in tech instructions to use the type of something as the name, but it's a terrible convention that makes reading and interpreting difficult for beginners. I stopped watching the video around 1:20 when I saw the implementation of the User type was named 'user'. Put another way, the line let user: User = User { may as well say, "bar bar bar bar bar bar bar bar bar bar bar" for all of the nuance it provides. This isn't BrainF**k, after all. Please, for the love of clarity, stop using the same name for types and their implementations. Give us tangible, meaningful examples. Like the type is "Vehicle" and the implementation is "car", or type is "Animal" and implementation is "dog", or whatever, just not "Thing" and "thing". It's bad enough that the documentation gives "path" as both a crate and a string name in the one example: use std::path::Path; ... let path = Path::new("./foo/bar.txt");
@criptouni
@criptouni Жыл бұрын
I actually tend to look for ::new instead of Default ... but I guess ::new without any args is Default LOL any tips on that @letsgetrusty ?
@InMemoryOfNeo
@InMemoryOfNeo 8 ай бұрын
1:12 how are you making these codes coming to screen like that?
Should Rust be your first programming language?
7:37
Let's Get Rusty
Рет қаралды 22 М.
All Rust features explained
21:30
Let's Get Rusty
Рет қаралды 284 М.
MEU IRMÃO FICOU FAMOSO
00:52
Matheus Kriwat
Рет қаралды 11 МЛН
ROCK PAPER SCISSOR! (55 MLN SUBS!) feat @PANDAGIRLOFFICIAL #shorts
00:31
but what is 'a lifetime?
12:20
leddoo
Рет қаралды 59 М.
All Rust string types explained
22:13
Let's Get Rusty
Рет қаралды 149 М.
You Should Really Know These Traits in Rust
18:36
Oliver Jumpertz
Рет қаралды 10 М.
Advanced Traits in Rust
13:35
Let's Get Rusty
Рет қаралды 38 М.
8 deadly mistakes beginner Rust developers make
14:14
Let's Get Rusty
Рет қаралды 156 М.
Rust's Most Important Containers 📦 10 Useful Patterns
17:11
Code to the Moon
Рет қаралды 113 М.
Intro to async/.await in Rust
13:57
Let's Get Rusty
Рет қаралды 82 М.
Rust Powered Polymorphism ⚡️ With Traits
9:55
Code to the Moon
Рет қаралды 90 М.
How to read complicated Rust types
20:17
chris biscardi
Рет қаралды 51 М.
Девушка и AirPods Max 😳
0:59
ОТЛИЧНИКИ
Рет қаралды 16 М.
Дени против умной колонки😁
0:40
Deni & Mani
Рет қаралды 12 МЛН
APPLE совершила РЕВОЛЮЦИЮ!
0:39
ÉЖИ АКСЁНОВ
Рет қаралды 1,6 МЛН
#miniphone
0:16
Miniphone
Рет қаралды 3,1 МЛН
WWDC 2024 - June 10 | Apple
1:43:37
Apple
Рет қаралды 10 МЛН