1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-04-11 11:51:53 +02:00

Adds a comment for an iterator based implementation for Polygon::length (#950)

Outlines an alternative implementation for Polygon::length.  This exercise is aimed at novice users and we want to keep the implementation simple. For users familiar with iterator concepts this comment points to an alternative.
This commit is contained in:
Erdem 2023-07-11 01:55:33 +03:00 committed by GitHub
parent cabf98be72
commit 427aab110f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -96,6 +96,12 @@ impl Polygon {
}
result += last_point.dist(self.points[0]);
result
// Alternatively, Iterator::zip() lets us iterate over the points as pairs
// but we need to pair each point with the next one, and the last point
// with the first point. The zip() iterator is finished as soon as one of
// the source iterators is finished, a neat trick is to combine Iterator::cycle
// with Iterator::skip to create the second iterator for the zip and using map
// and sum to calculate the total length.
}
}