0
0
Rustprogramming~10 mins

Loop with break in Rust - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to break the loop when the counter reaches 3.

Rust
let mut count = 0;
loop {
    count += 1;
    if count == [1] {
        break;
    }
}
println!("Count stopped at {}", count);
Drag options to blanks, or click blank then click option'
A10
B3
C5
D0
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using a number other than 3 causes the loop to break too early or too late.
2fill in blank
medium

Complete the code to break the loop when the input number is zero.

Rust
let mut input = 5;
loop {
    if input == [1] {
        break;
    }
    input -= 1;
}
println!("Loop ended because input is zero.");
Drag options to blanks, or click blank then click option'
A5
B1
C-1
D0
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using 1 or -1 instead of 0 causes the loop to break at the wrong time.
3fill in blank
hard

Fix the error in the loop to break when the variable reaches 4.

Rust
let mut num = 0;
loop {
    num += 1;
    if num == [1] {
        break;
    }
}
println!("Stopped at {}", num);
Drag options to blanks, or click blank then click option'
A4
B3
C5
D0
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using 3 or 5 causes the loop to break too early or too late.
4fill in blank
hard

Fill both blanks to create a loop that breaks when the counter is less than 2.

Rust
let mut counter = 5;
loop {
    counter -= 1;
    if counter [1] [2] {
        break;
    }
}
println!("Counter stopped at {}", counter);
Drag options to blanks, or click blank then click option'
A<
B==
C2
D3
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using '==' instead of '<' changes the stopping condition.
Using 3 instead of 2 breaks the loop at the wrong time.
5fill in blank
hard

Fill all three blanks to break the loop when the value is greater than 7 and print the value.

Rust
let mut value = 0;
loop {
    value += 2;
    if value [1] [2] {
        break;
    }
}
println!("Value is [3]", value);
Drag options to blanks, or click blank then click option'
A<
B>
C7
D{}
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using '<' instead of '>' changes the condition.
Forgetting '{}' in the print statement causes no value to show.