How can I define a Rust function type which returns its own type?

burfl

I'm learning Rust, and still very much trying to get my head around it. Consider the following Go definition:

type FnType func(paramType) FnType

It's just a function that returns a function of the same type. Can something similar be implemented in Rust? And, ideally, can it be done generically, so that paramType is specified by the client?

burfl

I did some digging in the docs and took to the playground and I think I've been able to answer this myself, although it does require an intermediary type: an enum, to be specific.

fn main() {
    let mut state = State::Some(first);
    while let State::Some(s) = state {
        state = s(0)
    }
}

enum State<T> {
    Some(fn(T) -> State<T>),
    None,
}

fn first(_: i32) -> State<i32> {
    println!("First");
    State::Some(second)
}

fn second(_: i32) -> State<i32> {
    println!("Second");
    State::None
}

You can verify that it runs on the playground.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to define in swift an optional variable for a function type, which can be nil?

From Dev

How can I define a return type of void for a function in a Typescript interface?

From Dev

How can I define the return type of a lodash reduce function with Typescript?

From Dev

How can I define the return type of a lodash reduce function with Typescript?

From Dev

How can I have a function accepting a function which is the same type?

From Dev

How to make a function which returns Type <T>

From Dev

How to make a function which returns Type <T>

From Dev

Can I write a function type that returns a function?

From Dev

How can I use a callback function as a parameter, in which has its own independent parameters?

From Dev

How can I write a function have a polymorphic return type based on the type argument of its type parameter?

From Dev

How can I display the function that returns enum type data in this program

From Dev

How can I display the function that returns enum type data in this program

From Dev

Can I change an object's type inside its own methods?

From Dev

How can I define a type to present a stream which can provide integers, but also may fail?

From Dev

In PostgreSQL, How can I identify a type is composite based on its type Oid in C function?

From Dev

Can I define a function pointer pointing to an object of std::function type?

From Dev

Can I override kotlin method which returns Unit type in Java

From Dev

Can I override kotlin method which returns Unit type in Java

From Dev

Can I define a variadic template function on a single type?

From Dev

Can I define a Typescript function parameter to have a type of boolean or string?

From Dev

How can I make a Rust function accept any floating type as an argument

From Dev

How can I define a type, that represents all binary or unary function taking ArgType as arguments and returning ReturnType?

From Dev

How to define the type profile for this function?

From Dev

How to define a object type in a function?

From Dev

In Rust, how can I create task that runs in its own OS thread?

From Dev

How can I print a list with my own type of elements in OCaml?

From Dev

How can I specify a type for a function argument without restricting its dimensions?

From Dev

How do I define a samba share so that every user can only see its own home?

From Dev

How to let the return value remain generic (without casting) when a method of a class with a bounded type parameter returns a value of its own type?

Related Related

  1. 1

    How to define in swift an optional variable for a function type, which can be nil?

  2. 2

    How can I define a return type of void for a function in a Typescript interface?

  3. 3

    How can I define the return type of a lodash reduce function with Typescript?

  4. 4

    How can I define the return type of a lodash reduce function with Typescript?

  5. 5

    How can I have a function accepting a function which is the same type?

  6. 6

    How to make a function which returns Type <T>

  7. 7

    How to make a function which returns Type <T>

  8. 8

    Can I write a function type that returns a function?

  9. 9

    How can I use a callback function as a parameter, in which has its own independent parameters?

  10. 10

    How can I write a function have a polymorphic return type based on the type argument of its type parameter?

  11. 11

    How can I display the function that returns enum type data in this program

  12. 12

    How can I display the function that returns enum type data in this program

  13. 13

    Can I change an object's type inside its own methods?

  14. 14

    How can I define a type to present a stream which can provide integers, but also may fail?

  15. 15

    In PostgreSQL, How can I identify a type is composite based on its type Oid in C function?

  16. 16

    Can I define a function pointer pointing to an object of std::function type?

  17. 17

    Can I override kotlin method which returns Unit type in Java

  18. 18

    Can I override kotlin method which returns Unit type in Java

  19. 19

    Can I define a variadic template function on a single type?

  20. 20

    Can I define a Typescript function parameter to have a type of boolean or string?

  21. 21

    How can I make a Rust function accept any floating type as an argument

  22. 22

    How can I define a type, that represents all binary or unary function taking ArgType as arguments and returning ReturnType?

  23. 23

    How to define the type profile for this function?

  24. 24

    How to define a object type in a function?

  25. 25

    In Rust, how can I create task that runs in its own OS thread?

  26. 26

    How can I print a list with my own type of elements in OCaml?

  27. 27

    How can I specify a type for a function argument without restricting its dimensions?

  28. 28

    How do I define a samba share so that every user can only see its own home?

  29. 29

    How to let the return value remain generic (without casting) when a method of a class with a bounded type parameter returns a value of its own type?

HotTag

Archive