Challenge - 5 Problems
Rust Scalar Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ Predict Output
intermediate2:00remaining
What is the output of this Rust code with scalar types?
Consider the following Rust code snippet. What will it print when run?
Rust
fn main() {
let x: i32 = 10;
let y: f64 = 3.5;
let z: bool = (x as f64) > y;
println!("{}", z);
}Attempts:
2 left
๐ก Hint
Remember that casting i32 to f64 allows comparison between x and y.
โ Incorrect
The integer 10 is cast to 10.0 as f64, which is greater than 3.5, so z is true.
โ Predict Output
intermediate2:00remaining
What value does this Rust code assign to variable `c`?
Given the code below, what is the value of `c` after execution?
Rust
fn main() {
let a: u8 = 255;
let b: u8 = 1;
let c = a.wrapping_add(b);
println!("{}", c);
}Attempts:
2 left
๐ก Hint
u8 can only hold values from 0 to 255. Wrapping addition wraps around on overflow.
โ Incorrect
Adding 255 + 1 overflows u8 and wraps to 0 using wrapping_add.
๐ง Debug
advanced2:00remaining
What error does this Rust code produce?
Analyze the code and select the error it will cause when compiled.
Rust
fn main() {
let x: i32 = 5.0;
println!("{}", x);
}Attempts:
2 left
๐ก Hint
Check the type assigned to x and the value given.
โ Incorrect
You cannot assign a floating-point value (5.0) to an integer variable (i32) without explicit casting.
๐ Syntax
advanced2:00remaining
Which option correctly declares a character scalar in Rust?
Select the correct Rust code that declares a variable `ch` as a character with value 'R'.
Attempts:
2 left
๐ก Hint
Characters use single quotes and the type is `char`.
โ Incorrect
Option C uses the correct type `char` and single quotes for a character literal.
๐ Application
expert2:00remaining
How many bytes does this Rust scalar type occupy?
In Rust, how many bytes does the scalar type `f64` occupy in memory?
Attempts:
2 left
๐ก Hint
f64 is a 64-bit floating point number.
โ Incorrect
f64 is a 64-bit (8 bytes) floating point scalar type in Rust, fixed size regardless of architecture.