0

I am trying to implement this Reddit coding challenge, but I get an error I don't understand and my searching was of no help.

#[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);
    }
}

When I try to build this, I get:

$ 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.

(since the code is shortened, line 41 refers to first occurrence of cycle_complete in main).

I am using Rust 1.8.0.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
ZeissS
  • 11,867
  • 4
  • 35
  • 50
  • I will try the Vec solution they use, but my error is completly different. – ZeissS May 05 '16 at 23:25
  • What difference are you encountering with the error? They're using a `Vec`, but an array is essentially identical: `let actions: [fn(State) -> State; 7] = [...];` should be sufficient to make it work. – huon May 06 '16 at 03:50
  • The `Vec` worked, but IMO it should work with an array too. See the error above. – ZeissS May 06 '16 at 14:39

0 Answers0