Custom extractors in Rust?

I like scala’s extractors (“unapply”) and I think they would be a very nice addition to Rust. I wanted to know if there was some appetite or if they were already proposed but have known issues that would make them a bad fit for Rust.

The gist, for those not familiar with scala, would be to define a custom way to destructure types so they can be used in match. As an example, there could be a

    trait Destructure<Args> {
        fn destructure(&self) -> Option<Args>; // same "magic used
           // in the new Fn/FnMut/FnOnce traits where Args is a tuple
          // of any arity that contains the results of the destructuring
    }

and “destructure” could be called when destructuring that type:

    match instance_of_my_type {
        (a, b, c) /* Args */ => (),
        // ....
    }

Motivation for this would be creating a general and elegant solution to scenarios like this one: http://www.reddit.com/r/rust/comments/2d7rrj/bit_level_pattern_matching/ or even a general solution to destructuring of smart pointers as in this other thread http://www.reddit.com/r/rust/comments/2cjii7/matching_with_smart_pointers_is_unergonomic/

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.