[][src]Function float_duration::iter::subdivide_with_step

pub fn subdivide_with_step(
    begin: FloatDuration,
    end: FloatDuration,
    steps: usize
) -> Zip<Subdivide, Repeat<FloatDuration>>

Subdivide the distance between two duration into steps evenly spaced points and include a timestep.

subdivide_with_step is equivalent to subdivide except that it returns the step size with the current time in each iteration. It is mainly a convenience function for the common case of running a simulation over discrete time steps.

It is exactly equivalent to:

use std::iter;

let steps = 100;

let sub = subdivide(begin, end, steps);
let step_size = sub.step_size();
let my_iter = sub.zip(iter::repeat(step_size));

Example usage in a simulation:

use float_duration::FloatDuration;
use float_duration::iter::subdivide_with_step;

let start = FloatDuration::zero();
let end = FloatDuration::hours(1.0);

let mut x = 5.0;
let mut v = 0.0;

for (t, dt) in subdivide_with_step(start, end, 100) {
     let a = x*x - v*x;
     let v = a*dt.as_seconds();
     let x = v*dt.as_seconds();

     println!("Position: {}", x);
}

Panics

This function panics if steps < 2 as this would violate the property that the iterator visits both endpoints.