0
0
Rustprogramming~10 mins

Assignment operators 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 add 5 to the variable x using an assignment operator.

Rust
let mut x = 10;
x [1] 5;
println!("{}", x);
Drag options to blanks, or click blank then click option'
A+=
B-=
C*=
D/=
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using '-' instead of '+' will subtract instead of add.
Forgetting to make the variable mutable with 'mut'.
2fill in blank
medium

Complete the code to multiply the variable y by 3 using an assignment operator.

Rust
let mut y = 4;
y [1] 3;
println!("{}", y);
Drag options to blanks, or click blank then click option'
A-=
B/=
C+=
D*=
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using '+=' or '-=' instead of '*=' changes the operation.
Not declaring the variable as mutable.
3fill in blank
hard

Fix the error in the code by choosing the correct assignment operator to divide z by 2.

Rust
let mut z = 20;
z [1] 2;
println!("{}", z);
Drag options to blanks, or click blank then click option'
A+=
B*=
C/=
D-=
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using '*=' multiplies instead of dividing.
Using '+=' or '-=' changes the operation.
4fill in blank
hard

Fill both blanks to subtract 7 from a and then add 10 to b using assignment operators.

Rust
let mut a = 15;
let mut b = 5;
a [1] 7;
b [2] 10;
println!("{} {}", a, b);
Drag options to blanks, or click blank then click option'
A-=
B+=
C*=
D/=
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Mixing up '+' and '-' operators.
Using '*=' or '/=' instead of addition or subtraction.
5fill in blank
hard

Fill all three blanks to multiply m by 4, divide n by 2, and add 3 to p using assignment operators.

Rust
let mut m = 3;
let mut n = 8;
let mut p = 1;
m [1] 4;
n [2] 2;
p [3] 3;
println!("{} {} {}", m, n, p);
Drag options to blanks, or click blank then click option'
A+=
B/=
D*=
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Confusing the order of operators.
Using the wrong operator for the intended operation.