Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.
โ Incorrect
The variable num is used as the loop variable to iterate over the range 1..4.
2fill in blank
mediumComplete 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'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using a different variable name inside the loop body.
Forgetting to declare
sum as mutable.โ Incorrect
The variable i is used as the loop variable to add each number to sum.
3fill in blank
hardFix 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'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using the vector name as the loop variable.
Not using a reference to the vector.
โ Incorrect
The loop variable num iterates over references to elements in the vector numbers.
4fill in blank
hardFill 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'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using the wrong step value.
Using a loop variable name that conflicts with other variables.
โ Incorrect
The variable num iterates over the range stepping by 2 to print even numbers.
5fill in blank
hardFill 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'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using the ** operator, which does not exist in Rust.
Using different variable names inside and outside the loop.
โ Incorrect
The loop variable x is used to calculate the square using multiplication x * x.