#why not working ?

47 messages · Page 1 of 1 (latest)

violet snow
#
fn main() {
    'mylabel:
        for i in 1..=10 {
            println!("{}", i)
            mylabel;
        }
}
cloud wigeon
violet snow
cloud wigeon
#

They're meant to be used with break or continue

#

Also, you don't need one here: that loop will continue looping all on its own with no label at all.

violet snow
dusky badgerBOT
#
1```
violet snow
#

now it works

#

but what is the point ?

cloud wigeon
#

? eval

'label: loop {
  for i in 0..10 {
    println!("{i}");
    break 'label;
  }
}
dusky badgerBOT
#
0
()```
violet snow
#

i see

#

but

#

i have another question

#

why ' and : used both ? : alone can mean label so why is the apostrophe there ?

cloud wigeon
#

That's ambiguous in breaks

#

?eval

let x = 'label: loop {
  loop {
    let label = 10;
    break 'label label;
  }
};
x
dusky badgerBOT
#
10```
cloud wigeon
#

In loops, break can be given a value to break with. It can also be given a label. The ' disambiguates. And since labels have a ' there, they might as well have one everywhere

#

Not sure about why we have the :. That's probably to be more similar to what people expect from C-like languages?

violet snow
#

hmmmmmmm

#

i used goto in c

#

so having : there is not an issue

#

but

#

having both colon and apostrophe

#

a little confusing

cloud wigeon
#

Bright side is, labels are really rare

#

Break/continue in nested loops is very uncommon.

Even less common is their other use

#
let x = 'label: {
  break 'label 5;
}
x
#

You can break out of normal blocks, but only if they're labelled.

#

Basically a forward-only goto, though it avoids most if the issues of that statement by being tied to a scope

rigid loom
violet snow
#

i see

#

there is a lot of ambiguity

cloud wigeon
#

😄

violet snow
#

hhh yeah

rigid loom
violet snow
#

but not an issue

#

?eval

fn main() {

    let mysentence : std::string::String = std::string::String::from("hello world");

    'start :

        for i in mysentence.chars() {
            if i == 'e' {
                std::println!("{}", i);
                break 'start;
            }
        }
}
dusky badgerBOT
#
e```
violet snow
#

thanks for the comments guys

upper charm
#

?play