0
0
Rustprogramming~20 mins

Scalar data types in Rust - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Rust Scalar 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 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);
}
Afalse
Btrue
C3.5
DCompilation error
Attempts:
2 left
๐Ÿ’ก Hint
Remember that casting i32 to f64 allows comparison between x and y.
โ“ Predict Output
intermediate
2: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);
}
A0
B256
C1
DCompilation error
Attempts:
2 left
๐Ÿ’ก Hint
u8 can only hold values from 0 to 255. Wrapping addition wraps around on overflow.
๐Ÿ”ง Debug
advanced
2: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);
}
ANo error, prints 5
BSyntax error
CRuntime panic
DType mismatch error
Attempts:
2 left
๐Ÿ’ก Hint
Check the type assigned to x and the value given.
๐Ÿ“ Syntax
advanced
2: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'.
Alet ch = 'R';
Blet ch: char = "R";
Clet ch: char = 'R';
Dlet ch: character = 'R';
Attempts:
2 left
๐Ÿ’ก Hint
Characters use single quotes and the type is `char`.
๐Ÿš€ Application
expert
2:00remaining
How many bytes does this Rust scalar type occupy?
In Rust, how many bytes does the scalar type `f64` occupy in memory?
A8 bytes
B16 bytes
CDepends on the system architecture
D4 bytes
Attempts:
2 left
๐Ÿ’ก Hint
f64 is a 64-bit floating point number.