0
0
Rustprogramming~10 mins

Loop execution flow 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 print numbers from 1 to 5 using a loop.

Rust
for i in 1..=[1] {
    println!("{}", i);
}
Drag options to blanks, or click blank then click option'
A10
B6
C0
D5
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using 6 will print numbers up to 6, which is one more than needed.
Using 0 will cause the loop to not run as expected.
2fill in blank
medium

Complete the code to skip printing the number 3 inside the loop.

Rust
for i in 1..=5 {
    if i == [1] {
        continue;
    }
    println!("{}", i);
}
Drag options to blanks, or click blank then click option'
A2
B4
C3
D5
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using a number other than 3 will skip the wrong iteration.
Forgetting to use continue will print all numbers.
3fill in blank
hard

Fix the error in the loop to stop printing numbers when i equals 4.

Rust
for i in 1..=5 {
    if i == [1] {
        break;
    }
    println!("{}", i);
}
Drag options to blanks, or click blank then click option'
A4
B5
C6
D3
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using 5 will print 4 before stopping, which is not desired.
Using 3 will stop too early.
4fill in blank
hard

Fill both blanks to create a loop that prints only even numbers from 1 to 10.

Rust
for i in 1..=[1] {
    if i % 2 [2] 0 {
        println!("{}", i);
    }
}
Drag options to blanks, or click blank then click option'
A10
B==
C!=
D5
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using '!=' will print odd numbers instead.
Using 5 as upper limit will miss numbers 6 to 10.
5fill in blank
hard

Fill all three blanks to create a loop that collects numbers greater than 3 into a vector.

Rust
let mut result = Vec::new();
for num in 1..=[1] {
    if num [2] [3] {
        result.push(num);
    }
}
println!("{:?}", result);
Drag options to blanks, or click blank then click option'
A5
B>
C3
D==
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using '==' instead of '>' will only select number 3, not greater numbers.
Using 3 as upper limit will miss number 4 and 5.