vec_utilities/
generation.rs

1use std::fmt::Display;
2
3pub fn arange<T>(start: T, end: T, step: T) -> Option<Vec<T>>
4where
5    T: std::ops::Add<Output = T>
6        + std::ops::Div<Output = T>
7        + std::ops::Sub<Output = T>
8        + std::cmp::PartialOrd
9        + Copy
10        + Default
11        + Display,
12{
13    if step == T::default() {
14        return None;
15    }
16
17    let mut output = Vec::new(); // Would like capacity... but numbers
18    let mut count = start;
19    output.push(count);
20
21    let step_negative = step < T::default();
22
23    loop {
24        count = count + step;
25
26        if step_negative {
27            if count <= end {
28                break;
29            }
30        } else {
31            if count >= end {
32                break;
33            }
34        }
35        output.push(count);
36    }
37
38    return Some(output);
39}