How to read complicated Rust types

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

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

  • @jamese.spivak4170
    @jamese.spivak4170 2 ปีที่แล้ว +182

    I'm speechless. This was so incredibly helpful! Exactly what I was looking for and more. It's crazy how much info there is to unpack in that one example. Rust is so strict and descriptive, while not being overly verbose --- one of the reasons I'm loving this language. Anyway, really appreciate seeing your thought process as you tackle a complicated function like this and your ad-lib-ish teaching style! Looking forward to more vids

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

      your second name literally means male singer in Ukrainian
      you're welcome

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

    Sized is a marker trait, it is basically used to mark a type and say it can't be dynamic, which means that you can't have a Vec because Filter must be sized.

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

      In more detail: Sized suggests wanting to instantiate the type, which requires the compiler to know how big the type is so it can allocate enough memory. But this cannot be done for dynamic types; the exact concrete type is unknown at compile time by definition, and therefore so is its size. You can either have trait methods instantiating Self or dynamic trait objects, but you cannot have both and Sized indicates which you intend for.
      You _can_, however, only put Sized in trait bounds for the methods that need to instantiate Self, and those methods will simply be inaccessible to trait objects of that trait.

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

    Reading doc is super power that no one want to teach you because this is the source of their super power.
    I'm gonna start learning from the docs itself.

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

    YOU ARE AMAZING! Your channel is so underrated. Every video that you create is always approachable and easy to follow, even without understanding everything. Thanks so much for everything that you do

  • @foobar8894
    @foobar8894 ปีที่แล้ว +17

    Story of my life. I don't handle rejection very well and I always get into generic arguments with people.

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

      this was good lol.

  • @Said-n7o
    @Said-n7o 11 หลายเดือนก่อน

    This is gold beginners like me avoid docs as long as possible cos its seems overwhelming but you broke it down clearly cannot thank you enough

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

    The quick answer I would give for this case, is that it is a method on a trait that returns a type which has the same name as the function but in uppercase.
    In those cases, the very first thing I would check is whether the returned type satisfies the trait it is defined on, in this case whether it returns a filter. In those cases, the idea is generally that you do not care what specific filter you have, you just care that you have an object satisfying that interface, and filter is a method that takes a filter and return a filter, possibly modifying it/adding middleware in some way.

  • @MattThiessen
    @MattThiessen 26 วันที่ผ่านมา

    Wow! Learning Rust for a month now. This was very helpful.

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

    Great content!.. A video like this does more to an advanced level programming than any hard code programming video can do.. thus with a proper and effective way to look into documentation you're half way there.

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

    I like almost any content you create for us

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

    I greatly value this video for its immense usefulness, as it provides a wealth of valuable insights and information. Your generosity in sharing such valuable knowledge is deeply appreciated, and it demonstrates your commitment to empowering others through education and enlightenment.

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

    Good content. Very informative and worth a sub. I found this via the homepage. Ive been consuming some rust related content lately. Never used the language yet and Ive been put off by many cases like this. As a personal background I am a hobbyist developer with primarily java experience.

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

    I'm actually very surprized how intuitive this function looks. If you just know the consept of traits and at least heard of associated types, then this code means exactly what it's looks like:
    Self: has to implement filter trait, associatied type Error of witch has to be Rejection, and it also has to implement Func;
    F: has to implement Func with generic arg Rejection
    F::Output (associated type of Func, since we already specified that F implements Func) has to implement traits TryFuture + Send,
    F::Output::Error (associated type of TryFuture, as we manualy speccified) has to implement IsReject.
    The only two things that are not really clear for me:
    1) Why we HAVE TO speccify F::Output as TryFuture to set constraint to Error, but don't have to specify F as Func to set constraint to Output? I thought that this is because Output constrained to implement 2 traits (TryFuture + Send), but if i remove Send, it still won't compile unless I manualy speccify Output as TryFuture...
    2) Why I get an error if I remove Sized constraint from Self? I thought that the type of Self already has to be known at compile time => size also has to be known.
    Actually, I found answer to the second question: it's because trait object exist (dyn &MyTrait), so theoreticly Self can be not known at compile time. Still, a bit confusing...

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

      The answer to 1 is probably that F::Output may implement some other trait with an associated type named Error, so we have to specify which Error type we mean(in this case we mean the one associated with TryFuture)

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

    Hi Chris, I'm very thankful for the subtitles. Could you make them a bit more split please? The wall of text sometimes hides the video and it can be hard to focus to read all in once. Thank you in advance!

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

    Nice. It is also important to note that this `recover` method consumes its object, `self`.

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

    FYI at 7:04 you could have used the little arrow in the top left corner to expand the file list and opened recover.rs from there!

  • @marko-lazic
    @marko-lazic 2 ปีที่แล้ว

    Amazing videos. I am glad you are getting so many views.

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

    Love it!
    Speaking of breaking things down, I'd love for you to do a video on "how to grok the rust docs"! Half the time, I give up when I cant _effectively_ look things up in the docs. I feel like it has its own grammar so to speak.

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

      Do you have an example of something that you'd like to look up more effectively? Maybe something recently that you've tried to look up and found hard?

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

      @@chrisbiscardi For sure. Lets take the simplest example. I want to learn more about "option" (because I saw something about Some/None). I head over to docs page and searching for "option" returns 20+ item. Not knowing much I pick "std::option" (the first result for me). Ooops. Its a barren looking page with `struct` and `enum` on the sidebar.
      So, I click on both only to find link within the same page. The struct link sends me off to IntoIter, Iter etc. Lot of clicking. Finally I come back to enum which sends me to option type. Finally begins to look familiar. But now the left sidebar has 5+ items with gazillion sub items.
      Hopefully you get the pic 😂

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

    So it should be read like this?
    Self should implement Filter (with Error, where Error is Rejection) and Sized
    F should implement Func (with Rejection)
    F's associated type Output should implement TryFutere and Send
    F's associated type Output for type TryFutere and this TryFutere's associated type Error should implement IsReject

  • @Tobi-gl2lb
    @Tobi-gl2lb 2 ปีที่แล้ว

    Excellent walkthrough, thank you!

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

    What window manager are you using?

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

    Headache, this is all I can feel rn

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

    Thanks for this. The associated type thing helped me a lot!

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

    Very well explained, thanks a lot!

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

    tip with your subtitles: try and keep them the same length-ish, usually a single or half of a sentence

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

    Great video! Thank you 🙏🏾

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

    Hey Chris nice video!
    Do you know where can I find architecture books for Rust?

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

      It depends on what you're looking for, but most Rust books don't have much in the way of high level architecture. I definitely can suggest Rust for Rustaceans as a language level resource after the Rust book and other earlier resources -- rust-for-rustaceans.com/

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

      @@chrisbiscardi I'm looking for Domain Driven Design for rust or maybe Event Driven Design...? some thing like that. But I would definitely take a look at that book. Thanks man!

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

    I love your videos. I wondered how best to create a pool of Reqwest clients and map them to Axum as "with_state".

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

      The sqlx crate has a great example of a pool structure that you can stick into Axum state, and there's an Axum sqlx-postgres example in the git repo that shows how to use it. There's also crates like r2d2 which implement generic pools you can use in your application.

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

    the only confusing part is the where, yet the rust where restriction is powerful and typescript like

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

    Super helpful video !

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

    thanks, Chris! How did you got so good at 'reading' Rust :D

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

      reading more of it and writing more programs :)

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

    Would like to see a large backend of a large Website supported by this. No http response with a "hello world" string. Anything commercial shop, bank, news site, anything highly interactive, international. Any idea?

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

    "Handle rejection is gonna be that F" yep

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

    Hello there, God bless your efforts.
    I am a new sql learner having a general enquiry.
    How a company manages to write a documentation of its own sql application?
    I would be so grateful for any kind of help.

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

    You catch on really fast, it seems complex but once you learn the basics it pretty much branches into experintation

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

    I wish people would write full names for generic type variable instead of using a single capital letter.

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

      Sometimes I agree with you. Some uses of generics aren't very generic and are meant to be pretty specific. In which case more names helps.
      In the cases where they are more generic it can happen that there is no good name. In these cases it helps if you think of the single letter as standing for "a type defined by the restrictions". Instead of being defined by the concrete type, or a named variable, the single letter indicates that you can pass whatever you want in as long as it satisfies the constraints.

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

    thx

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

    You know, it could have been useful if searching and learning documentation was taught at university. But that would just be too useful. Go implement linked lists and invert them in the billionth language. It's good we have someone on TH-cam that actually shows this stuff, even if it probably doesn't need to be that complicated in the first place.

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

      computer science != software development

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

    Thank You For TeacNice tutorialng Us Brother

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

    This is some brainy shit. To be honest, I think many Rust programmers take this stuff way too far. Crazy levels of generic abstraction, but when I get the stopwatch out it factors slower than a naive implementation, which may not have been blessed with the same levels of abstraction, but is easily understood and manipulated. Still, thank you for analyzing, and getting to the bottom of things!

    • @lev-th
      @lev-th 3 หลายเดือนก่อน

      It really isn't. This is just used to express complex requirements to exclude a ton of bugs on compile time. In C for example you would pose the same requirements using a ton of if statements and documentation, which is wayyy less robust, readable and, what's funny, somewhat less performant, as those checks are done at runtime.

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

      ​@@lev-ththat sounds plausible, but there's no proof.

    • @lev-th
      @lev-th 3 หลายเดือนก่อน

      @@earx23 no proof in that conversation. I guess there is otherwise. But I wouldn't really press on that, I just like Rust's semantics and strictness in it's type system tbh

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

    Amazing channel and video, but Rust still too hard to read and fully understand. I still prefer my elixir for doing WebAPI and use rust to complement when needed it.

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

    you know that by now tho

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

    Rust looks like overcomplicated languages. So why do we need it?

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

    Feels so boring, to read and understand that code

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

      Is it supposed to be exciting?

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

      @@andrewf8366 idk , it just feels chaotic I have to go to source no I have to go to docs no source docs back and forth and it’s just tiring to even watch all that and then becomes boring cause you don’t get a lot from that

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

      @@michadarowny3811 it's library code. To me it looks comparable to highly templated C++ library code.

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

      @@michadarowny3811 It's actually pretty handy to have all that information in the function's header. This is a pretty extreme example, so it's not like you'll come across functions like this all the time. Chances are you wont even need to know about all those different constraints unless the compiler complains, in which case it will spell out in plain English which constraints haven't been met.

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

    How to read rust docs

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

    gonna be real this did not help me at all, assumes way too much knowledge

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

      What's your experience level with Rust?

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

    This is just jibberish. 😅