From 347f30bd867343c5ace1097e085a1f7e356553f7 Mon Sep 17 00:00:00 2001 From: mokou Date: Wed, 21 Apr 2021 16:21:56 +0200 Subject: [PATCH] fix(main): Let find_exercise work with borrows --- src/main.rs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/main.rs b/src/main.rs index 5af5feb..a95a09d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -201,13 +201,13 @@ fn main() { } Subcommands::Run(subargs) => { - let exercise = find_exercise(subargs.name, exercises); + let exercise = find_exercise(&subargs.name, &exercises); run(&exercise, verbose).unwrap_or_else(|_| std::process::exit(1)); } Subcommands::Hint(subargs) => { - let exercise = find_exercise(subargs.name, exercises); + let exercise = find_exercise(&subargs.name, &exercises); println!("{}", exercise.hint); } @@ -285,14 +285,12 @@ fn spawn_watch_shell(failed_exercise_hint: &Arc>>) { }); } -fn find_exercise(name: String, exercises: Vec) -> Exercise { - let matching_exercise = |e: &Exercise| name == e.name; - +fn find_exercise<'a>(name: &str, exercises: &'a [Exercise]) -> &'a Exercise { exercises - .into_iter() - .find(matching_exercise) + .iter() + .find(|e| e.name == name) .unwrap_or_else(|| { - println!("No exercise found for your given name!"); + println!("No exercise found for '{}'!", name); std::process::exit(1) }) }