Skip to content

Commit

Permalink
fix(main): Let find_exercise work with borrows
Browse files Browse the repository at this point in the history
  • Loading branch information
shadows-withal committed Apr 21, 2021
1 parent 6177b6e commit 347f30b
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -285,14 +285,12 @@ fn spawn_watch_shell(failed_exercise_hint: &Arc<Mutex<Option<String>>>) {
});
}

fn find_exercise(name: String, exercises: Vec<Exercise>) -> 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)
})
}
Expand Down

0 comments on commit 347f30b

Please sign in to comment.