Boot.dev
Published in

Boot.dev

Loops in Rust; Breaking From Nested Loops

Standard For-Loop

fn main() {
for x in 0..10 {
println!("{}", x);
}
}
0
1
2
3
4
5
6
7
8
9
for var in iterator {
// do stuff
}
for i := 0; i < 10; i++ {
fmt.Println(i)
}

Continue and Break

for x in 0..10 {
if x > 5 && x < 7 {
continue
}
println!("{}", x);
}
0
1
2
3
4
5
7
8
9
for x in 0..10 {
if x > 5{
break
}
println!("{}", x);
}
0
1
2
3
4
5

Working With Nested Loops

'outer: for x in 0..5 {
for y in 0..5 {
if y > 2{
break 'outer
}
println!("x: {}, y: {}", x, y);
}
}
x: 0, y: 0
x: 0, y: 1
x: 0, y: 2

Thanks For Reading

--

--

Follow for articles about Golang, Python and cloud-native back-end development.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Lane Wagner

I love Go and Rust, and I like JavaScript and Python. I’m indiehacking on https://boot.dev when I’m not with my wife and daughter.