I'm reading Learning Rust With Entirely Too Many Linked Lists and I'm confused about why the linked list (stack) needs a destructor.
I think when the list value is out of the scope, the list itself, and all nodes would be clean up. Is it just for demonstration?
I benchmarked the version with and without a manual destructor and I found the "without destructor" one has better performance:
for _ in 1..30000000 {
let mut list = List::new();
list.push(1);
assert_eq!(list.pop(), Some(1));
}
With manual destructor:
real 0m11.216s
user 0m11.192s
sys 0m 0.020s
Without manual destructor:
real 0m9.071s
user 0m9.044s
sys 0m0.004s
See Question&Answers more detail:os