在Rust中,实现简单FSM的最惯用方法是什么?

时间:2016-08-24 18:40:18

标签: generics rust

在学习Rust时,我正在尝试实现一个简单,干净的FSM,与我在Go中多次使用的this example非常相似:

package main

import (
    "fmt"
)

type StateFn func(interface{}) StateFn

func First(interface{}) StateFn {
    fmt.Println("First")
    return Second
}

func Second(interface{}) StateFn {
    fmt.Println("Second")
    return nil
}

func main() {
    for state := First(nil); state != nil; state = state(nil) {}
}

我希望有类似干净和富有表现力的东西,理想情况下是通用的,这样我就不必在func参数中使用Rust等效的interface{}

此问题是How can I define a Rust function type which returns its own type?的扩展名。

0 个答案:

没有答案
相关问题