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: or even a general solution to destructuring of smart pointers as in this other thread

1 Like

closed #3

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