mirror of
https://github.com/rust-lang/rustlings.git
synced 2024-12-10 11:10:26 +02:00
fix(main): Let find_exercise work with borrows
This commit is contained in:
parent
6177b6e126
commit
347f30bd86
14
src/main.rs
14
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<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)
|
||||
})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user