Create a linked list in Rust: The RC smart pointer
ฝัง
- เผยแพร่เมื่อ 5 มิ.ย. 2024
- Here, I implement a linked list in Rust utilizing the RC smart pointer. You will learn about the linked list data structure as well as the Box and RC smart pointers in Rust.
There are many other things to learn here, such as the Iterator trait, setting up a test in Rust, and much more.
You can get the code used in this video at github.com/cudidotdev/linked-...
Timestamps
00:00 Introduction
00:08 What is a Linked List?
00:38 The Linked List and Node struct
01:25 The Box smart pointer
01:50 The Linked List and Node struct (contd.)
02:44 Getting the head of a linked list list
04:08 Pushing an element to a linked list list
04:30 The RC smart pointer
04:58 Pushing an element to a linked list list (contd.)
05:35 The RC clone method
05:45 Pushing an element to a linked list list (contd.)
06:43 Popping an element from a linked list list
07:32 Iterating through the elements in a linked list list
10:04 Verifying if an element is contained in a linked list list
11:16 Inserting ahead of a node
11:38 Deleting ahead of a node
11:49 Inserting an element in a linked list
14:49 Removing an element from a linked list
16:16 Reversing a linked list
#rust #dsa #beginnerfriendly
Thanks for watching. You can support me by buying me a coffee @ buymeacoffee.com/cudidotdev
I appreciate your support. Here's a link to the code written in this video github.com/cudidotdev/linked-list-in-rust
You can code along with me while watching the video and remember to give your feedback in the comments. Thank you
Hey good video, you're doing a great job
Thank you. This means a lot to me
Very nice content
Thanks for the nice comments 💙
Shouldn't pop return the element it pops off the list?
You are right. But after removing the element, nothing points to it, so it's dropped. It would be easier if the elements implement the clone trait. I had to do a general case scenario without using any unsafe block
@@cudidotdev why not return the RC. Would that work? I'm not at my PC right now.
Liked list is an interesting problem in Rust for sure! I think you did a decent job at tackling it. :)
I think so. You can try and let me know
@@cudidotdevI think the best way is to do the if let Some on &self.head.take(). This way, you will get ownership of the RC. Then you can call &self.head.replace() with the value obtained by calling next.
@@LtdJorge I will try that and see