Rust 1.96 is out!
My favorite change is the new core::range::* types.
Today in Rust, 0..10 still creates the legacy range type.
The thing is: that type is also an Iterator, so it has iteration state. Once you start consuming it, it changes:
let mut r = 0..3;
r. next(); // Some(0)
r. next(); // Some(1)
And this is why making it Copy would be kind of dangerous, because you would be copying something that also has internal iteration state, which can get confusing pretty fast. The new core::range::Range fixes this by separating the idea of “a range” from the act of iterating over it.
It uses IntoIterator instead of being an Iterator directly, and now a range can behave more like plain data: just start and end.
This makes it possible to store ranges inside small Copy types, like spans, slice accessors, parser tokens, compiler ranges, editor selections, and similar stuff.
Small change, but very Rust!
ALT use core::range::Range;
#[derive(Clone, Copy, Debug)]
struct Span(Range<usize>);
impl Span {
fn text(self, input: &str) -> &str {
&input[self.0]
}
}
fn main() {
let span = Span(Range { start: 0, end: 4 });
let copied = span;
let text = "Rust 1.96";
println!("{}", span.text(text));
println!("{}", copied.text(text));
}