0
0
Rustprogramming~10 mins

For loop 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 each number from 1 to 3.

Rust
for [1] in 1..4 {
    println!("{}", [1]);
}
Drag options to blanks, or click blank then click option'
Ai
Bval
Cx
Dnum
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using a variable name that is not consistent inside the loop.
Using a variable name that is a Rust keyword.
2fill in blank
medium

Complete the code to sum numbers from 1 to 5 using a for loop.

Rust
let mut sum = 0;
for [1] in 1..6 {
    sum += [1];
}
println!("Sum: {}", sum);
Drag options to blanks, or click blank then click option'
Ai
Bx
Cnum
Dval
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using a different variable name inside the loop body.
Forgetting to declare sum as mutable.
3fill in blank
hard

Fix the error in the for loop to print each element in the vector.

Rust
let numbers = vec![10, 20, 30];
for [1] in &numbers {
    println!("{}", [1]);
}
Drag options to blanks, or click blank then click option'
Anumbers
Bi
Cnum
Dval
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using the vector name as the loop variable.
Not using a reference to the vector.
4fill in blank
hard

Fill both blanks to create a for loop that prints even numbers from 2 to 10.

Rust
for [1] in (2..11).step_by([2]) {
    println!("{}", [1]);
}
Drag options to blanks, or click blank then click option'
Anum
B2
Ci
D3
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using the wrong step value.
Using a loop variable name that conflicts with other variables.
5fill in blank
hard

Fill all three blanks to create a for loop that prints the squares of numbers from 1 to 5.

Rust
for [1] in 1..6 {
    let square = [2] [3] [2];
    println!("{}", square);
}
Drag options to blanks, or click blank then click option'
Ax
C**
D*
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using the ** operator, which does not exist in Rust.
Using different variable names inside and outside the loop.