5 traits your Rust types must implement

  Рет қаралды 62,141

Let's Get Rusty

Let's Get Rusty

Күн бұрын

Пікірлер
@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 Жыл бұрын
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.
@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.
@Андрей-ю2р6л
@Андрей-ю2р6л Жыл бұрын
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 6 ай бұрын
@@Андрей-ю2р6л ​ @user-ne1nw6hw2q So for example, a http status code shouldn't implement default trait because 0 is not valid http status, am I correct?
@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. 11 ай бұрын
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.
@orterves
@orterves Жыл бұрын
The feature flag approach is beautiful - a real have your cake and eat it too moment
@londospark7813
@londospark7813 Жыл бұрын
Thank you, great video and I'm really glad you showed how to feature-gate serde.
@n0kodoko143
@n0kodoko143 Жыл бұрын
Noob here. Super cool use of cfg attributes and features! This languages get cooler everyday (that I learn more about it)
@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.
@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
@creativecraving
@creativecraving 9 ай бұрын
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! 😅
@viniciusataidedealbuquerqu2837
@viniciusataidedealbuquerqu2837 Жыл бұрын
amazing in a nutshell. this is incredible how I have less fear programming in rust traits now
@nomadsome
@nomadsome 6 ай бұрын
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 6 ай бұрын
Glad I could help!
@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.
@daveduvergier3412
@daveduvergier3412 Жыл бұрын
Very nice explanation of feature switches there
@Amplifity
@Amplifity Жыл бұрын
Great video. Thanks for your efforts. I learn a lot from you.
@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 Жыл бұрын
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 Жыл бұрын
O maybe just use generic type parameter for pointer type. With default Rc.
@nirajgautam403
@nirajgautam403 9 ай бұрын
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
@F776-b2s
@F776-b2s 7 ай бұрын
Wow, I was implementing PartialEq manually with match cases
@after_alec
@after_alec Жыл бұрын
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.
@jptcnde9803
@jptcnde9803 3 ай бұрын
nice vid, one question, whats that extension called when the red text is being shown?
@MH_VOID
@MH_VOID Жыл бұрын
8:10 why not use a raw string literal (r#"..."#) for `user_str`?
@normiemango
@normiemango Жыл бұрын
Rustacean army come in! 🦀🦀🦀
@ishanksharma9051
@ishanksharma9051 Жыл бұрын
amazing video, keep it up. I was about to comment on the feature flag for serde!!
@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.
@antifa_communist
@antifa_communist 4 ай бұрын
You could use a generic
@meka4996
@meka4996 4 ай бұрын
You are The MAN!
@hilal53426
@hilal53426 Жыл бұрын
You are really doing great work! do you have a Udemy course?
@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.
@a_maxed_out_handle_of_30_chars
@a_maxed_out_handle_of_30_chars Жыл бұрын
this was awesome, thank you :)
@AdamGetchell1
@AdamGetchell1 10 ай бұрын
Super useful and helpful, thanks!
@sinaebr7337
@sinaebr7337 Жыл бұрын
love this type of videos. thanks
@chrishabgood8900
@chrishabgood8900 Жыл бұрын
do you have to derive each time?
@shahriarzaman4715
@shahriarzaman4715 Жыл бұрын
So, If you don't own Role struct, how can you implement debug/clone/xyz trait?
@krisaor
@krisaor Жыл бұрын
Thanks! great explanations!
@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!
@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?
@henrymaddocks984
@henrymaddocks984 Жыл бұрын
Excellent video 👍🏼
@Taladar2003
@Taladar2003 3 ай бұрын
Please don't blindly derive Default unless the resulting value actually makes sense in the context of your type (e.g. if you have an identifier type Default with an empty string makes no sense).
@GlobalYoung7
@GlobalYoung7 Жыл бұрын
thank you 👍
@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 ?
@bsgamer5069
@bsgamer5069 3 ай бұрын
Can’t rust provide these traits by default?
@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.
@tbird-z1r
@tbird-z1r Жыл бұрын
@@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 Жыл бұрын
@@tbird-z1r Well thank you for the explanation, now I understand.
@electrolyteorb
@electrolyteorb Жыл бұрын
Sexually transmitted diseases
@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?
@LibreGlider
@LibreGlider 5 ай бұрын
YT is not a good forum to get help with code.
@adibhanna
@adibhanna Жыл бұрын
I was working on a similar video :D
@modernkennnern
@modernkennnern Жыл бұрын
Now people are telling me to exposing my STDs.. smh Good video 👍
@mcpecommander5327
@mcpecommander5327 Жыл бұрын
Society when rust adds Debug to the list of default traits
@not_herobrine3752
@not_herobrine3752 Жыл бұрын
unbelievable
@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.
@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?
@ErikBongers
@ErikBongers 6 ай бұрын
Drop the music.
@MrEW1985
@MrEW1985 Жыл бұрын
It was a nice and easy video until you started with serialization code.
@Christobanistan
@Christobanistan 7 ай бұрын
Adding unnecessary traits can really slow down compile times.
@bsgamer5069
@bsgamer5069 3 ай бұрын
Yeah first compile time is slow
@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");
@InMemoryOfNeo
@InMemoryOfNeo Жыл бұрын
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
Рет қаралды 24 М.
8 deadly mistakes beginner Rust developers make
14:14
Let's Get Rusty
Рет қаралды 173 М.
If people acted like cats 🙀😹 LeoNata family #shorts
00:22
LeoNata Family
Рет қаралды 3,6 МЛН
За кого болели?😂
00:18
МЯТНАЯ ФАНТА
Рет қаралды 2,9 МЛН
You Should Really Know These Traits in Rust
18:36
Oliver Jumpertz
Рет қаралды 16 М.
@sasautoaccessorie MG HS 2024 Car Upholstery
0:37
Sehr Al Shimal Auto Accsesories Tr.
Рет қаралды 4
Why do developers hate Rust?
8:20
Let's Get Rusty
Рет қаралды 142 М.
but what is 'a lifetime?
12:20
leddoo
Рет қаралды 78 М.
Rust Powered Polymorphism ⚡️ With Traits
9:55
Code to the Moon
Рет қаралды 104 М.
All Rust string types explained
22:13
Let's Get Rusty
Рет қаралды 183 М.
why rust libraries may never exist.
7:26
Low Level
Рет қаралды 276 М.
Choose the Right Option
18:14
Logan Smith
Рет қаралды 73 М.
Closures in Rust
19:53
Let's Get Rusty
Рет қаралды 80 М.
Use Arc Instead of Vec
15:21
Logan Smith
Рет қаралды 152 М.
Новый матовый iMac M4
0:59
Romancev768
Рет қаралды 185 М.