#why not working ?
47 messages · Page 1 of 1 (latest)
What do you expect this to do?
this is a loop label so i expect it to starting looping after i use the loop label name
Simply writing a loop label does nothing. (Also, you didn't write it correctly: labels should always be prefixed with a ')
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.
?play
fn main() {
'mylabel:
for i in 1..=10 {
println!("{}", i);
break 'mylabel;
}
}
1```
Nested loops
? eval
'label: loop {
for i in 0..10 {
println!("{i}");
break 'label;
}
}
0
()```
i see
but
i have another question
why ' and : used both ? : alone can mean label so why is the apostrophe there ?
It can mean that in loops, but not when you use them
That's ambiguous in breaks
?eval
let x = 'label: loop {
loop {
let label = 10;
break 'label label;
}
};
x
10```
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?
hmmmmmmm
i used goto in c
so having : there is not an issue
but
having both colon and apostrophe
a little confusing
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
(which also use :)
In case it wasn't obvious, I agree having both is weird
😄
hhh yeah
is there?
not only this
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;
}
}
}
e```
thanks for the comments guys
?play