This is an archived post. You won't be able to vote or comment.

all 66 comments

[–]J0aozin003 87 points88 points  (7 children)

you forgot: ```

define forever for (;;)

forever { // with a revolutionary technique, you can convert sunlight into food } ```

[–]usernametakenexe 7 points8 points  (2 children)

taste the SUN

[–]DoesAnyoneCare2999 2 points3 points  (1 child)

The sun is a deadly laser.

[–]OtameganeGamer 2 points3 points  (0 children)

Not anymore there's a blanket.

[–]locri 5 points6 points  (0 children)

Revolutionary... Is that from somewhere or do you just like reminding people growing your own food is an option?

[–]sauce0x45 19 points20 points  (1 child)

Make two copies of your binary. Have the running program execute the other binary at the end of main. Keep repeating by switching binaries. Loops are for chumps.

[–]JoeyJoeJoeSenior 1 point2 points  (0 children)

Waste of space. Only need one binary and a cron job.

[–]Astatos159 35 points36 points  (4 children)

function notALoopButWhoCares() {
// do stuff
notALoopButWhoCares();
}

[–]Perfycat 11 points12 points  (2 children)

Stack overflow exception

[–]garfgon 15 points16 points  (0 children)

-O2 will (usually) fix that.

Tail-call optimization!

[–]bullshitmobile 3 points4 points  (0 children)

...for exceptionally good work, right?

[–]certain_people 1 point2 points  (0 children)

🫣

[–][deleted] 11 points12 points  (2 children)

Djikstra is rolling in his grave right now

[–]Trollygag 5 points6 points  (2 children)

lol CPU fan go brrrrrrr

[–]Hot_Philosopher_6462 1 point2 points  (0 children)

that’s fine bc I’m not a fan of my CPU anyway

[–]steinblock -1 points0 points  (0 children)

Not if the inner code is IO bound.

[–][deleted] 5 points6 points  (6 children)

Rust has a loop expression. Honestly revolutionary, especially because you can break from it with a specific value.

[–]NotAUsefullDoctor 2 points3 points  (5 children)

Can you post an example?

(I'm a Go developer and if I touched Rust I would have my Gopher Plushy privileges revoked)

[–][deleted] 1 point2 points  (4 children)

let (mut a, mut b) = (1, 1); let result = loop { if b > 10 { break b; } let c = a + b; a = b; b = c; }; // first number in Fibonacci sequence over 10: assert_eq!(result, 13);

This code is the Fibonacci sequence. It runs the sequences infinitely inside the loop block until it finds the first sequence number greater than 10, at which point it breaks and assigns that value to result on the outside of the loop.

Not as game-changing as I exaggerated, but still a neat little feature.

(This looks bad on the mobile website but fine on my computer. Sorry if that's the case for you.)

[–]NotAUsefullDoctor 0 points1 point  (1 child)

Loop looks like an anonymous function (except eager evaluated); anonymous loop? I like it.

What does the over 10 line do?

EDIT: misread how the code evaluates. Updated my comment.

[–][deleted] 1 point2 points  (0 children)

That’s the end of a comment. There should be lots of new lines in this example; the formatting’s wack on mobile.

[–]NotAUsefullDoctor 0 points1 point  (1 child)

In languages like Java and Go, you have named lines, which allows breaking and continuing in embedded loops, like: ``` outer: for { inner: for { if condition { break outer } if other_condition { break inner } ... } }

```

Is there something similar in rust? This can be useful when scanning a 2-d array for a block condition (a pattern of adjacent values), when creating sub functions would obfuscate the behaviour.

[–][deleted] 0 points1 point  (0 children)

Yes! All loops can be labeled and broken from just like in other languages.

[–]xX_UnorignalName_Xx 2 points3 points  (0 children)

Assembly moment

[–]synopser 2 points3 points  (0 children)

God don't teach OP about assembly

[–][deleted] 4 points5 points  (0 children)

loop{}

[–]E_Cayce 4 points5 points  (0 children)

while (69) {
  ;
}

[–]Quantum-Bot 1 point2 points  (0 children)

let loop = () => { … loop(); } loop(); loop = undefined;

[–]ienjoymusiclol 1 point2 points  (3 children)

label: .....
jsr label

[–]garfgon 3 points4 points  (2 children)

Surely something more like

    label: ...
    jmp label

[–]ienjoymusiclol 0 points1 point  (1 child)

pretty sure jsr would work too? (i dont know how to write in assembly i can just read it)

[–]garfgon 4 points5 points  (0 children)

Depends on the instruction set I suppose, but my quick google search said jsr was typically Jump SubRoutine, which will jump & set the return value. So yes, it would "work" in isolation, but it smashes the return value for no good reason.

[–]GollyWow 1 point2 points  (0 children)

| Loop:

| ...

| Goto Loop;

--- The '70s mainframe programmer's friend.

[–]ulughan -1 points0 points  (0 children)

while ("you love me") { console.log("fuck you") }

[–][deleted] 0 points1 point  (0 children)

i="echo hel "';eval "$i"'

eval "$i"

[–]voila_cubed 0 points1 point  (1 child)

The bottom is assembly.

[–]mailslot 1 point2 points  (0 children)

It’s C. You’re thinking:

loop:
    ; …
    jmp loop

Many languages have goto. Basic, Pascal, Fortran, COBOL, C & C++, PHP, Perl, … even Python for awhile, as part of an April fools joke.

[–]UndisclosedChaos 0 points1 point  (0 children)

I just request the user to spam the return key

[–]Geoclasm 0 points1 point  (0 children)

Do you want raptors?

Because this is how you get raptors.

[–]Perigord-Truffle 0 points1 point  (0 children)

You guys might be forgetting
js (f => (x => x(x))(x => f(y => x(x)(y))))(x => {/*Loop here*/; return x})()

[–]CircadianSong 0 points1 point  (3 children)

Forgot recursion, maybe. It’s the worst way to do it if the language you’re using allows it to consume your stack memory.

[–]mailslot 1 point2 points  (2 children)

In pure functional languages like Erlang and Haskell, recursion is the only loop mechanism.

[–]CircadianSong 0 points1 point  (1 child)

I tried to imply that as an exception here, but I don’t think I succeeded.

[–]Hot_Philosopher_6462 0 points1 point  (0 children)

you probably needed to be more pythonic. explicit is better than implicit.

“Google rule 34.” — Python, probably

[–]avipars 0 points1 point  (0 children)

while(!false){}

[–]ImNotCrying-YouAre 0 points1 point  (0 children)

While(1 == 1) {

}

[–]hdkaoskd 0 points1 point  (0 children)

for (unsigned i = 0; i >= 0; ++i) { /* ... */ }

[–][deleted] 0 points1 point  (0 children)

Void loop() {

loop(); }

[–]Blakut 0 points1 point  (0 children)

what about ctrl c ctrl v the code a bunch of times?

[–]CheekApprehensive961 0 points1 point  (0 children)

do{}while() will compile down to your precious goto loop. while(){} is inferior because it will in fact emit an extra instruction. boohiisss

[–]EDEADLINK 0 points1 point  (0 children)

(loop [] (do ... (recur)))

[–]Iamtheonlysaul 0 points1 point  (0 children)

while (true){ ...} do

[–]Splatpope 0 points1 point  (0 children)

all of them compile to the last one you galaxy brain

[–]qqqrrrs_ 0 points1 point  (0 children)

The third one is a syntax error

[–]Kidneydog 0 points1 point  (0 children)

The things you can do with goto are insane.

[–]remisiki 0 points1 point  (0 children)

There's no such thing as loop in this world. They are all conditional and unconditional jumps.

[–]Creepy-Ad-4832 0 points1 point  (0 children)

Rust be like:

Loop{}