0
0
Rustprogramming~20 mins

Assignment operators in Rust - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Rust Assignment Operator Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
What is the output of this Rust code using assignment operators?

Consider the following Rust code snippet. What will it print?

Rust
fn main() {
    let mut x = 5;
    x += 3;
    x *= 2;
    println!("{}", x);
}
A16
B10
C13
D8
Attempts:
2 left
๐Ÿ’ก Hint

Remember that += adds and assigns, and *= multiplies and assigns.

โ“ Predict Output
intermediate
2:00remaining
What value does variable y hold after this code?

Analyze the Rust code below and determine the final value of y.

Rust
fn main() {
    let mut y = 10;
    y -= 4;
    y /= 3;
    println!("{}", y);
}
A3
B2
C1
D0
Attempts:
2 left
๐Ÿ’ก Hint

Integer division truncates towards zero in Rust.

โ“ Predict Output
advanced
2:00remaining
What is printed by this Rust code using bitwise assignment operators?

Look at the Rust code below. What will it print?

Rust
fn main() {
    let mut z = 0b1100u8; // binary 12
    z &= 0b1010; // binary 10
    z |= 0b0101; // binary 5
    println!("{:b}", z);
}
A1101
B1000
C1111
D1010
Attempts:
2 left
๐Ÿ’ก Hint

Apply bitwise AND then OR step by step on the binary numbers.

โ“ Predict Output
advanced
2:00remaining
What error does this Rust code produce?

Examine the Rust code below. What error will it cause when compiled?

Rust
fn main() {
    let x = 5;
    x += 1;
    println!("{}", x);
}
Ano error, prints 6
Berror: expected `;` after expression
Cerror: mismatched types
Derror: cannot assign twice to immutable variable `x`
Attempts:
2 left
๐Ÿ’ก Hint

Variables are immutable by default in Rust unless declared mut.

๐Ÿง  Conceptual
expert
3:00remaining
How many times is the variable a updated in this Rust code?

Consider the following Rust code snippet. How many times is the variable a updated (assigned a new value) during execution?

Rust
fn main() {
    let mut a = 1;
    a += 2;
    a *= 3;
    a -= 4;
    a /= 2;
    a %= 3;
    println!("{}", a);
}
A5
B4
C6
D7
Attempts:
2 left
๐Ÿ’ก Hint

Count each assignment operator usage as one update.