Replies: 1 comment 2 replies
-
|
The loop has type fn looping() -> string {
let mut i = 0
loop {
if i > 3 { break "Exited" }
i += 1
}
}You can also do this: fn looping() -> string {
let mut i = 0
let result = loop {
if i > 3 { break "Exited" }
i += 1
}
fmt.Println("Loop returned:", result)
result
}The basic That means that this will compile: fn foring() {
for _ in 0..5 {}
}But this wont fn foring() -> string {
for n in 0..5 {
if n == 30 { return "Exited" }
}
}You need to provide a default value if you want to "exit early" fn foring() -> string {
let s = "Default return value"
for n in 0..5 {
if n == 3 { return "Exited" }
}
s
}In practice |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I have following code:
and it fails to compile:
IMHO, it should be treated as a
loop, not an endingfor. Am I wrong?Beta Was this translation helpful? Give feedback.
All reactions