Right now range is not extremely useful—it can yield an iterator from a to b (exclusive), and that’s it. Quite often more complex ranges are needed—ones that have no upper limit, ranges that need to be inclusive of the upper bound, steps other than 1, and so on. Macros are great for this! It’s quite simple to design a nice range macro:
macro_rules! range {
(.. $to: expr) => {
std::iter::range(std::num::zero(), $to)
};
(... $to: expr) => {
std::iter::range_inclusive(std::num::zero(), $to)
};
(.. $to: expr, $step: expr) => {
std::iter::range_step(std::num::zero(), $to, $step)
};
(... $to: expr, $step: expr) => {
std::iter::range_step_inclusive(std::num::zero(), $to, $step)
};
($from: expr .., $step: expr) => {
std::iter::count($from, $step)
};
($from: expr .. $to: expr) => {
std::iter::range($from, $to)
};
($from: expr ... $to: expr) => {
std::iter::range_inclusive($from, $to)
};
($from: expr ..) => {
std::iter::count($from, std::num::one())
};
($from: expr .. $to: expr, $step: expr) => {
std::iter::range_step($from, $to, $step)
};
($from: expr ... $to: expr, $step: expr) => {
std::iter::range_step_inclusive($from, $to, $step)
};
}
Playpen link.
This takes into account basically every type of range/counter I can think of, and it’s also got a very nice syntax (although the .. vs. ... distinction could be confusing, but Ruby uses it too IIRC). If this were added to std::macros, ranges would be a lot nicer to construct.
Thoughts?