Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
do-while loops in Rust
while {
let x = foo();
bar(x);
x != 0
} {}
@gsemet

This comment has been minimized.

Show comment
Hide comment
@gsemet

gsemet Jan 15, 2014

what is the extra {} for?

gsemet commented Jan 15, 2014

what is the extra {} for?

@bstrie

This comment has been minimized.

Show comment
Hide comment
@bstrie

bstrie Jan 15, 2014

@stibbons while loops in Rust typically look like this:

while x != 0 {
    x = foo();
}

...but note that the condition there is allowed to be any expression. And blocks are themselves expressions. So you can write the above like this:

while { x != 0 } {
    x = foo();
}

What's been done here is that they've simply moved all the normal loop logic into the condition:

while { x = foo(); x != 0 } {
    // empty loop body
}

...such that it acts just like the typical do/while loop from other languages, thereby evaluating the loop body once before testing the condition. From there it's just a simple matter to reformat for style points:

while {
    x = foo();
    x != 0
} {}

bstrie commented Jan 15, 2014

@stibbons while loops in Rust typically look like this:

while x != 0 {
    x = foo();
}

...but note that the condition there is allowed to be any expression. And blocks are themselves expressions. So you can write the above like this:

while { x != 0 } {
    x = foo();
}

What's been done here is that they've simply moved all the normal loop logic into the condition:

while { x = foo(); x != 0 } {
    // empty loop body
}

...such that it acts just like the typical do/while loop from other languages, thereby evaluating the loop body once before testing the condition. From there it's just a simple matter to reformat for style points:

while {
    x = foo();
    x != 0
} {}
@steveklabnik

This comment has been minimized.

Show comment
Hide comment
@steveklabnik

steveklabnik Jan 15, 2014

Wonder if you could macro away the extra {}

Wonder if you could macro away the extra {}

@bstrie

This comment has been minimized.

Show comment
Hide comment
@bstrie

bstrie Jan 15, 2014

No need to macro it away...

while {
    x = foo();
    x != 0
}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            {}

There you go. :)

bstrie commented Jan 15, 2014

No need to macro it away...

while {
    x = foo();
    x != 0
}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            {}

There you go. :)

@UtherII

This comment has been minimized.

Show comment
Hide comment
@UtherII

UtherII Jan 15, 2014

Arg, It's even worse : with this syntax, you don't see the dirty trick at all.
Until you try to use break or continue ...

UtherII commented Jan 15, 2014

Arg, It's even worse : with this syntax, you don't see the dirty trick at all.
Until you try to use break or continue ...

@gsemet

This comment has been minimized.

Show comment
Hide comment
@gsemet

gsemet Jan 16, 2014

i see :)

gsemet commented Jan 16, 2014

i see :)

@jrupac

This comment has been minimized.

Show comment
Hide comment
@jrupac

jrupac Jan 21, 2014

You can do the same trick in C, too:

int x;
while (
  x = foo(),
  bar(x),
  x != 0
) {}

and even weirder, like this (on some compilers):

int x;
while (({
  x = foo();
  bar(x);
  x != 0;
})) {}

jrupac commented Jan 21, 2014

You can do the same trick in C, too:

int x;
while (
  x = foo(),
  bar(x),
  x != 0
) {}

and even weirder, like this (on some compilers):

int x;
while (({
  x = foo();
  bar(x);
  x != 0;
})) {}
@jFransham

This comment has been minimized.

Show comment
Hide comment
@jFransham

jFransham Jun 15, 2015

This is an ancient thread but I thought I'd share a trick from my first rust project -- it's crazy that this still doesn't exist in stable rust (and I haven't found any mention of it in the nightly book).

macro_rules! run {
    ($x:block until $y:expr) => {{
        while {
            $x;
            !$y
            } {}
    }};
    ($x:block if_still $y:expr) => {{
        while {
            $x;
            $y
            } {}
    }};
}

Additionally, I think keyword! should be parsed seperately to keyword, so you could write this as do! while! without the compiler complaining.

(Should note that I never ended up using this macro because it was too ugly in use, so take from that what you will)

This is an ancient thread but I thought I'd share a trick from my first rust project -- it's crazy that this still doesn't exist in stable rust (and I haven't found any mention of it in the nightly book).

macro_rules! run {
    ($x:block until $y:expr) => {{
        while {
            $x;
            !$y
            } {}
    }};
    ($x:block if_still $y:expr) => {{
        while {
            $x;
            $y
            } {}
    }};
}

Additionally, I think keyword! should be parsed seperately to keyword, so you could write this as do! while! without the compiler complaining.

(Should note that I never ended up using this macro because it was too ugly in use, so take from that what you will)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment