Adding Functionality to Data Types - Idiomatic Rust in Simple Steps part 9

Поделиться
HTML-код
  • Опубликовано: 11 окт 2024

Комментарии • 11

  • @LoZander
    @LoZander 3 месяца назад

    I really like how you introduce Rust features and patterns in relation to each other instead of in isolation. There's a natural flow to how one leads to the other

    • @FiosQuest
      @FiosQuest  3 месяца назад +1

      Thank you! This one came together really naturally, but idioms and patterns are something I'm keeping my eye out for :)

  • @LoZander
    @LoZander 3 месяца назад

    very nice introduction to impl, generics and type state pattern (does this fall under that?)!
    If you always want a cat to be in one of these moods, another approach could be to inverse the model by making the cat type generic with a "mood". Then you can impl sleep only on Cat. This would also make all the general cat behaviour accessible no matter mood. You could then restrict this mood generic to a mood trait which you only implement on the type states :D
    Ofc. the downside of this approach is that you lose the general state machine and would have to implement the transitions on each animal, if you wanted to use the pattern on multiple animals.

    • @FiosQuest
      @FiosQuest  3 месяца назад

      Shh, you'll spoil the next one 😅 You are absolutely right but you can keep the default behaviour with Traits 😉

    • @LoZander
      @LoZander 3 месяца назад

      @@FiosQuest sorry 😅😂
      But im sure you'll put it much more eloquently than I 😊

    • @LoZander
      @LoZander 3 месяца назад

      @@FiosQuest im looking forward to seeing the next one and how you use traits in it 😄

    • @FiosQuest
      @FiosQuest  3 месяца назад +1

      @@LoZander Haha, nothing to be sorry about, I'm delighted its gelling with you 😅

  • @misterwoody_
    @misterwoody_ 2 месяца назад

    Is the idiomatic rust name for a "static type" an "associated type"? (e.g. the new function). And also a getter is called a method?

    • @FiosQuest
      @FiosQuest  2 месяца назад

      Hi there!
      I'm not sure what a "static type" would be normally. "Static typing" in languages is when a language won't allow you to change the type of a variable.
      Eg, statically typed languages, which Rust is, don't allow you to do something like:
      let a = 10;
      a = "bob"; // this won't work as a is a number
      There are many statically typed languages that do this including; C, C++, Java, Go, and more!
      Static values are achievable in Rust as well using the "const" keyword which I haven't gone over yet. There is also the concept of static lifetimes (&'static) in Rust which are given to any value that is guaranteed to always be accessible because it was built in at compile time, eg:
      let a = "hello";
      This technically has a static lifetime as the value "hello" will be part of the compiled binary and therefore always exist while the program is running.
      Static methods are also something in Rust which are functions that are attached to Types, such as "new" or other constructors which are not part of the instantiated value.
      Rust has a concept of Associated Types which are a more advanced topic where Traits can specify a type that is actually chosen when the trait is implemented. An example of this is the Iterator trait which has an associated Item type that you must explicitly decide on when implementing Iterator. See: doc.rust-lang.org/std/iter/trait.Iterator.html
      Getters are a subset of methods. Methods are functions that will work on an instantiated type (a value of a given type) and will allow you to "get" data from inside of the constructed value, usually something that might otherwise be private. This is really helpful to restrict _how_ data can be accessed and manipulated.
      I appreciate this is a fairly light touch explanation, I hope it helps. LMK if I can clarify anything further.

    • @misterwoody_
      @misterwoody_ 2 месяца назад

      @@FiosQuest Ah apologies - I meant "Static method" - so the equivalent of what would be called on a class in java or C sharp rather than an instance. That will teach me to comment at midnight :) And the rust term I think is Associated function (which is what I actually meant to write)

    • @FiosQuest
      @FiosQuest  2 месяца назад

      Ah yes, quite right! I should be careful to use the correct terminology. ☺️