Intro to Rustlang (Structs, Methods, Functions, Related Functions and the Display/Debug Traits)

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

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

  • @karthikarunr8505
    @karthikarunr8505 5 ปีที่แล้ว +10

    Man, I've just started with Rust and I'm loving your videos. Thank God Reddit redirected me here.

    • @TensorProgramming
      @TensorProgramming  5 ปีที่แล้ว +3

      I was wondering where I was getting the influx of Rust views from. Well, glad you like the content and glad to see more new faces.

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

    This whole series is very begginer friendly and well explained and at just the right pace. Very good work.

    • @TensorProgramming
      @TensorProgramming  4 ปีที่แล้ว

      Thank you. Glad you found the content useful.

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

    I watched about five videos prior to finding your channel, and I was worried I would never fully grasp Struct and Traits. This video was easy to follow, you went to great lengths to explain even minor details in a way I believe anyone could understand, and you actually used numbers for height/width and showed the expected output. No other channel did that!?! Thank you so much, I am sold and just subscribed! Keep up the amazing work and the in-depth explanations with examples! You are a life saver!

  • @diogoxp
    @diogoxp 7 ปีที่แล้ว +14

    Very insightful, absolutely love the videos. I've been reading a lot on rust, but there's nothing like watching your videos to truly get a sense of how the language works. Keep up the good work!

    • @TensorProgramming
      @TensorProgramming  7 ปีที่แล้ว +9

      Thank you, I appreciate the comment. Its high praise to be told that my videos are helping someone learn the language. Rust might feel a bit difficult at first, but try not to go too "functional" with it and you'll be alright.

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

    Just as I was slowing down on learning Rust, your videos just brought me back on track. Great job!

  • @rh2462
    @rh2462 4 ปีที่แล้ว

    One of the nicest videos on Rust!! great job, thank you so much

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

    Wow! Rust is literally the most idiomatic and intuitive systems-level language I've seen!

    • @TensorProgramming
      @TensorProgramming  6 ปีที่แล้ว

      Once you get past the ownership system, yes it does become the most intuitive systems level language next to Golang.

    • @SilvestreVivo
      @SilvestreVivo 5 ปีที่แล้ว

      @@TensorProgramming I am still trying to get past over there..... even so, I think Rust is beautiful

    • @TensorProgramming
      @TensorProgramming  5 ปีที่แล้ว

      @@SilvestreVivo haha. Yeah, that happens. Just take your time and sit with the concepts for a bit. Maybe build something with them and eventually you'll get them.

    • @SilvestreVivo
      @SilvestreVivo 5 ปีที่แล้ว

      @@TensorProgramming Thanks for your response. I come from the JS world and learning Rust is a hate-love feeling. I am using CodeWars to make some exercises every day to get used with it. Cheers!!!!

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

      @@SilvestreVivo Good old JS. Good luck man.

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

    Rust is really awesome! I have used Go for production for 2 years and I hate its simplicity and old fashion syntax, there are so many boilerplate codes in Go. I always want to move to a better language and come up with Rust, Scala and Elixir. Rust is more difficult than Elixir but easier than Scala, it also has the best performance, so I finally settled in Rust and start learning it. I would recommend Rust to anyone who wants to build microservices, distributed system and cli tools, etc.

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

      I'd say that Rust and Scala have about the same learning curve. Scala just is a larger language and it has more specialized features in the form of things like Higher Kinded types etc so it can feel a bit more difficult then it really is.
      Rust is a great language for its use-cases and it will continue to grow in the future.

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

    Best tutorial videos for Rust introduction. Thanks a lot. Much love, man.

  • @maximilianotorres1225
    @maximilianotorres1225 6 ปีที่แล้ว

    Object Oriented Programming in Rust
    Amazing video

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

      yeah the OOP in Rust is fairly smooth and easy to learn. No messy items like Inheritance.

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

    How did the Formatter from Display trait get resolved automagically in the last example? (Coming from Scala, didn't see any implicits either here)

    • @TensorProgramming
      @TensorProgramming  6 ปีที่แล้ว

      The write macro is what takes care of that for us. We defined the formatter as the buffer that we wanted to write the string to which then was invoked every time we accessed the display trait for this specific struct. The formatter itself is a bit of a generic buffer in that it allows us to use other buffers since it only formats the strings for us.

    • @ShreyasPurohit
      @ShreyasPurohit 6 ปีที่แล้ว

      @@TensorProgramming I see, thanks

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

    so obj::new() is basically constructor for Object class ?

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

      Well, I wouldn't call the Object struct a class since there aren't really such things in Rust but yes its basically an initialization function.

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

    What vscode theme are you using? thanks

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

      Spacemacs dark with fira code font.

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

    How does it know the difference between methods and related functions. Is it just the self parameter?

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

      Yes. Basically associated functions are functions that are in the implementation block which is associated with a type but they don't include self as the first parameter while methods include the self parameter.

  • @arslanmughal4863
    @arslanmughal4863 5 ปีที่แล้ว +3

    Good Source for noobs thank you :)

  • @Wajahat-Ali
    @Wajahat-Ali 5 ปีที่แล้ว

    What is the name of its Grammer extension that you are using?

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

    so beautiful . 😭😭😭😭

  • @anderslundstrom4388
    @anderslundstrom4388 6 ปีที่แล้ว

    very good!

  • @balajisofficialutube
    @balajisofficialutube 6 ปีที่แล้ว

    Hi tensor, wat's diff bw method and relative func ?

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

      A method is a function that is defined in the context of an enum, struct, or trait and its first parameter is always self.
      Related functions are functions that are attached to am implementation block but do not follow the method rules above.

    • @kevinwang6188
      @kevinwang6188 6 ปีที่แล้ว

      Tensor Programming Should we assume that a “related” function is more like a class/static method in some other languages?

    • @adriankenway9961
      @adriankenway9961 4 ปีที่แล้ว

      @@TensorProgramming Thank you for the reply. That clarified stuff for me :)

  • @TheEdwinpako
    @TheEdwinpako 6 ปีที่แล้ว

    Are those objects located in stack or heap? if on stack how can get them on heap?

    • @TensorProgramming
      @TensorProgramming  6 ปีที่แล้ว +3

      By default, structs are stored on the stack. If you want to store something on the heap, you have to do it manually in most cases by wrapping it in a container type like a Box, RC, Vec, String etc.

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

    i wonder why you always say have good night at the end of your videos maybe its noon here by the way i like the method you use for teaching

  • @Akash-oo5bx
    @Akash-oo5bx 11 หลายเดือนก่อน

    Should someone in 2023 consider learning rust for stable carrier prospects or something else you would recommend

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

      Rust is more relevant then ever in terms of finding a mainstream job. That being said, many of the Rust positions that I've personally seen or been involved with generally require a higher level of experience then your typical language because the domains are more complex. Some companies are using it for web servers and APIs, and in those jobs, a junior dev can absolutely fill the position but in the case of system level development, many companies are a bit nervous to put a newer dev on their team. This should change over time however, given that the language is still seeing a wide adoption. Either way, learning a new language is always a worthwhile endeavor.

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

    THis looks like a bit of a mess. Implicit returns? the Object constructor using parameters that have the same name as the internal attributes. That looks like begging for bugs. How am I to know which one the runtime will reference? If nothing else it completely wrecks the readability of the code.

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

      Implicit returns are a staple of expressions. You get used to them and actually when moving to a language without them start to wonder why they aren't there. Then again, I am also a little biased because I work with a ton of functional languages. Languages like Dart have them but hide them as well. It also helps with conditional flow, since it forces you to have an else branch if you are returning something. Not to mention that you can do var assignment on a match/conditional expression. `let x = if {} else {}` etc... And you can skip calling return explicitly in functions.
      As for shadowed variables and constructors, its not very difficult though it can feel awkward at first. Luckily, rust has its syntactic sugar like User {name: name, email: email} can become User {name, email} which generally pushes you toward using the same names for vars and arguments. Personally, I think its more readable in many respects since you don't have to consider alternative names.
      As with any language that features shadowing, the var lower down the scope takes precedence. let x = 10; will be replaced by let x = 11; later with the first x being dropped from memory entirely. Its not all that different from when you use the `this` or `self` keyword in other OOP languages.

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

      @@TensorProgramming From a programmers point of view there's a certain convenience to it, but being a professional programmer, I'm thinking more about code reviews.
      Where C/C++ code spells out what it is doing and can therefore be subjected to code review, Rust seems to be a lot of "reading between the lines". A code review becomes tedious and ineffective if you have to run through a whole laundry list of things that the compiler/run time may have or may not have implicitely done.
      How do you do an effective code review in a critical production environment, where the 4-eyes principle is mandated?

  • @robertdowneysr_
    @robertdowneysr_ 4 ปีที่แล้ว

    fn main ()
    {
    let x : u8 = 10;
    let y : u8 ;
    y = x ;
    print! ("x is {}
    ", x);
    print! ("y is {}
    ", y);
    }
    why is it not showing error even if x lost the value

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

      Its not "losing the value" since its a primitive. The 10 from x is being copied to y in this case so both have the value 10 in them.

  • @johvahn14
    @johvahn14 6 ปีที่แล้ว

    What are the extensions you use?

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

      In general or for Rust? For Rust I am using RLS and Racer, there is a video that talks about this on the intro to rust playlist here: th-cam.com/video/zSIZB8WWa-o/w-d-xo.html. I use a fair few extensions for everything else however. The theme is the Spacemacs Dark Theme and the font I use is Fira Code with Code Ligatures enabled.

    • @johvahn14
      @johvahn14 6 ปีที่แล้ว

      Thanks, Love the videos

    • @GurpreetKakar4873
      @GurpreetKakar4873 6 ปีที่แล้ว

      I use same extensions and same fonts but I suggest check out Plastic theme for Vscode.Much better color support imo.

    • @TensorProgramming
      @TensorProgramming  6 ปีที่แล้ว

      Ill take a look. I like the Spacemacs theme simply because its very readable on these screen casts. If the Plastic theme is more readable in a video then I definitely will use it. Thanks.
      Edit: It seems like a nice theme, but for some of the languages that I am using the theme shows a lot of grey on black text. This could be potentially bad for video tutorials. Ocaml and Dart seem to have this problem on some of there components. I would have to look at Rust, Go and Elixir to see if this is an issue there as well. Perhaps, I will take the black background and use it with the syntax coloring of the Spacemac dark theme.

    • @GurpreetKakar4873
      @GurpreetKakar4873 6 ปีที่แล้ว

      Yeah it's personal opinion but I like it.

  • @thedevhacker4608
    @thedevhacker4608 5 ปีที่แล้ว

    WoW this was difficult

    • @TensorProgramming
      @TensorProgramming  5 ปีที่แล้ว +4

      What parts did you find difficult? Perhaps I can clarify some of them.
      Rust follows a paradigm that is splits the data from the behavior of an "Object". In a language like Java for instance, you would have a single class that contains both the data and the behavior but in Rust, you have a struct which defines the shape of the data and then you have an implementation block which defines behavior. Then you've got traits which are similar to interfaces in other languages. They are an interface of functions that can be implemented on multiple different types. Since you separate the data from the behavior, you can also just have a data structure without the behavior or behavior without a data structure (in the form of a trait).

    • @thedevhacker4608
      @thedevhacker4608 5 ปีที่แล้ว

      @@TensorProgramming thanks bro ❤️❤️

  • @Elite7555
    @Elite7555 6 ปีที่แล้ว

    Ok, that style of implementing methods looks very much like golang. Could anybody explain to me, why that is preferable to classes? In a class, I would have a constructor method and static methods which serve the same purpose as your new method.
    That looks like much boilerplate and chaotic code, when anybody anywhere can implement new methods.

    • @TensorProgramming
      @TensorProgramming  6 ปีที่แล้ว +7

      The main reason why you would use a class in general is for polymorphism and shared behavior via inheritance. While inheritance is a tool to solve polymorphism, it isn't necessarily the most elegant one. Rust''s approach of separating data (Structs and Enums) from behavior (Impl) and using traits to group behaviors for polymorphism is a paradigm shift from your typical java/c++ style inheritance-heavy model and of course it requires some getting used to.
      "Composition over Inheritance" is the main philosophy behind this style of Polymorphism. Also, Enums in Rust nicely solve closed polymorphism problems in a very simple way where many OOP languages would require a ton of boilerplate. The separation of data from behavior also has its own set of advantages; for instance, passing data over foreign function interface boundaries becomes much more natural with this style of writing data and behavior. (You can just implement a trait for instance)
      Rust also has the explicit self arguments and methods which is somewhat orthogonal to the whole classes vs no classes argument. it is very useful to have an explicit self argument that can be passed in multiple different ways (self vs &self vs &mut self etc). This is kind of similar to the c++ const and mutable specifiers but a little bit more flexible in my opinion.
      Part of it will come down to opinion as with any programming language but I think if you give it a chance you will see that it is a rather elegant solution (especially when you start to look at Traits, Enums and generics). Traits can be implemented via declarative annotation macros which really cuts down the amount of potential boilerplate and helps justify this model and Enums are extremely powerful in relation to the full featured pattern matching that exists in the Rust language.
      Rust also used to have classes but they removed them because they were redundant and if you really wanted to re-extend the behavior of classes you could do so through the Rust Macro system. You also have to keep in mind that these are just examples and they by no means showcase the full strength of the data/behavior model in Rust. Look at some of the later project videos for a more complete showcase of this.
      For my own experience, I do like inheritance for some use cases but also I do like the way that Rust handles things. It really will depend on where I chose to use each of these languages and for what. I do dislike how many OOP languages obfuscate the data where as Rust certainly takes a much more data-centric approach. Coming from a background where I worked for years with languages like C++, Java and C, Rust's approach is a breath of fresh air in many ways. My advice of course, is to look at the whole tool kit before rushing to judgement (Structs, Traits, Enum and Impl blocks etc).
      Anyways, Cheers mate.

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

      I really would love a video from you about your views on OOP. I am not really asking you to give a definitive answer but I feel like Data-Oriented Programming might be the better way atleast for somethings and still I am not sure.
      If you share your insight I think it will be extremely helpful for the community. And especially me.

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

    It's like reading the Bible - every time I watch, I never fail to understand more.