From aa5702a0a324304c534748dd363c84bdab6dcd13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Ortega=20Froysa?= Date: Sat, 2 Jul 2022 12:10:36 +0200 Subject: [PATCH] Use while-loop to avoid unnecessary break. --- src/main.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index 91dcda6..4673916 100644 --- a/src/main.rs +++ b/src/main.rs @@ -93,17 +93,16 @@ fn main() }; // the next (first) box the prisoner is to open is the one with his number (`i`) let mut next_box:usize = i as usize; - // for every attempt the prisoner has (i.e. `size/2`) - for _j in 0..(size/2) + // while the prisoner still has attempts left AND he hasn't found his number + while prisoner_i.attempts < size/2 && !prisoner_i.found { + // count attempt prisoner_i.attempts += 1; // if the number inside the box is his number if boxes[next_box] == i { // assign `found` to true prisoner_i.found = true; - // stop looking through more boxes. - break; } else {