[rust-dev] Request for feedback

Steve Jenson stevej at fruitless.org
Sat Dec 8 10:51:31 PST 2012


On Fri, Dec 7, 2012 at 12:54 PM, Patrick Walton <pwalton at mozilla.com> wrote:

> 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<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).


I'm still learning how things are done in Rust, I cribbed the @record
inside an enum variant from libcore/dlist.rs (see DList). The library code
has been a treasure trove of ideas but the downside is that it can be hard
to form a coherent picture of how things are meant to be used in the latest
version of the language.

I switched to PairingHeapCell(E, @List<PairingHeap<E>>) which has the
benefit of working better with pattern matching.


In Rust 0.5 you can say
>
>     pub enum PairingHeap<E:Copy Eq Ord> {
>         Empty,
>         PairingHeapCell {
>             head: E,
>             rest: @List<PairingHeap<E>>
>         }
>     }
>

That looks much nicer.


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

Oh, that's just what I wanted!


> * 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.
>

Tom pointed out to me that a semi-colon at the end of an expression turns
it into a statement which is why I had switched to return statements. I'm
glad to see I can leave off both the semi-colon and the return and just use
an expression.



> * Some of your if statements could be replaced with pattern guards.
>
> * is_empty() could just be:
>
>    pure fn is_empty(&self) -> bool { *self == Empty_ }
>

Great, thanks a bunch for all this useful feedback, both to you and
Benjamin. Anything that worked with 0.4 I switched to, the rest I'll do
after 0.5 lands.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/rust-dev/attachments/20121208/bb0fe59d/attachment.html>


More information about the Rust-dev mailing list