Created
July 6, 2021 16:02
-
-
Save alfonmga/c670f08260b6d4cdace3294ff30f4644 to your computer and use it in GitHub Desktop.
Rust: Iterator over floating-point range
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct FRange { | |
val: f64, | |
end: f64, | |
incr: f64 | |
} | |
fn range(x1: f64, x2: f64, skip: f64) -> FRange { | |
FRange {val: x1, end: x2, incr: skip} | |
} | |
impl Iterator for FRange { | |
type Item = f64; | |
fn next(&mut self) -> Option<Self::Item> { | |
let res = self.val; | |
if res >= self.end { | |
None | |
} else { | |
self.val += self.incr; | |
Some(res) | |
} | |
} | |
} | |
fn main() { | |
for x in range(0.0, 1.0, 0.1) { | |
println!("{} ", x); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output result: