如何定义要调用的函数列表?

时间:2016-05-05 23:20:05

标签: rust

我正在尝试实施此Reddit coding challenge,但我收到一个错误,我不明白,我的搜索没有任何帮助。

#[derive(Debug)]
struct State {}

fn button_clicked(state: State) -> State {
    state
}

fn cycle_complete(state: State) -> State {
    state
}

fn main() {
    let actions = [
        button_clicked,
        cycle_complete,
        button_clicked,
        button_clicked,
        button_clicked,
        button_clicked,
        button_clicked
    ];
    let mut state = State {};
    for action in actions.iter() {
        state = action(state);
        println!("State = {:?}", state);
    }
}

当我尝试构建它时,我得到:

$ cargo build
   Compiling garageautomata v0.1.0 (file:///Users/zeisss/p/rust-example/garageautomata)
src/main.rs:41:9: 41:23 error: mismatched types:
 expected `fn(State) -> State {button_clicked}`,
    found `fn(State) -> State {cycle_complete}`
(expected fn item,
    found a different fn item) [E0308]
src/main.rs:41         cycle_complete,
                       ^~~~~~~~~~~~~~
src/main.rs:41:9: 41:23 help: run `rustc --explain E0308` to see a detailed explanation
error: aborting due to previous error
error: Could not compile `garageautomata`.

To learn more, run the command again with --verbose.

(由于代码缩短了,第41行指的是cycle_complete中第一次出现main

我正在使用Rust 1.8.0。

0 个答案:

没有答案