Skip to content

Commit

Permalink
"Strings and Iterators" solution that uses let-else (google#833)
Browse files Browse the repository at this point in the history
Add "Strings and Iterators" solution that uses let-else.
  • Loading branch information
pwnall authored Jun 29, 2023
1 parent b83dd9a commit 0c867c9
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions src/exercises/day-2/strings-iterators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,25 @@
// ANCHOR: prefix_matches
pub fn prefix_matches(prefix: &str, request_path: &str) -> bool {
// ANCHOR_END: prefix_matches
let prefixes = prefix.split('/');
let request_paths = request_path
.split('/')
.map(|p| Some(p))
.chain(std::iter::once(None));

for (prefix, request_path) in prefixes.zip(request_paths) {
match request_path {
Some(request_path) => {
if (prefix != "*") && (prefix != request_path) {
return false;
}
}
None => return false,
let mut request_segments = request_path.split('/');

for prefix_segment in prefix.split('/') {
let Some(request_segment) = request_segments.next() else {
return false;
};
if request_segment != prefix_segment && prefix_segment != "*" {
return false;
}
}
true

// Alternatively, Iterator::zip() lets us iterate simultaneously over prefix
// and request segments. The zip() iterator is finished as soon as one of
// the source iterators is finished, but we need to iterate over all request
// segments. A neat trick that makes zip() work is to use map() and chain()
// to produce an iterator that returns Some(str) for each pattern segments,
// and then returns None indefinitely.
}

// ANCHOR: unit-tests
Expand Down

0 comments on commit 0c867c9

Please sign in to comment.