10
我試圖實現一個返回遞歸閉包的函數,雖然我不知道如何在函數簽名中表示該函數。這裏是Python中的工作實現的示例代碼返回遞歸閉包的函數簽名
def counter(state):
def handler(msg):
if msg == 'inc':
print state
return counter(state + 1)
if msg == 'dec':
print state
return counter(state - 1)
return handler
c = counter(1)
for x in range(1000000):
c = c('inc')
和Rust的僞代碼。
enum Msg {
Inc,
Dec
}
fn counter(state: Int) -> ? {
move |msg| match msg {
Msg::Inc => counter(state + 1),
Msg::Dec => counter(state - 1),
}
}