Structs in Rust

แชร์
ฝัง
  • เผยแพร่เมื่อ 23 พ.ย. 2024

ความคิดเห็น • 125

  • @letsgetrusty
    @letsgetrusty  3 ปีที่แล้ว +8

    📝 Get your *FREE Rust cheat sheet* : www.letsgetrusty.com/cheatsheet

  • @advanceringnewholder
    @advanceringnewholder 3 ปีที่แล้ว +126

    It is kinda helpful that rust tels you what's wrong and you don't have to search the error on stackoverflow

    • @masela01
      @masela01 2 ปีที่แล้ว +14

      It's EXTREMELY helpful and I absolutely love that in Rust

  • @nicejungle
    @nicejungle 2 ปีที่แล้ว +15

    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

  • @tiagomello
    @tiagomello 10 หลายเดือนก่อน +7

    I just read the structs chapter from the book. You are giving a fantastic walkthrough of the topic. Much easier to follow.

  • @UncleBenZ3
    @UncleBenZ3 4 หลายเดือนก่อน +3

    Awesome, you manage to provide so much helpful information in such a limited time and make it clear at the same time

  • @douglaspiresmartins2955
    @douglaspiresmartins2955 หลายเดือนก่อน +1

    Simply AMAZING. Props bro, you are really making the difference.

  • @rafaboanova
    @rafaboanova 2 ปีที่แล้ว +7

    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 ปีที่แล้ว

      he's following the book which is very good , sometimes my jelly brain is too bored to read

  • @RedCrabs-e8l
    @RedCrabs-e8l ปีที่แล้ว +8

    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!

  • @chairmakerPete
    @chairmakerPete 2 ปีที่แล้ว +7

    Following the course - it's getting powerful already, and we're not very far in. Nice!

  • @LebronJames-mj2uz
    @LebronJames-mj2uz 5 หลายเดือนก่อน +1

    god bless your soul the best rust tutorial online!

  • @zainjadoon759
    @zainjadoon759 3 ปีที่แล้ว +23

    thank you you saved me from rage quitting.

  • @verdell2x246
    @verdell2x246 2 ปีที่แล้ว +3

    These tutorials are amazing I never realized how good tuples were until now

  • @sonicsplasher
    @sonicsplasher 10 หลายเดือนก่อน +2

    Weirdly reminded me of TheNewBoston's C++ videos. Nostalgic. Great Tutorial

  • @ChristianAltamiranoAyala
    @ChristianAltamiranoAyala 2 ปีที่แล้ว +18

    Such a wonderful course, congrats and thank you for the effort you put into this.

  • @LukePighetti
    @LukePighetti 2 ปีที่แล้ว +5

    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 ปีที่แล้ว +3

      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.

  • @chadwood7997
    @chadwood7997 หลายเดือนก่อน +1

    Did I see that right? You can unpack a class, which provides its attributes as key value pairs for parameters in the initialization of another class? And you can do it AFTER your explicit parameters while those still override what’s supplied by the unpacked class?

  • @Christobanistan
    @Christobanistan 3 ปีที่แล้ว +124

    Man, VS Code is freaking awesome for Rust.

    • @zohnannor
      @zohnannor 3 ปีที่แล้ว +75

      say thank you to rust-analyzer developers!

    • @QmVuamFtaW4
      @QmVuamFtaW4 2 ปีที่แล้ว +21

      nvim with rust-analyzer :)

    • @QmVuamFtaW4
      @QmVuamFtaW4 2 ปีที่แล้ว +1

      @@NexushasTaken No.

    • @QmVuamFtaW4
      @QmVuamFtaW4 2 ปีที่แล้ว

      @@NexushasTaken No.

    • @vimalk78
      @vimalk78 2 ปีที่แล้ว

      @@QmVuamFtaW4 yeahhhh

  • @1nan821
    @1nan821 ปีที่แล้ว +1

    老师讲的非常好,感谢!美中不足是我的英语不太好,翻译的字幕看起来吃力。The teacher spoke very well, thank you! The only drawback is that my English is not very good and the translated subtitles look difficult.

  • @jhjacobs81
    @jhjacobs81 3 ปีที่แล้ว +10

    Very helpful :) i find these videos much easier to understand then the books

  • @xibodohls9469
    @xibodohls9469 ปีที่แล้ว +2

    Please explain where instances of structs are created? On stack or on heap?

  • @SeleDreams
    @SeleDreams ปีที่แล้ว +1

    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

  • @Erfa
    @Erfa 3 ปีที่แล้ว +11

    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 ปีที่แล้ว +6

      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 3 ปีที่แล้ว +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 3 ปีที่แล้ว +1

      ​@@markusbergkvist4882 ​ @Let's Get Rusty
      This man deserves a hearted comment 💯​

    • @the-nasim
      @the-nasim 2 ปีที่แล้ว +6

      In this case immutable reference is the solution.
      let name = &user1.username;

    • @yolowex6876
      @yolowex6876 2 ปีที่แล้ว

      @@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
      };
      ```

  • @knightlyadventure
    @knightlyadventure ปีที่แล้ว +1

    Thank you so much for your work.

  • @joaquimgregorio3380
    @joaquimgregorio3380 3 ปีที่แล้ว +2

    You are just an awesome man. Thanks for this great content!

  • @samdavepollard
    @samdavepollard 7 หลายเดือนก่อน

    very nice; super useful supplement to The Book

  • @phil86_
    @phil86_ 2 ปีที่แล้ว +1

    Great course, practical and useful

  • @raffimolero64
    @raffimolero64 2 ปีที่แล้ว +1

    uppercase `Self` keyword would've been a nice addition too

  • @homtom2
    @homtom2 10 หลายเดือนก่อน

    love these, thanks so much for making them

  • @2002budokan
    @2002budokan 2 ปีที่แล้ว +1

    Very good and complete, thank you.

  • @rohanbhandari7796
    @rohanbhandari7796 2 ปีที่แล้ว +1

    Awesome, I hope there is goodness coming in future videos as well. Keep up the good work.

  • @dangelgeek
    @dangelgeek 6 หลายเดือนก่อน

    Do you have the repository about this examples? thank you, great tutorials!

  • @lsatenstein
    @lsatenstein 2 ปีที่แล้ว +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 ปีที่แล้ว +3

      Yes.
      type NewTypeName = OldTypeName;

  • @BigfootPrinting
    @BigfootPrinting 2 ปีที่แล้ว +1

    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.

  • @geonysbrandlab
    @geonysbrandlab 2 ปีที่แล้ว +1

    Awesome course ! Thanks

  • @RamirodeSouza
    @RamirodeSouza 2 ปีที่แล้ว +1

    Do structs also follow the snake_case naming convention?
    Do they have to start with upper case?

    • @marc536
      @marc536 10 หลายเดือนก่อน +1

      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`

  • @ShaffleOne
    @ShaffleOne 2 ปีที่แล้ว

    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?

  • @AlwynSchoeman
    @AlwynSchoeman 2 ปีที่แล้ว +1

    Awesome as usual. Just wishing it didn't have the crappy background music.

  • @alfianandi1388
    @alfianandi1388 8 หลายเดือนก่อน

    Thanks, really helpful!

  • @alexanderschonfeld320
    @alexanderschonfeld320 2 ปีที่แล้ว

    Hi Bogdan. Can you share which VSCode extension you use?

  • @denizsincar29
    @denizsincar29 หลายเดือนก่อน

    In a whole time programming in rust, i shocked that enum variants can not only be just a type, but a whole struct:
    enum MyEnum {
    MyVariant {a: i32, b: bool}
    }
    Wow, i missed is somewhere.

  • @myhendry
    @myhendry 3 ปีที่แล้ว

    amazing tutorials. awesome and fantastic. subscribed. thank you

  • @AchuCSGO
    @AchuCSGO 2 หลายเดือนก่อน

    Nice tutorial bro

  • @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.

  • @a_maxed_out_handle_of_30_chars
    @a_maxed_out_handle_of_30_chars ปีที่แล้ว

    this was very good, thank you :)

  • @davidsimek1197
    @davidsimek1197 2 ปีที่แล้ว

    So if I input &self, its like non-static method and if its empty, its static?

  • @abacussssss
    @abacussssss 3 ปีที่แล้ว +2

    1:26 almost a very unfortunate typo!

  • @sshovelyjoe
    @sshovelyjoe ปีที่แล้ว +2

    Great tutorial but the background music is very distracting.

  • @meka4996
    @meka4996 3 ปีที่แล้ว

    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?

  • @asjn3e
    @asjn3e 2 ปีที่แล้ว

    thank you for great videos

  • @aniket0fficial
    @aniket0fficial 9 หลายเดือนก่อน

    Do I need to use all the attributes that we create in the structs ?

  • @msauditech
    @msauditech 2 ปีที่แล้ว

    Great one!

  • @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 ปีที่แล้ว +1

      They should perform exactly the same, it's just a syntactic difference.

  • @-karter-4556
    @-karter-4556 ปีที่แล้ว

    I rarely see people use tuples like class instances, is that how they are supposed to be used?

  • @KozaKrisz
    @KozaKrisz ปีที่แล้ว

    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()
    }

  • @vorrnth8734
    @vorrnth8734 2 ปีที่แล้ว +1

    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 2 ปีที่แล้ว

      @@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 2 ปีที่แล้ว

      You can express using composition, no?

  • @Bookwala12
    @Bookwala12 ปีที่แล้ว

    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.

  • @ksnyou
    @ksnyou 3 ปีที่แล้ว +1

    cool.. see u in next vid

  • @arsil_
    @arsil_ 3 ปีที่แล้ว

    its' just really awesome!!

  • @yolowex6876
    @yolowex6876 2 ปีที่แล้ว

    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.. }

  • @darshanv1748
    @darshanv1748 2 ปีที่แล้ว +1

    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

  • @nautilus2024
    @nautilus2024 2 ปีที่แล้ว

    Thanks a lot ^_^ it's very helpful ^_^

  • @Scotttclipz
    @Scotttclipz 8 หลายเดือนก่อน

    why does the sign change colors on every camera cut

  • @СергейДехтярёв-ъ4н
    @СергейДехтярёв-ъ4н 11 หลายเดือนก่อน

    So basically, associative methods are static functions?

  • @GolangDojo
    @GolangDojo 3 ปีที่แล้ว +1

    Is this in 24 fps?

  • @NikhilSharma-cw9lh
    @NikhilSharma-cw9lh 10 หลายเดือนก่อน

    Is there a git repo for this ?

  • @dzaki4559
    @dzaki4559 3 ปีที่แล้ว

    Good tutorials, i'm waiting videos about macro in rust 😃

  • @arturo3511
    @arturo3511 2 ปีที่แล้ว

    Why do you sometimes use :: and sometimes . ?

  • @sahilverma4077
    @sahilverma4077 3 ปีที่แล้ว +2

    what do you think why rust doesn't have classes?

    • @letsgetrusty
      @letsgetrusty  3 ปีที่แล้ว +11

      Rust has other tools such as structs and traits which allow you to accomplish similar goals while favoring composition over inheritance.

    • @Christobanistan
      @Christobanistan 3 ปีที่แล้ว +8

      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 ปีที่แล้ว +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 3 ปีที่แล้ว +3

      @@sahilverma4077 then learn c++, very similar but without safe memory management, which is what the borrow checker accomplishes

  • @adriancruz2822
    @adriancruz2822 ปีที่แล้ว

    Let's Get Structy

  • @shasank6854
    @shasank6854 2 ปีที่แล้ว +1

    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.

  • @daniel-lb4bh
    @daniel-lb4bh ปีที่แล้ว

    perfect

  • @kouko6727
    @kouko6727 2 ปีที่แล้ว

    Bro I'm loving rust right now but input gathering is killling me XD

  • @vimalk78
    @vimalk78 2 ปีที่แล้ว +1

    is it possible to remove the background soundtrack? it gets annoying after sometime

  • @sffortytwo
    @sffortytwo 2 ปีที่แล้ว +2

    Good course. But the background music is distracting to the point of maddening.

  • @bfrd9k
    @bfrd9k 2 ปีที่แล้ว

    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.

  • @Yupppi
    @Yupppi ปีที่แล้ว

    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?

  • @MrAtomUniverse
    @MrAtomUniverse 2 ปีที่แล้ว

    I think i died in this episode

    • @muhammad359
      @muhammad359 ปีที่แล้ว

      You are not alone!

  • @AngriestEwok
    @AngriestEwok 2 ปีที่แล้ว

    This one hurts my head.

  • @MightyMindsDev
    @MightyMindsDev หลายเดือนก่อน

    Богдан... 💀

  • @LiamDennehy
    @LiamDennehy 2 ปีที่แล้ว +1

    It's so distracting seeing the flag on the wall facing the wrong way.

  • @ali_randomNumberHere
    @ali_randomNumberHere ปีที่แล้ว

    "chapter 10"

  • @jbrown-acuity
    @jbrown-acuity 3 ปีที่แล้ว +2

    Love your videos, but the background music on this one makes it impossible to concentrate.