0
0
Rustprogramming~20 mins

Integer types in Rust - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Rust Integer 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 with integer overflow?

Consider the following Rust code snippet. What will it print when run in debug mode?

Rust
fn main() {
    let x: u8 = 255;
    let y = x + 1;
    println!("{}", y);
}
A0
B256
Cpanic at overflow
D255
Attempts:
2 left
๐Ÿ’ก Hint

Think about what happens when you add 1 to the maximum value of an 8-bit unsigned integer in Rust debug mode.

โ“ Predict Output
intermediate
2:00remaining
What is the value of variable after wrapping addition?

What value does z hold after this Rust code runs?

Rust
fn main() {
    let x: u8 = 250;
    let y: u8 = 10;
    let z = x.wrapping_add(y);
    println!("{}", z);
}
A250
B4
Coverflow panic
D260
Attempts:
2 left
๐Ÿ’ก Hint

Wrapping addition means the value wraps around on overflow instead of panicking.

๐Ÿง  Conceptual
advanced
1:30remaining
Which integer type can hold values from -32,768 to 32,767?

In Rust, which integer type can store values from -32,768 to 32,767?

Ai16
Bu16
Ci32
Du32
Attempts:
2 left
๐Ÿ’ก Hint

Think about signed 16-bit integers.

โ“ Predict Output
advanced
2:00remaining
What is the output of this Rust code using type inference and literals?

What does this Rust program print?

Rust
fn main() {
    let a = 1000;
    let b: u16 = 500;
    let c = a + b as i32;
    println!("{}", c);
}
A1000
BType mismatch error
C500
D1500
Attempts:
2 left
๐Ÿ’ก Hint

Check how Rust handles integer types and casting in expressions.

๐Ÿ”ง Debug
expert
2:30remaining
What error does this Rust code produce?

What error will this Rust code cause when compiled?

Rust
fn main() {
    let x: i8 = 128;
    println!("{}", x);
}
ALiteral out of range for i8
BType mismatch error
CRuntime panic
DNo error, prints 128
Attempts:
2 left
๐Ÿ’ก Hint

Check the valid range for i8 and what happens when you assign a literal outside that range.