Bird
Raised Fist0
Rustprogramming~20 mins

Scalar data types in Rust - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. Which of the following is NOT a scalar data type in Rust?
easy
A. Integer
B. Boolean
C. Array
D. Character

Solution

  1. Step 1: Recall scalar data types in Rust

    Scalar types include integers, floats, booleans, and characters, which hold a single value.
  2. Step 2: Identify the non-scalar type

    An array holds multiple values and is a compound type, not scalar.
  3. Final Answer:

    Array -> Option C
  4. Quick Check:

    Scalar types exclude arrays [OK]
Hint: Scalar types hold one value; arrays hold many [OK]
Common Mistakes:
  • Confusing arrays with scalar types
  • Thinking floats are not scalar
  • Mixing compound and scalar types
2. Which of the following is the correct way to declare a boolean variable named is_active set to true in Rust?
easy
A. let is_active: bool = true;
B. let is_active = True;
C. let is_active = 1;
D. let is_active: boolean = true;

Solution

  1. Step 1: Check Rust boolean syntax

    Rust uses lowercase true and false for booleans, and the type is bool.
  2. Step 2: Validate each option

    let is_active: bool = true; correctly declares is_active as a bool with value true. let is_active = True; uses uppercase True which is invalid. let is_active = 1; assigns an integer, not a boolean. let is_active: boolean = true; uses boolean which is not a Rust type.
  3. Final Answer:

    let is_active: bool = true; -> Option A
  4. Quick Check:

    Boolean type is bool and value is lowercase true [OK]
Hint: Booleans use lowercase true/false and type bool [OK]
Common Mistakes:
  • Using uppercase True/False
  • Using wrong type name boolean
  • Assigning integers to booleans
3. What will be the output of the following Rust code?
let x: i32 = 10;
let y: f64 = 3.5;
println!("{}", x as f64 + y);
medium
A. 13
B. 13.5
C. Type error
D. 10.0

Solution

  1. Step 1: Understand type casting in the code

    The integer x is cast to f64 using as f64, so 10 becomes 10.0.
  2. Step 2: Perform the addition

    Adding 10.0 (casted x) and 3.5 (y) results in 13.5.
  3. Final Answer:

    13.5 -> Option B
  4. Quick Check:

    Cast integer to float, then add floats = 13.5 [OK]
Hint: Cast integers to float before adding floats [OK]
Common Mistakes:
  • Forgetting to cast integer
  • Expecting integer addition
  • Assuming type error without cast
4. Identify the error in the following Rust code snippet:
let letter: char = "a";
medium
A. Missing semicolon
B. char cannot be assigned a letter
C. Variable name is invalid
D. char type must use single quotes, not double quotes

Solution

  1. Step 1: Check char literal syntax

    In Rust, char literals must be enclosed in single quotes, e.g., 'a', not double quotes.
  2. Step 2: Analyze the code

    The code uses double quotes "a", which is a string slice, not a char. This causes a type mismatch error.
  3. Final Answer:

    char type must use single quotes, not double quotes -> Option D
  4. Quick Check:

    char literals use single quotes 'a' [OK]
Hint: Use single quotes for char, double quotes for strings [OK]
Common Mistakes:
  • Using double quotes for char
  • Confusing char with string
  • Ignoring type mismatch errors
5. You want to store the temperature reading 36.6 in Rust using scalar types. Which declaration is best and why?
Options:
A) let temp: i32 = 36.6;
B) let temp: char = '36';
C) let temp: bool = true;
D) let temp: f64 = 36.6;
hard
A. let temp: f64 = 36.6; because floats store decimal numbers
B. let temp: char = '36'; because characters store numbers
C. let temp: bool = true; because temperature is true or false
D. let temp: i32 = 36.6; because integers store decimal numbers

Solution

  1. Step 1: Understand the data type needed for decimal numbers

    Temperature 36.6 has a decimal part, so it requires a floating-point type like f64.
  2. Step 2: Evaluate each option

    let temp: i32 = 36.6; because integers store decimal numbers tries to assign a decimal to an integer, which is invalid. let temp: f64 = 36.6; because floats store decimal numbers correctly uses f64 for decimals. let temp: bool = true; because temperature is true or false uses boolean, which is unrelated. let temp: char = '36'; because characters store numbers uses char with invalid multi-character literal.
  3. Final Answer:

    let temp: f64 = 36.6; because floats store decimal numbers -> Option A
  4. Quick Check:

    Decimal numbers need float types like f64 [OK]
Hint: Use float types for decimal numbers like temperature [OK]
Common Mistakes:
  • Assigning decimals to integers
  • Using bool for numeric values
  • Using char for multi-digit numbers
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard