in Julia (Julia is 1-indexed, not 0-indexed as most):
for n in 1:2:9
println(n)
end
output: 1, 3, 5, 7, 9
Proposal to Rust:
for n in start:stop {}
let a= 1:4;
for n in 1:4
{
print!("{}, ", n);
}
output: 1, 2, 3,
for n in start:=stop {}
let a= 1:=4;
for n in 1:=4
{
print!("{}, ", n);
}
output: 1, 2, 3, 4,
for n in start:step:stop {}
let a= 1:2:9;
for n in 1:2:9
{
print!("{}, ", n);
}
output: 1, 3, 5, 7,
for n in start:step:=stop {}
let a= 1:2:=9;
for n in 1:2:9
{
print!("{}, ", n);
}
output: 1, 3, 5, 7, 9
Rust already has ranges. And inclusive ranges. And can achieve stepping with the step_by iterator adapter.
@BurntSushi
inside https://github.com/rust-lang/rust/blob/master/src/libcore/ops/range.rs I have the struct Range
I use it as in https://doc.rust-lang.org/std/ops/struct.Range.html
the .. increase the start 1 by 1 until the end
in mathematics, I define the step of a progression from start until end, not 1 by 1, but I define it
I think it would be better like:
1:9 corresponds to [1, 3, 5, 7, 9]
1:<9 corresponds to [1, 3, 5, 7]
with closure
1: |x| x + 2 :9 corresponds to [1, 3, 5, 7, 9]
1: |x| x + 2 :<9 corresponds to [1, 3, 5, 7]
@cindRoberta Changing the syntax is a breaking change (which will not happen).
Also, although it's possible to do math programming in Rust, Rust is not specifically designed for mathematics. It is first and foremost a systems programming language.
Stepping is handled with iterators, like this:
(1..=9).step_by(2) // [1, 3, 5, 7, 9]
(1..9).step_by(2) // [1, 3, 5, 7]
Iterators are very powerful, they let you do many things, including mapping, filtering, chaining, folding, etc.
There's nothing preventing us from adding new syntax. We could even use editions to workaround minor breaking changes that can be automatically fixed. Nevertheless, there must strong reason the new syntax must be chosen (e.g. try and async), and IMO this new syntax a:b:c doesn't pass.
@BurntSushi
@Pauan
@kennytm
the : token instead the .. and the := token instead the ..= is just a matter of preference (because of my experience in Python and Julia, I consider the best style), it's less important for the case. Also if there is no conflict of tokens, I see no reason to not put it as an alternative
the crucial question here is to use closure to determine the step instead a 1 by 1 step, it can be using .. or : I don't care so much about it, the : instead .. is relevant for me, but I consider the closure in range, relevant to Rust
still, I understand if there is technical difficulty or performance problem (although I believe that if there is a problem implementing this in Range, it can be solved implementing a new struct)
say that 1 by 1 is sufficient and functions (closures) are not necessary in math progressions or logic is a joke and a laughing matter to mathematicals and logicals. Say that in programming is viable and understandable for technical difficulty or performance problem
1:9 corresponds to [1, 3, 5, 7, 9]
1:<9 corresponds to [1, 3, 5, 7]
with closure
1: |x| x + 2 :9 corresponds to [1, 3, 5, 7, 9]
1: |x| x + 2 :<9 corresponds to [1, 3, 5, 7]
1: |x| 2 * x * x + 3 * x + 1 :200
say that 1 by 1 is sufficient and functions (closures) are not necessary in math progressions or logic is a joke and a laughing matter to mathematicals and logicals
No one has said that. What has been said is that Rust isn't primarily intended for mathematical programming (unlike Julia or APL, for example).
the crucial question here is to use closure to determine the step instead a 1 by 1 step,
A previous comment has shown how to specify a fixed step size different than 1 via an iterator adapter (and here it is in the playground).
It's unclear to me what point about closures you are trying to make beyond that. What would the value of x be each time the closure is called in 1: |x| x + 2 :9 and how many times is the closure called?
Most helpful comment
Rust already has ranges. And inclusive ranges. And can achieve stepping with the
step_byiterator adapter.