Re:Linus Torvalds' good taste argument for linked lists, exp...
by vips7L on Sunday December 06, 2020 @08:05PM
(#25328343)
Wouldn't you end up with a Box of Null? Wouldn't it be better to:
[ Parent
| on HN ]
Re:Linus Torvalds' good taste argument for linked lists, exp...
by brundolf on Sunday December 06, 2020 @08:13PM
(#25328385)
Yeah that's true, but the problem with your version is it loses the homogeny because an empty list can't simply be a Node, it has to be represented in some other way (as an Option<Node> or something), so we're back to square one. Maybe I was wrong and this can't be done perfectly even with union types.
Re:Linus Torvalds' good taste argument for linked lists, exp...
by vips7L on Sunday December 06, 2020 @09:28PM
(#25328797)
In that case wouldn't you just:
struct List {
head: Option<Node>
}
struct Node {
value: i32,
next: Option<Box<Node>>
}
Re:Linus Torvalds' good taste argument for linked lists, exp...
by brundolf on Sunday December 06, 2020 @09:54PM
(#25328920)
Yep, but this replicates the original inelegancy (from the article): Option<Node> and Option<Box<Node>> are not the same type, and so they can't be treated the same way.
Re:Linus Torvalds' good taste argument for linked lists, exp...
by vips7L on Tuesday December 08, 2020 @08:22PM
(#25354161)
I guess you could just allocate the head node on the heap too.
I think rust probably has better tools for this.
Probably some sort of
let node = node.next;
while let Some(n) = node {
node = n.next
}