A Singly Linked List in Rust

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

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

  • @dodomorandi
    @dodomorandi 4 года назад +46

    How to fight a `malloc` troll:
    * Lure it in a cage with some juicy aligned memory
    * Close the cage
    * Leave the troll in the cage for an hour or two
    * Open the cage and `free` the troll
    * Open the cage and `free` the troll
    Thank you for your great videos Ryan!

    • @dntwantgglplus
      @dntwantgglplus 4 года назад +2

      choked on my coffee while reading this. lol

    • @moisesalec4604
      @moisesalec4604 3 года назад

      a trick: you can watch series at flixzone. Me and my gf have been using it for watching loads of movies lately.

    • @leandrokillian9202
      @leandrokillian9202 3 года назад

      @Moises Alec Yup, have been using flixzone for since november myself =)

  • @smutnesovatko
    @smutnesovatko 4 года назад +11

    Hands down best explanation of basic Rust concepts. Rust's learning curve is steep, these videos make it way easier to grip basic concepts than book. You saved me days or weeks. Thank you.

  • @el_carbonara
    @el_carbonara 2 года назад +1

    this is like gold. First time I understood what the take methods do by motivating it with std::mem::replace. found it a lot easier to follow this video than the book, thanks so much.

  • @JothikumarEkanath
    @JothikumarEkanath 13 дней назад

    Need your videos still in 2025, please make more videos. Your channel is one of the best for learning rust

  • @RuairidhKJ
    @RuairidhKJ 4 года назад +6

    Thanks for the video! I fell into that trap of trying to implement a LinkedList as my first Rust project, and it really put me off learning the language any further. I think I'll give it another shot now that I've watched this though.

  • @heater5979
    @heater5979 4 года назад

    What a great presentation. Learned a good few neat Rust tricks there even if I never want to write such a linked list. Thanks.
    I just wrote a little program that generates random mazes like those in the old Hunt the Wumpus game. 20 nodes (caves), three links (exits) out from each, 20 edges (tunnels). After I had done it I realized I had a solution to the linked list problem in Rust. But it was so simple! I just kept all my nodes in an Vec, all links are just indices into the Vec. Like an old school C programmer that did not want to use malloc.

  • @bjugdbjk
    @bjugdbjk 2 года назад

    Simply amazing man, the way u explained the diff between & and Box and the lifetimes, Thnx a ton.

  • @bkaankose
    @bkaankose 3 года назад +1

    Very informative Rust content you put here. I appreciate!

  • @sangamo38
    @sangamo38 3 года назад +1

    Correct me if I am wrong but linked list should push at the end of the current list. Usually we don't have pop in linked list but if we do its queue basically. This implementation is more close stack.

    • @Gr8putin
      @Gr8putin 2 года назад +1

      This is a linkedlist implementation of stack

  • @mysticrustacean4065
    @mysticrustacean4065 2 года назад

    A question from a newbie: Instead of using Link enum what if we had the Node struct defined as:
    struct Node {
    data : T,
    next: Option
    }
    I had been trying to implement from the aforementioned Node struct without any success.

  • @fbytgeek
    @fbytgeek 4 года назад +5

    Is there any plan to do the same livestream for Graph data structure? If not then thats fine :) just curious

    • @RyanLevicksVideos
      @RyanLevicksVideos  4 года назад +7

      I was considering doing a doubly linked list, but a graph could also be interesting!

    • @Gr8putin
      @Gr8putin 2 года назад

      @@RyanLevicksVideos please do a video on double linkedlist

  • @stephenjames2951
    @stephenjames2951 4 года назад

    Coming from years and years of c/c++ one of the most interesting aspects of rust is enums. They are different enough from enums in existing languages I almost wish they had been called something else. They remind me of custom types in elm or tagged unions in other languages even ADTs. From the name enum I would not have thought of using them as the LInk type. The level of detail in these streams is great I have learned more in the last couple of weeks from these than in all my reading over the last six months.

    • @stephenjames2951
      @stephenjames2951 4 года назад

      This sent me down a rabbit hole regarding tagged unions and c++17 feature variants which are similar but not so much.

    • @YoTengoUnLCD
      @YoTengoUnLCD 3 года назад +1

      They’re exactly ADTs.

  • @MukkaiKrishnamoorthy
    @MukkaiKrishnamoorthy 2 года назад

    Thanks a lot. I have gone your document carefully. Now this youtube link is an added bonus. How to count the length of the linked list - How do you write an iterator for this. Thanks a miliion for your efforts in educating future rust developers

    • @MukkaiKrishnamoorthy
      @MukkaiKrishnamoorthy 2 года назад

      I used you iter and implemented the length - For your feedback
      pub fn len(self)->i32 {
      let mut count1 = 0;
      let mut iter:Iter =self.iter();
      loop{
      let x=iter.next();
      match x{
      None=>return count1,
      Some(_)=> count1 = count1 + 1,
      }
      }
      }

    • @deryilz
      @deryilz 10 месяцев назад

      ​@@MukkaiKrishnamoorthy you could have just use self.iter().count()

  • @Baremutation
    @Baremutation 4 года назад

    Would it not be possible to leave new_head's next value as None, assign self.head to new_head and then assign old_head to new_head's next?

  • @TutaMuniz
    @TutaMuniz 4 года назад +1

    51:56 -> Hello from future!

  • @MrAnandml
    @MrAnandml 3 года назад

    Can we use Refcell instead of Box

  • @historicalheros9684
    @historicalheros9684 3 года назад

    self.head.take() , always make head = None , so how next_node point to previous ?

  • @ankur-dhama
    @ankur-dhama 4 года назад +1

    I don't think this is necessarlly a stack thing, even for allocating on heap the compiler needs to know the size of the allocation and computing the size of recursive struct is a non-terminating recursion. People will trip on this if they are comming from reference based languages (like java etc) where everything is a pointer so having a node inside a node means having a node pointer inside a node (and that's what the compiler error is talking about to use box)

    • @RyanLevicksVideos
      @RyanLevicksVideos  4 года назад +2

      Yes this is certainly true that it's not really directly related to the stack. I think my explanation wasn't quite clear. You are correct that the size of _any_ allocation (irrespective of where it is) must be known at compile time and that GCed languages often obscure this because the automatically allocated and deal in references on the users behalf.

  • @albertlee9592
    @albertlee9592 4 года назад

    Hi Ryan, thank you for providing awesome rust contents in your channel. Where/how can I view the livestream version of it?

  • @joelmontesdeoca6572
    @joelmontesdeoca6572 2 года назад

    Thanks for the great video

  • @showbufire
    @showbufire 4 года назад

    52:00 Hello from the future

  • @mcreyfonacier1982
    @mcreyfonacier1982 3 года назад

    I learned a lot in this tutorial to the point that I got hungry hahaha.

  • @daque1960
    @daque1960 2 года назад

    A linked list is in the std crate, instead of trying to make one why don’t people just look at the implication there? 🤔🤷. Thanks for the video. I once thought about trying to make an RBTree collection but there is a crate for that too.

  • @timanderson5717
    @timanderson5717 4 года назад

    what's the difference between todo! and unimplemented! ?

    • @RyanLevicksVideos
      @RyanLevicksVideos  4 года назад +1

      They're the same thing. `unimplemeneted!` was introduced first and `todo!` was introduced later. `todo!` is shorter and so I always use it.

    • @KevinDay
      @KevinDay 4 года назад +5

      The error message is slightly different as well now (it was not not when the PR was first introduced). `todo!()` indicates that you intend to implement it eventually, while `unimplemented!()` can be used for functions that aren't applicable to some implementations, e.g. on a platform that doesn't have some feature.

  • @AK-vx4dy
    @AK-vx4dy Год назад

    I know that is interesting structure because of Rust nature, but in current state of hardware (memory x100 slower that cpu and huge caches) using lists should be strongly discouraged, even smarter structures with pointers for N bellow 100 or even 1000 have almost no advantage.

  • @minneelyyyy
    @minneelyyyy 2 года назад +2

    Coming from C I can't stand Rust code. Everything has a weird type. Why do command line arguments need their own type (two actually)? Why do stdout and stderr need to be two separate types? Why are there so many string types that cannot be interchanged? I get having a strong type system but this is ridiculous, and it just doesn't make sense to me. It's memory safety has never been a factor for me writing code either. I've tried to use it, I really have, I tried to love it, But C pulls me back in with its simplicity every time.

    • @minneelyyyy
      @minneelyyyy 2 года назад

      TBF, writing an echo in one line made me feel very happy, ignoring the gigantic 3.7MB the binary was.

  • @bocckoka
    @bocckoka 4 года назад +4

    **&straighforward

  • @lizzienovigot
    @lizzienovigot Год назад +1

    I hate this “take” hack coz it’s a hack. The only reason one needs to do it is to satisfy borrow checker. That’s one of the cases where Rust certainly feels awkward and dogmatic
    Prove me wrong )))

  • @ABHISHEKSINGH-nv1se
    @ABHISHEKSINGH-nv1se 4 года назад

    so i need 1 hr to create a linked list in rust🤔

    • @RyanLevicksVideos
      @RyanLevicksVideos  4 года назад +4

      If you’re explaining Rust’s system for borrowing and ownership. Once you know it, it takes around a minute or so. Plus linked lists are almost _never_ used in Rust.