Hacker News new | past | comments | ask | show | jobs | submit login
Rust, Generics, and Collections (cglab.ca)
187 points by Gankro on Dec 1, 2014 | hide | past | favorite | 10 comments



Ha, that trick with `Map<T, ()>` is actually really neat, and I'm happy to see that there's no qualms about its efficiency that would preclude its use in the standard library. This post has inadvertently become the most thorough documentation on zero-sized types that I know of, in addition to being the gold standard for guiding people interested in implementing data structures in Rust. :)


And I completely forgot about how they can be used to give collections "chunks" of code to run without the need for function pointers!

And how you can use unboxed closures with no captures to make a zero-sized bundle of static-data.

Ah well. More stuff for part-2.


Interestingly, if we limit ourselves to pure functions, a ZST corresponds to category-theoretic concept of the terminal object. The latter is defined as such an object (think "type") T that for any other object (think "another type") X there is precisely one arrow (think "function") from X to T [1]. Indeed, if T is a ZST like the empty tuple () then for any type of argument there is precisely one function to T: namely, the one returning T's sole value, ().

Also, Result<T, E> is an example of a variant type corresponding neatly to co-products [2].

[1] http://en.wikipedia.org/wiki/Initial_and_terminal_objects

[2] http://en.wikipedia.org/wiki/Coproduct


It'd be nice if the examples had syntax highlighting.


Done.


[deleted]


Fixed. Although for some reason geddit froze up trying to save and it was briefly So Much Worse.


Hey @Gankro, your blah/ dir doesn't have an index of all files. Unless you know the links for these Rust posts, you can't really navigate to them. Especially if you keep posting, which you should, I'd keep in mind the navigation to and from these rust-lang posts.


There is a blah/rss.xml, but yeah that's on my TODO. Maybe for the next post.


Why not use `fold` to implement `find_min`?


Well I mean if you want to cheat with iterator adaptors you can just use min_by: http://doc.rust-lang.org/std/iter/trait.IteratorExt.html#tym...

:P

(shhh I was tired and trying to come up with a simple but moderate complexity example)




Consider applying for YC's W25 batch! Applications are open till Nov 12.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: