Structs in Rust

  Рет қаралды 95,744

Let's Get Rusty

Let's Get Rusty

Күн бұрын

The ultimate Rust lang tutorial. Follow along as we go through the Rust lang book chapter by chapter.
📝 Get notified when the Rust Cheatsheet comes out: letsgetrusty.com/cheatsheet
The Rust book: doc.rust-lang.org/stable/book/​
0:00 Intro
0:36 Defining/Using Structs
3:39 Function Constructors
5:13 Reusing Instance Data
6:00 Tuple Structs
7:10 Example Use Case
11:43 Method Syntax
15:11 Associated Functions
16:47 Outro
#letsgetrusty​ #rust​lang #tutorial

Пікірлер: 119
@letsgetrusty
@letsgetrusty 3 жыл бұрын
📝 Get your *FREE Rust cheat sheet* : www.letsgetrusty.com/cheatsheet
@advanceringnewholder
@advanceringnewholder 2 жыл бұрын
It is kinda helpful that rust tels you what's wrong and you don't have to search the error on stackoverflow
@masela01
@masela01 Жыл бұрын
It's EXTREMELY helpful and I absolutely love that in Rust
@Christobanistan
@Christobanistan 3 жыл бұрын
Man, VS Code is freaking awesome for Rust.
@zohnannor
@zohnannor 2 жыл бұрын
say thank you to rust-analyzer developers!
@stdint.h
@stdint.h Жыл бұрын
no
@QmVuamFtaW4
@QmVuamFtaW4 Жыл бұрын
nvim with rust-analyzer :)
@QmVuamFtaW4
@QmVuamFtaW4 Жыл бұрын
@@NexushasTaken No.
@QmVuamFtaW4
@QmVuamFtaW4 Жыл бұрын
@@NexushasTaken No.
@zainjadoon759
@zainjadoon759 3 жыл бұрын
thank you you saved me from rage quitting.
@tiagomello
@tiagomello 4 ай бұрын
I just read the structs chapter from the book. You are giving a fantastic walkthrough of the topic. Much easier to follow.
@sonicsplasher
@sonicsplasher 4 ай бұрын
Weirdly reminded me of TheNewBoston's C++ videos. Nostalgic. Great Tutorial
@chairmakerPete
@chairmakerPete 2 жыл бұрын
Following the course - it's getting powerful already, and we're not very far in. Nice!
@ChristianAltamiranoAyala
@ChristianAltamiranoAyala 2 жыл бұрын
Such a wonderful course, congrats and thank you for the effort you put into this.
@phil86_
@phil86_ 2 жыл бұрын
Great course, practical and useful
@verdell2x246
@verdell2x246 Жыл бұрын
These tutorials are amazing I never realized how good tuples were until now
@joaquimgregorio3380
@joaquimgregorio3380 2 жыл бұрын
You are just an awesome man. Thanks for this great content!
@nicejungle
@nicejungle Жыл бұрын
Thanks for these tutorial. I was very dubious by the lack of OOP in Rust but implementations in structs has definitely converted me. Let's get Rusty :D
@Turalcar
@Turalcar Жыл бұрын
It's more enums that do that
@homtom2
@homtom2 4 ай бұрын
love these, thanks so much for making them
@rohanbhandari7796
@rohanbhandari7796 2 жыл бұрын
Awesome, I hope there is goodness coming in future videos as well. Keep up the good work.
@knightlyadventure
@knightlyadventure Жыл бұрын
Thank you so much for your work.
@handsome_brunch
@handsome_brunch 2 жыл бұрын
Awesome course ! Thanks
@2002budokan
@2002budokan 2 жыл бұрын
Very good and complete, thank you.
@user-ct1bq5xz4q
@user-ct1bq5xz4q Жыл бұрын
Thanks for your great content! Your background music is a bit noisy, if you can disable it while recording videos, that helps alot for focusing on learning!
@myhendry
@myhendry 2 жыл бұрын
amazing tutorials. awesome and fantastic. subscribed. thank you
@alfianandi1388
@alfianandi1388 2 ай бұрын
Thanks, really helpful!
@asjn3e
@asjn3e 2 жыл бұрын
thank you for great videos
@LukePighetti
@LukePighetti 2 жыл бұрын
Love your tutorials. One "why can't" question. Why can't String types accept String literals? It seems like the compiler could easily determine if a String type was needed, and cut down on some boilerplate
@codyheiner3636
@codyheiner3636 2 жыл бұрын
I think it's because String literals have type String slice. And saying "it seems the compiler could easily..." is very easy to say in toy examples, but can you make a set of rules that are guaranteed to do the right thing in ALL cases no matter how complex the code is? You might ask, why are string literals slices rather than strings. Probably for better performance.
@jhjacobs81
@jhjacobs81 2 жыл бұрын
Very helpful :) i find these videos much easier to understand then the books
@a_maxed_out_handle_of_30_chars
@a_maxed_out_handle_of_30_chars 6 ай бұрын
this was very good, thank you :)
@samdavepollard
@samdavepollard Ай бұрын
very nice; super useful supplement to The Book
@rafaboanova
@rafaboanova Жыл бұрын
This is fantastic, THANK YOU! I was looking for videos on Rust to get a feeling of it (learning casually whenever I have some minutes to spare, no coding yet) and the ones I found before yours were either too slow, too advanced or too light on code. But the way you do it is perfect for me. Explain, show example, repeat. Really awesome, thank you!
@skellderknowledge3621
@skellderknowledge3621 5 ай бұрын
he's following the book which is very good , sometimes my jelly brain is too bored to read
@msauditech
@msauditech 2 жыл бұрын
Great one!
@arsil_
@arsil_ 2 жыл бұрын
its' just really awesome!!
@nhanguyen2023
@nhanguyen2023 Жыл бұрын
Thanks a lot ^_^ it's very helpful ^_^
@ksnyou
@ksnyou 3 жыл бұрын
cool.. see u in next vid
@ShaffleOne
@ShaffleOne Жыл бұрын
Hey! Thanks for the lessons, searched a great playlist and I finally found it :) One question: Whats the point of using String, String::from(), when there is just a &str?)
@vbmade2000
@vbmade2000 2 жыл бұрын
Thanks for the video. Can you make a video on PhantomData?
@raffimolero64
@raffimolero64 2 жыл бұрын
uppercase `Self` keyword would've been a nice addition too
@1nan821
@1nan821 Жыл бұрын
老师讲的非常好,感谢!美中不足是我的英语不太好,翻译的字幕看起来吃力。The teacher spoke very well, thank you! The only drawback is that my English is not very good and the translated subtitles look difficult.
@SeleDreams
@SeleDreams Жыл бұрын
it would have been good to cover the storage of other structs in a struct, since this is where it gets pretty complex compared to other languages. once it's not one of the primary types
@lsatenstein
@lsatenstein 2 жыл бұрын
Definitely, thank you for your excellent podcasts. Is there a typedef type of definition for a struct? I come from the hostile C environment where I make use of typedefs for structs and fields within structs. Just learning and wanting to transition to Rust, and port my code over as a learning exercise.
@codyheiner3636
@codyheiner3636 2 жыл бұрын
Yes. type NewTypeName = OldTypeName;
@daniel-lb4bh
@daniel-lb4bh Жыл бұрын
perfect
@dangelgeek
@dangelgeek 6 күн бұрын
Do you have the repository about this examples? thank you, great tutorials!
@xibodohls9469
@xibodohls9469 Жыл бұрын
Please explain where instances of structs are created? On stack or on heap?
@Erfa
@Erfa 3 жыл бұрын
Great video! I'm curious what is happening at 2:50. Is the new "name" variable taking ownership of the username string, is it a reference or does it implicitly clone it?
@letsgetrusty
@letsgetrusty 3 жыл бұрын
Excellent question! In this case ownership is being moved. If you try to create another variable and assign username to it, you'll get an error.
@markusbergkvist4882
@markusbergkvist4882 2 жыл бұрын
@Erfa90 I had the same question. Turns out you get "partially moved value" if you are trying to use `user1.name`. Try `rustc --explain E0382` to learn more about it.
@NuLuumo
@NuLuumo 2 жыл бұрын
​@@markusbergkvist4882 ​ @Let's Get Rusty This man deserves a hearted comment 💯​
@the-nasim
@the-nasim 2 жыл бұрын
In this case immutable reference is the solution. let name = &user1.username;
@yolowex6876
@yolowex6876 Жыл бұрын
@@letsgetrusty 5:52 - What happens to the ownership of the attributes of user2 if we create user3 just without defining email and username? i mean like: ``` let user3 = User { ..user2 }; ```
@alexanderschonfeld320
@alexanderschonfeld320 Жыл бұрын
Hi Bogdan. Can you share which VSCode extension you use?
@BigfootPrinting
@BigfootPrinting Жыл бұрын
I'm a little thrown off coming from C++ and Swift that String and &str (literals/references to literals) cannot be implicitly interchanged, and thus requires an explicit conversion from the literal to a String with ::from("") which is ALL OVER programs.
@Bookwala12
@Bookwala12 8 ай бұрын
All the functions in the impl are associated functions irrespective if they carry an instance as a parameter or not. They are not methods if they dont have self as the first parameter.
@huuhhhhhhh
@huuhhhhhhh Жыл бұрын
Seem like a bug in rust analyser to annotate the arugments to functions with the parameter names. If I type this out explicitly, I get a compiler error since rust doesn't allow naming arguments to functions (just parameters at definition). I'll look at opening an issue toward the extension but thought I'd point it out here too.
@meka4996
@meka4996 2 жыл бұрын
Could you please point me in the right direction for this "fn initialize(ctx: Context) -> ProgramResult {...}" ... Initialize is a struct, does that mean ctx is a variable of type Context struct with Initialize struct inside?
@aniket0fficial
@aniket0fficial 2 ай бұрын
Do I need to use all the attributes that we create in the structs ?
@-karter-4556
@-karter-4556 9 ай бұрын
I rarely see people use tuples like class instances, is that how they are supposed to be used?
@dzaki4559
@dzaki4559 2 жыл бұрын
Good tutorials, i'm waiting videos about macro in rust 😃
@letsgetrusty
@letsgetrusty 2 жыл бұрын
Coming soon!
@davidsimek1197
@davidsimek1197 Жыл бұрын
So if I input &self, its like non-static method and if its empty, its static?
@rusty9060
@rusty9060 2 жыл бұрын
Is there a specific reason we used tuples for Struct fields at 6:20 or else could we also use arrays wouldn't which be a better fit for the job of storing same data types and also easy on memory as it would be stored in stack?
@codyheiner3636
@codyheiner3636 2 жыл бұрын
They should perform exactly the same, it's just a syntactic difference.
@yolowex6876
@yolowex6876 Жыл бұрын
Isn't there constructors that we can define for our struct like the way we define constructors in c++? Great tutorial, thank you.
@pavloburyanov5842
@pavloburyanov5842 Жыл бұрын
Just add fn new(..args..) -> Self { ..code.. }
@RamirodeSouza
@RamirodeSouza Жыл бұрын
Do structs also follow the snake_case naming convention? Do they have to start with upper case?
@marc536
@marc536 3 ай бұрын
they should be in camel case. if you try otherwise, the compile/rust analyser gives you this warning: Structure `Rectangle_Struct` should have CamelCase name, e.g. `RectangleStruct` type `Rectangle_Struct` should have an upper camel case name convert the identifier to upper camel case: `RectangleStruct`
@abacussssss
@abacussssss 2 жыл бұрын
1:26 almost a very unfortunate typo!
@vorrnth8734
@vorrnth8734 2 жыл бұрын
How are "is a" relationships modelled? Say I want square. A square is also a rectangle and computes the area basically in the same way. In c++ i could use inheritance for that but how do you express that in rust? And is there access control, something like "private", for structs in rust?
@AssemblyWizard
@AssemblyWizard 2 жыл бұрын
A square is not a rectangle in OOP: A function that uses a rectangle can change its width and be guaranteed that the height doesn't change, but a square breaks that, violating the Liskov substitution principle.
@HcmfWice
@HcmfWice Жыл бұрын
@@AssemblyWizard An immutable square is absolutely a(n immutable) rectangle. If you allow mutation, and have Square inherit from Rectangle, your model is wrong.
@diadetediotedio6918
@diadetediotedio6918 Жыл бұрын
You can express using composition, no?
@Yupppi
@Yupppi 6 ай бұрын
So Rust doesn't have something like a class where you'd put the functionality inside the struct itself and remove some clutter in the file?
@NikhilSharma-cw9lh
@NikhilSharma-cw9lh 3 ай бұрын
Is there a git repo for this ?
@KozaKrisz
@KozaKrisz 6 ай бұрын
Hi! I know this video was made in 2021, but what about this solution with the can_hold function? fn can_hold(&self, other: &Rectangle) -> bool { self.area() > other.area() }
@darshanv1748
@darshanv1748 2 жыл бұрын
You make amazing content but just pointing out an error that is you can't do something that you did at 3:20 instead can use mem::replace
@bidens_left_toenail
@bidens_left_toenail 2 ай бұрын
why does the sign change colors on every camera cut
@arturo3511
@arturo3511 2 жыл бұрын
Why do you sometimes use :: and sometimes . ?
@AlwynSchoeman
@AlwynSchoeman Жыл бұрын
Awesome as usual. Just wishing it didn't have the crappy background music.
@GolangDojo
@GolangDojo 3 жыл бұрын
Is this in 24 fps?
@letsgetrusty
@letsgetrusty 3 жыл бұрын
30 fps actually
@adriancruz2822
@adriancruz2822 6 ай бұрын
Let's Get Structy
@kouko6727
@kouko6727 Жыл бұрын
Bro I'm loving rust right now but input gathering is killling me XD
@user-ji6ip7ou8d
@user-ji6ip7ou8d 5 ай бұрын
So basically, associative methods are static functions?
@MrAtomUniverse
@MrAtomUniverse Жыл бұрын
I think i died in this episode
@muhammad359
@muhammad359 Жыл бұрын
You are not alone!
@sahilverma4077
@sahilverma4077 3 жыл бұрын
what do you think why rust doesn't have classes?
@letsgetrusty
@letsgetrusty 3 жыл бұрын
Rust has other tools such as structs and traits which allow you to accomplish similar goals while favoring composition over inheritance.
@Christobanistan
@Christobanistan 3 жыл бұрын
OOP requires polymorphism, which is not zero cost. It requires RTTI and every virtual call requires a lookup. It becomes quite costly over time.
@sahilverma4077
@sahilverma4077 3 жыл бұрын
@@Christobanistan yea that can be a valid reason, after reading more on structures in c I feel like the way they implemented it is really awesome, all I need is to bind data and the capability to call methods on it and there are access specifiers too
@sahilverma4077
@sahilverma4077 3 жыл бұрын
@@Christobanistan I hate Borrow checker though
@julians.2597
@julians.2597 2 жыл бұрын
@@sahilverma4077 then learn c++, very similar but without safe memory management, which is what the borrow checker accomplishes
@shasank6854
@shasank6854 Жыл бұрын
I guess some people are getting confused by the errors showing up around 9:42, it can be solved by adding the return keyword in front of the expression. And a semicolon at the end, ofcourse.
@bfrd9k
@bfrd9k Жыл бұрын
As someone who is learning rust I kinda hate this vscode module. When assigning user2 it is not obvious to people learning rust that email and username should not be typed out, if you do type it out as shown and test or build you will get an error because rust functions do not support named parameters, they are positional only.
@sshovelyjoe
@sshovelyjoe 9 ай бұрын
Great tutorial but the background music is very distracting.
@vimalk78
@vimalk78 Жыл бұрын
is it possible to remove the background soundtrack? it gets annoying after sometime
@AngriestEwok
@AngriestEwok Жыл бұрын
This one hurts my head.
@sffortytwo
@sffortytwo 2 жыл бұрын
Good course. But the background music is distracting to the point of maddening.
@ali_randomNumberHere
@ali_randomNumberHere Жыл бұрын
"chapter 10"
@jbrown-acuity
@jbrown-acuity 2 жыл бұрын
Love your videos, but the background music on this one makes it impossible to concentrate.
@LiamDennehy
@LiamDennehy Жыл бұрын
It's so distracting seeing the flag on the wall facing the wrong way.
Enums and Pattern Matching in Rust
12:34
Let's Get Rusty
Рет қаралды 78 М.
Understanding Ownership in Rust
25:31
Let's Get Rusty
Рет қаралды 234 М.
顔面水槽がブサイク過ぎるwwwww
00:58
はじめしゃちょー(hajime)
Рет қаралды 106 МЛН
Which one will take more 😉
00:27
Polar
Рет қаралды 86 МЛН
8 deadly mistakes beginner Rust developers make
14:14
Let's Get Rusty
Рет қаралды 153 М.
but what is 'a lifetime?
12:20
leddoo
Рет қаралды 55 М.
Closures in Rust
19:53
Let's Get Rusty
Рет қаралды 70 М.
All Rust features explained
21:30
Let's Get Rusty
Рет қаралды 274 М.
Traits in Rust
11:44
Let's Get Rusty
Рет қаралды 91 М.
Why do developers hate Rust?
8:20
Let's Get Rusty
Рет қаралды 88 М.
Rust YouTubers Tier List
4:58
Let's Get Rusty
Рет қаралды 36 М.
TypeScript Generics are EASY once you know this
22:21
ByteGrad
Рет қаралды 119 М.
The Rust Survival Guide
12:34
Let's Get Rusty
Рет қаралды 123 М.
3D printed Nintendo Switch Game Carousel
0:14
Bambu Lab
Рет қаралды 2,9 МЛН
Теперь это его телефон
0:21
Хорошие Новости
Рет қаралды 1,5 МЛН
Распаковка айфона в воде😱 #shorts
0:25
Mevaza
Рет қаралды 1,5 МЛН
Save Work Efficiently on Your Computer 18/05/2024
0:51
UNIQUE PHOTO EDITING
Рет қаралды 305 М.