Easy Rust 191: Using enums as traits

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

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

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

    great share.
    Someone needs to come up with a "Patterns" book for rust. It's a unique language
    This "model with enum" pattern would be one of them, for certain.
    Option::as_ref() and Result::as_ref() would be another commonly rust pattern not discussed much. It's really a fundamental pattern, yet not discussed much.
    There's an "enum_dispatch" crate you should look into if not familiar with it.
    Ties the trait to the enum, that way you still have the benefits of the trait, and it's also fast.
    Here's a snippet of code from the crate docs
    #[enum_dispatch]
    trait KnobControl {
    //...
    }
    #[enum_dispatch(KnobControl)]
    enum Knob {
    LinearKnob,
    LogarithmicKnob,
    }

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

      True, plus patterns are super fun to explore so it would be an engrossing read.
      There was a small discussion about patterns yesterday and I think two videos from now I'll talk about this state pattern: mstdn.social/@Mithridates/109448071418032470
      Which includes learning more about impl blocks and how they can take generics as well, something else commonly overlooked.

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

    Love this, thanks!

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

    Thank you!👍

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

    thank you ❤

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

    Thanks!

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

    Great video!