[rust-dev] Request for feedback

Steve Jenson stevej at fruitless.org
Sun Dec 23 16:54:18 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). 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).
>

Unfortunately, deriving_eq doesn't work for this enum (the same error is
reported 3 times):

rustc -g -o bin/algorithms --lib crate.rc
pairing_heap.rs:14:0: 15:3 error: instantiating a type parameter with an
incompatible type (needs `copy`, got ``, missing `copy`)
pairing_heap.rs:14 #[deriving_eq]
pairing_heap.rs:15 pub enum PairingHeap<E: Copy Eq Ord> {
pairing_heap.rs:14:0: 15:3 error: instantiating a type parameter with an
incompatible type (needs `copy`, got ``, missing `copy`)
pairing_heap.rs:14 #[deriving_eq]
pairing_heap.rs:15 pub enum PairingHeap<E: Copy Eq Ord> {
pairing_heap.rs:14:0: 15:3 error: instantiating a type parameter with an
incompatible type (needs `copy`, got ``, missing `copy`)
pairing_heap.rs:14 #[deriving_eq]
pairing_heap.rs:15 pub enum PairingHeap<E: Copy Eq Ord> {
error: aborting due to 3 previous errors
make: *** [all] Error 101

Best,
Steve
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/rust-dev/attachments/20121223/e9d49235/attachment-0001.html>


More information about the Rust-dev mailing list