![]() ![]() Since VBTWADI is exactly the kind of thing that actually makes using a linked-list desirable over an array, behaving poorly on this case would be a bit of a disappointment. Box is able to run the drop implementation of its contents in-place, so it doesn't suffer from this issue. This could be very expensive if we generalize our list and someone uses it to store instances of VeryBigThingWithADropImpl (VBTWADI). So our implementation only moves around pointers to nodes, while the pop-based one will move around the values we stored in nodes. Pop returns Option, while our implementation only manipulates Links ( Box). Generalizing our list to store things other than integers? Click to expand for answer It different, and what performance issues could result from it once we start ![]() ![]() Manually implementing Drop for our List as the compiler would: impl Drop for List, which is certainly simpler. Incorrect! To see why, let's try to write what the compiler has to do, by Language would ensure that such code wouldn't blow the stack". Some of you might be thinking "this is clearly tail recursive, and any decent Recursive code, and recursive code can blow the stack! Some of you might rightly be getting nervous. When list gets dropped, it will try to drop A, which will try to drop B, Let's consider a simple list: list -> A -> B -> C The automatic handling is going to be bad. List, all it would want to do is drop its head, which in turn would maybe You don't actually need to implement Drop if you contain types that implementĭrop, and all you'd want to do is call their destructors. The Drop trait has the followingīasically, "when you go out of scope, I'll give you a second to clean up your Traits are Rust's fancy term for interfaces. A type has a destructor if it implements a trait called Drop. Like C++, Rust uses destructors to automatically clean up resources when they'reĭone with. We can make a stack, push on to, pop off it, and we've even tested that it allĭo we need to worry about cleaning up our list? Technically, no, not at all! ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
January 2023
Categories |