I had it working without passing parameters but now I get an expected function pointer, found function item error.
Code Snippet: Creating the menu and the function it's storing
fn create_menu() -> Menu {
let mut menu = Menu::new("Main Menu".to_string(), "Make your selection: ".to_string());
menu.add_item(MenuItem::new('A', "Add Movie".to_string(), Some(do_add))); // Here's the error
menu.add_item(MenuItem::new('E', "Edit Movie".to_string(), None));
menu.add_item(MenuItem::new('D', "Delete Movie".to_string(), None));
menu.add_item(MenuItem::new('V', "View Movie".to_string(), None));
menu.add_item(MenuItem::new('S', "Search Movies".to_string(), None));
menu.add_item(MenuItem::new('Q', "Quit".to_string(), None));
return menu;
}
fn do_add(movies: &Vec<MovieModel>) {
println!("Doing nothing");
}
Code Snippet: the menu item:
use crate::{model::MovieModel, output};
#[derive(Clone)]
pub struct MenuItem {
option: char,
text: String,
action: Option<fn(Vec<MovieModel>)>,
}
impl MenuItem {
pub fn new(option: char, text: String, action: Option<fn(Vec<MovieModel>)>) -> MenuItem {
return MenuItem {
option, text, action
};
}
pub fn get_option(&self) -> char {
return self.option;
}
pub fn get_action(&self) -> Option<fn(Vec<MovieModel>)> {
return self.action;
}
pub fn render(&self, new_line: bool) {
let mut message = String::new();
message.push(self.option);
message.push_str(". ");
message.push_str(&self.text);
output::plain_text(&message, new_line);
}
}
The full code can be found at GitHub - RichardGladman/movies-rust · GitHub