The Box Smart Pointer in Rust

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

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

  • @letsgetrusty
    @letsgetrusty  3 ปีที่แล้ว +12

    📝 Get your *FREE Rust cheat sheet* : www.letsgetrusty.com/cheatsheet

  • @long-live-linux
    @long-live-linux ปีที่แล้ว +5

    The following code works:
    #[derive(Debug)]
    enum List

  • @sagnikbhattacharya1202
    @sagnikbhattacharya1202 3 ปีที่แล้ว +43

    Please cover RefCell :)

  • @GolangDojo
    @GolangDojo 3 ปีที่แล้ว +61

    Golang beginners - Pointers are so hard to understand
    Rusty Bogdan - Regular pointers aren't cool enough

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

      so true

    • @mindmaster064
      @mindmaster064 3 ปีที่แล้ว +8

      If you ever had any problems in your code with dangling pointers you don't miss 'em. I'll stay happy with the little Box and be even happier I never have to deal with that mess. What you won't use in this is the List enum because linked lists are hot garbage and there is absolutely zero use case except for some kludge-y recursive algorithms. Using Box with Vec is however the best way to use it. We all have growable Vec's that we need to pass between bits of code by reference. I think linked list information really should be included in another topic specifically on recursion where it is most likely to be used, because for the rest of the programming you ever do you just won't care about it or use it.

  • @thingsiplay
    @thingsiplay 3 ปีที่แล้ว +21

    You do a great job in explaining these things. Thank you for the videos.

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

    Congrats! You are half way through the tutorial series! Keep up the momentum!
    Thanks for the great videos bogdan!

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

      this comment actually made me very happy, thanks!

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

    Didn't even want to learn Rust... But, the presentation and bite sized nature is easily digestable!

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

    Seems like you could also get away with using Option right? Is Option a smart pointer type as well since it can be None?
    EDIT: Nope. Option needs to know the size of T

    • @MK-fd3gz
      @MK-fd3gz 2 ปีที่แล้ว +2

      Option perhaps.

  • @babajagaisyou
    @babajagaisyou ปีที่แล้ว +11

    I know that you are calling this a "cons list" but this is just an old school linked list. Cons might be from lisp, but linked lists are language agnostic

    • @dagoberttrump9290
      @dagoberttrump9290 6 หลายเดือนก่อน

      it's not an old school linked list. linked list consists of a accessory struct and nodes, whereas each node points to the next one. in a cons list, each list points to another or no list

    • @babajagaisyou
      @babajagaisyou 6 หลายเดือนก่อน +4

      @@dagoberttrump9290 a cons list points to another cons list or no list. so exactly like a classical linked list... (obviously a node points at another node, but structurally another node is just the start of another linked list). i think its just semantics at this point.

    • @chudchadanstud
      @chudchadanstud 2 หลายเดือนก่อน

      ​@@dagoberttrump9290So a linked list? You don't need to wrap a node pointing to another node in a struct to call it a linked list.

  • @高主任-g6j
    @高主任-g6j 3 ปีที่แล้ว +2

    A very clear explanation and a good example. Thanks for the video!

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

    i have gotten a lot out of your videos thanks dude!!

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

    That was well explained, thank you.

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

    Thanks for what your do.

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

    Why we need the use statement at 6:43? Assuming an enum is like a type, what does it mean "to bring a type into scope", and why isn't it already in scope right after being defined?

    • @dulanchampa
      @dulanchampa 3 ปีที่แล้ว +7

      Here "to bring a type into scope" means bringing the variants of enums into main scope. i.e. you can write it like: "Variant" instead of "Enum::Variant"

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

      @@dulanchampa Thanks!

  • @Michael-en7ub
    @Michael-en7ub 2 ปีที่แล้ว +6

    Interesting! But now I'm wondering: could a & reference be used instead of Box? What are the upsides to using Box in this example?

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

      Nope, you can't allocate new memory and save it on a reference, you need a box to do it

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

      @@someon3 yeah, but why not? What is the reason?

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

      ​@@lizzienovigotbecause... That's... How it's made?

    • @flippert0
      @flippert0 11 หลายเดือนก่อน +1

      @@lizzienovigot You cannot transfer ownership with a normal reference.

    • @lizzienovigot
      @lizzienovigot 11 หลายเดือนก่อน +2

      @@flippert0 I know, but thats not exactly what I asked. As far as I understood the original comment "could a & reference be used instead of Box?", it referred to the Linked List example in the video, so the question is "could linked list be written in Rust using references, not Boxes". Its not immediately obvious that you need to keep ownership, so just brushing it off with "you cannot transfer ownership with a reference" does not answer the question. Maybe I dont need to transfer ownership. Maybe I do, but its not obvious.

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

    Learning and enjoying your videos 👍 I think none would be the proper syntax here. No nils in Rust

  • @1234fewgfwe
    @1234fewgfwe 4 หลายเดือนก่อน

    THE RUST GANG!!

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

    more usefull use of Box is using traits, when you just store object as trait

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

    Awesome! Keep it up! Hope you will soon switch to 2 videos per week :)

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

    O wow I just realized that a rust enum is basically a union

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

      What's a union?

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

      @@RenderingUser a datatype that only allocates space to hold one of its members.
      Example:
      union Foo {
      i32 x,
      i64 y,
      i128 z,
      }
      is 128 bits long
      The difference between rust enum and union is that at runtime the information which one is active is not available in a union and is often stored alongside in a separate field or manually kept track of by the developer.

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

    Doesn't an enum need additional memory to specify which of the variants is actually stored?

    • @Dan-yb1wy
      @Dan-yb1wy ปีที่แล้ว +1

      It doesn't always need extra space. For example, if you call std::mem::size_of::() it returns 8. The compiler uses null pointer optimization and uses 0 to represent the 'None' option and any other value to represent the 'Some' option so a tag field isn't needed.
      It can't do this with eg. Option because all values of u64 are valid, so the size is 16 bytes (8 bytes for u64, 1 byte for tag, and 7 bytes padding for alignment).
      It can do it for pointer-like types (eg. &T, &mut T, Box, function pointers) and numerical types where 0 isn't a valid representation, eg. core::num::NonZeroU64.
      size_of::() == 8
      size_of::() == 16

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

    I found this part of the book confusing :(, in the book it doesn't show that rust knows list is of type List, but in your VScode we can see the type, but it's not clear how it was able to infer the type. Why it doesn't treat list as "Cons" for example?

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

    What is the point of deallocating Box(5) when the program terminates??? Isn't the entire stack and heap automagically deallocated at program termination?

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

      If you have a program which runs for a long time and has lots of allocations, this is called a memory leak. Over time the entire system memory gets taken up by data which isn't accessed by anyone until the system no longer has any memory to hand out which is very bad.

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

    Amazingly good content!

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

    Thank you.

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

    I really liked your explanations. I just have a question about the recursive list structure. You mentioned at the end of the video that line 9 is not very readable. It got me thinking if there would be any other nicer way of making it more readable but using the same recursive data structure approach. Do you have any idea in mind? keep rolling the videos!! :)

    • @bebo3276
      @bebo3276 3 ปีที่แล้ว +8

      Well you could define a little helper function like this:
      fn cons(i: i32, list: List) -> List {
      List::Cons(i, Box::new(list))
      }
      and then construct the list like this:
      let list = cons(1, cons(2, cons(3, Nil)));

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

      You could also define a helper function that adds one layer, like:
      fn prepend(l: List, value: i32) -> List {
      Cons(value, Box::new(l))
      }
      let mut l = Nil;
      l = prepend(l, 5);
      l = prepend(l, 7);

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

    thanks so much!

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

    Isnt that just a link list and if not what is the difference 🤨

  • @Nico-rl4bo
    @Nico-rl4bo 2 ปีที่แล้ว +2

    7:20 getting flashbacks from functional programming pls put a trigger waring there xd

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

    But what if there are so many pointers and structures even on heap - what will be result of this situation? out of memory error? Panic? Process will be killed by os? What happens? And how set a limit for number of pointers and enums instances? For example if i want to set 100 limit and set it in "enum logic" or some structure what would wrap enum. Sorry for my English

    • @KohuGaly
      @KohuGaly 3 ปีที่แล้ว +9

      If allocation fails, rust program just terminates with out of memory error. The program can't panic, because panic needs to drop all values and drops may allocate memory (which we don't have).
      To limit the number of items in the list, you need some struct that keeps track of the length, and you need the list to be a private field of the struct, so it can't be accessed by the user directly. The creation and modification of the list needs to happen through the methods of the struct, so it can enforce the maximum capacity with custom logic.

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

      @@KohuGaly Box also has a try_new that allows you to gracefully catch allocation errors

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

    How to impl a iterator for this emum?

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

    Stupid question, but why do your VSCode suggestions have a benzene molecule?

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

      Tabnine I think
      It's an AI based tool

    • @sonicsplasher
      @sonicsplasher 9 หลายเดือนก่อน

      Github copilot wasnt available 2 years ago. TabNine was a really good AI-based suggestion tool

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

    Keep going bro

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

    Isn't a pointer to a small object, such as ints and chars, just as expensive as the object itself?

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

      It is.
      In fact, on a 64 bit architecture, a pointer (8 bytes) is larger than an i32 (4 bytes)

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

    I don't really understand the cases of using smart pointer, particularly Boxes ... Even though the explanation of writing the code is clear.

    • @ChumX100
      @ChumX100 3 ปีที่แล้ว +6

      As illustrated by the example in the video, Box is useful when the size of the data cannot be known at compile time, but a fixed size structure is required by the compiler (as in recursive structures). Now, you could use regular references in these scenarios as well, making Box redundant, but there is a key difference: Box owns the data it points to, while regular references do not. So with references, you would need to specify lifetime parameters to make sure that the reference is valid throughout your program. Box is convenient as it handles heap allocation and deallocation automatically based on variable scopes and as it implements the Deref trait, can be used as value directly.

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

      @@ChumX100 Thanks man! I got it when i checked again

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

      @@ChumX100 ​ that makes sense, but if using smart pointers is more convenient option, what are the downsides compared to lifetimes? Is it a good idea to just use smart pointers and forget about explicit lifetime annotations?
      Rust could use it internally, with some syntax sugar, and not bother users with lifetimes, but it doesn’t… I guess because smart pointers are real structs and require additional memory allocation?

  • @hcn6708
    @hcn6708 5 หลายเดือนก่อน

    The Nil variant in my project is 4 bytes ☹

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

    4:43

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

    This (and the book) bugged me because I wanted to print the list.
    let list = Cons(2, Box::new(Cons(3, Box::new(Nil))));
    print!("[");
    let mut item = list;
    let mut first = true;
    loop {
    let y = match item {
    List::Cons(a, next) => {
    item = *next;
    a
    }
    List::Nil {} => {
    println!("]");
    break;
    }
    };
    if !first {
    print!(", ");
    }
    print!("{}", y);
    first = false;
    }

  • @tech3425
    @tech3425 2 หลายเดือนก่อน

    kewl

  • @sonicsplasher
    @sonicsplasher 9 หลายเดือนก่อน

    How is this different from a linked list?

    • @chudchadanstud
      @chudchadanstud 2 หลายเดือนก่อน

      It's not. It's just rust being rust. Sometimes the book is frustrating to read because of indirection like this. 90% of the examples used are not applicable to anything.

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

    I always get this doubt are you saying "the rust programming language " or "the best programming language " ?

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

    Not that great explanation because cargo check prmoted not only box, but also rc, so it wasnt clear why you didnt use rc instead.

  • @sadiqabubakar7185
    @sadiqabubakar7185 5 หลายเดือนก่อน

    Anyone from the future still here

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

    why do you keep copying/pasting examples from the book ? come up with your ones(better ones). To me, it seems like whatever book says, it's mentioned in these videos.

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

      The title of the video series is "The Rust Lang Book", so he's basically going over whatever is in the book.