|
|
Subscribe / Log in / New account

Key Rust concepts for the kernel

Key Rust concepts for the kernel

Posted Sep 27, 2021 20:38 UTC (Mon) by nybble41 (subscriber, #55106)
In reply to: Key Rust concepts for the kernel by error27
Parent article: Key Rust concepts for the kernel

The more readable version is always going to be the one that says what it means: a while loop for zero or more iterations, and a do/while loop for one or more. Requiring the reader to have extra context about how the function is called to determine whether variables are properly initialized before being used is *not* what I would consider "more readable". That's prioritizing a minor style preference above *correctness*.


to post comments

Key Rust concepts for the kernel

Posted Sep 27, 2021 21:56 UTC (Mon) by Wol (subscriber, #4433) [Link] (2 responses)

Aren't you missing something? Languages where the syntax is

DO ... WHILE ... UNTIL ... END DO

???

What's important (and this is where FORTRAN/Fortran failed), is that the test should be carried out where the test is written.

DO WHILE condition
statements
END DO

or

DO
statements
WHILE condition END DO

Cheers,
Wol

Key Rust concepts for the kernel

Posted Sep 27, 2021 23:13 UTC (Mon) by nybble41 (subscriber, #55106) [Link]

> What's important (and this is where FORTRAN/Fortran failed), is that the test should be carried out where the test is written.

The context is C code compiled by GCC and Clang. C doesn't have any issue with the placement of the test. A while loop puts the test at the top of the loop, and evaluates it at the top of the loop (before the first iteration of the loop body). The do/while loop places the test at the bottom and evaluates it after executing the body of the loop.

However, if you use a while loop in a context where it would be *incorrect* (as in: results in reading from uninitialized variables) to exit the loop before the body had executed at least once, there is a bug in your code, or at least the very strong *potential* for one even if you think you know that the inputs will always cause the body to be executed. In the absence of some clear human- and machine-readable indication of the initial condition, such as assert() statements, the compiler or other static analysis tool should warn about the potentially uninitialized variable(s). Even with assert() statements I would consider it better to use do/while since it better reflects the actual flow control vs. "assert(expr); while (expr) { ... }" which has the false appearance of a test and branch prior to the first iteration which will (hopefully) be removed by the compiler as dead code.

Or you could structure the code so that it reliably does the right thing even if the initial condition results in the body of the loop not being executed. That would be ideal: easier to read and reason about, while handling a wider range of inputs without risking undefined behavior.

Key Rust concepts for the kernel

Posted Sep 27, 2021 23:22 UTC (Mon) by anselm (subscriber, #2796) [Link]

Standard FORTRAN didn't even have “DO WHILE … END DO” loops (although some implementations added them as a non-standard extension). You would emulate structured loops like these with “IF” and “GOTO” .

The other thing was that it was pretty easy to get loops wrong. The moral equivalent to a “FOR” loop in FORTRAN looks like

      DO 10 I=1,10
        …
10    CONTINUE
IIRC the line with the statement label “10” doesn't have to be “CONTINUE” (which is basically a no-op which is useful to hang statement labels off of), although it makes for more readable loops. But you would have to take care not to accidentally write something like
      DO 10 I=1.10
        …
10    CONTINUE
where, since spaces inside variable names are allowed in FORTRAN and variables don't need to be explicitly declared, the compiler would assume the first line to be an assignment statement rather than the beginning of a loop (having a “10 CONTINUE” statement without a corresponding “DO” isn't an error, either). FORTRAN 77 changed the syntax of “DO” to optionally allow
      DO 10, I=1,10
which would prevent the error, and back in a previous life when I was the TA for a FORTRAN course at the university (I was young and needed the money) I would heavily penalise students who didn't avail themselves of this safety measure.


Copyright © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds