nannou-org / nannou

A Creative Coding Framework for Rust.
https://nannou.cc/
6.05k stars 306 forks source link

app.window_rect().x.step_by(50) fails as iterator #938

Closed npetrangelo closed 1 year ago

npetrangelo commented 1 year ago

I am trying to draw a row of ellipses and this for loop will not compile, complaining that the returned pub struct Range<S = scalar::Default> does not satisfy the Iterator trait bound, even though to my understanding, Iterator is supposed to be implemented for all Ranges.

for i in app.window_rect().x.step_by(50) {
    draw.ellipse()
        .color(RED)
        .x_y(i as f32, -100.0);
}

The rest of my project can be found here.

npetrangelo commented 1 year ago

I'm not sure there's really an issue with this library or Rust itself, but it is very unclear what the idiomatic way is to loop over this range with a custom step.

npetrangelo commented 1 year ago

I found a solution, although I wish I could directly cast a Range to a Range.

let w = app.window_rect().w() as i32;
for i in (-w..w).step_by(100) {
    draw.ellipse()
        .color(RED)
        .x_y(i as f32 - 200.0, -100.0);
}