[rust-dev] Request for feedback

Patrick Walton pwalton at mozilla.com
Fri Dec 7 12:54:31 PST 2012


On 12/7/12 11:58 AM, Steve Jenson wrote:
> Hi rust gurus,
>
> Today I ported the purely functional Pairing Heap found in Okasaki's
> Purely Functional Data Structures to Rust. I was hoping that some of you
> might take a look at it and give me feedback on where I could be using
> Rust's idioms better.
>
> https://github.com/stevej/rustled/blob/master/pairing_heap.rs
>
> The code I wrote is a little longer than Okasaki's example, mostly due
> to Standard ML's more concise pattern matching. (see page 54 for
> comparison) Is there a way to do pattern matching in argument lists as
> in Haskell or SML?

Yes, in Rust 0.5 this works.

I noticed several things:

* Using explicit self (&self) will help make your levels of indirection 
consistent between `self` and `other` in a few functions. This works 
better in 0.5 than it does in 0.4.

* Braces aren't necessary after the => in patterns unless you want 
multiple statements.

* I'm confused as to why you need an @record as your type in 
PairingHeap_ (note that records are deprecated in favor of structs). In 
Rust 0.5 you can say

     pub enum PairingHeap<E:Copy Eq Ord> {
         Empty,
         PairingHeapCell {
             head: E,
             rest: @List<PairingHeap<E>>
         }
     }

* You can use "self" as the return value in a trait.

* In 0.5 you can use #[deriving_eq] for your enum to avoid writing the 
Eq definition, although I'm not sure that works for struct-like enum 
variants as in PairingHeapCell above (I should check this).

* @ signs are required for pattern matching because pattern matching 
never dereferences through pointers implicitly.

* You have a bunch of "return" expressions in which "return" can be left 
off.

* Some of your if statements could be replaced with pattern guards.

* is_empty() could just be:

    pure fn is_empty(&self) -> bool { *self == Empty_ }

Patrick


More information about the Rust-dev mailing list