News for hackers. Stuff that matters.
Re:Formally Verifying Rust's Opaque Types by sophacles on Monday August 01, 2022 @01:44PM (#32309537)
Sure it there is:

    for i in 1..n {
        some_mut_arr[i] = T::default();
    }

    // OR
    mut i = 0;
    while i < n {
        do_something(some_arr[i]);
        i += 1;
    }
Re:Formally Verifying Rust's Opaque Types by solar-ice on Monday August 01, 2022 @02:35PM (#32310307)
1..n is an iterator. The while loop is torturing the poor language - it is using the wrong tools to achieve a goal that is much better achieved using the things it does have direct semantic support for.
Re:Formally Verifying Rust's Opaque Types by sophacles on Monday August 01, 2022 @03:00PM (#32310623)
I see what you mean by "semantic support" now. Fair enough that 1..n is an iterator, however it's not an iterator over the array we're manipulating.

As for the while loop "torturing the poor language" - that's you injecting some preference. It's perfectly valid rust, and there's plenty of situations that look close to what I wrote (albeit my example was distilling down those cases to the core) and which aren't easily achievable without contortion to get everything into an iterator.

Don't get me wrong - I really like iterators. Sometimes though, iterators add obfuscation, particularly after the 5th or 6th chained iterator method.

Re:Formally Verifying Rust's Opaque Types by tialaramex on Monday August 01, 2022 @05:46PM (#32312450)
Rust only really has one loop, it's named loop and it's an infinite loop (you break to leave it).

So just as Rust's "for" is syntactic sugar, so is "while", it will de-sugar into loop with an exit condition break at the start, since that's how while works in Rust.

But this sort of illustrates how limited your model is, you're assuming the way loops worked in some language you've more experience with, probably C, is just "how loops work" but it's a weird feature of one language invented decades before the present, but decades after the fundamentals. It's like fixating on how the brakes worked on the 1971 Ford Cortina. That's not "how car brakes work" it's just how the brakes worked on that particular model of car, a brand new BMW i4 and a Model T are both equally reasonable examples of "how car brakes work".