Scalar data types hold a single value at a time. They help us store simple pieces of information like numbers or characters.
Scalar data types in Rust
let variable_name: data_type = value;
Use let to create a variable.
Specify the data type after a colon : (optional, Rust can infer it).
x with value 5.let x: i32 = 5;y with value 3.14.let y: f64 = 3.14;is_active set to true.let is_active: bool = true;letter holding the letter 'A'.let letter: char = 'A';
This program creates four scalar variables: an unsigned 8-bit integer for age, a 32-bit float for temperature, a boolean for rain status, and a character for grade. It then prints each value.
fn main() {
let age: u8 = 30;
let temperature: f32 = 36.6;
let is_raining: bool = false;
let grade: char = 'B';
println!("Age: {}", age);
println!("Temperature: {}", temperature);
println!("Is it raining? {}", is_raining);
println!("Grade: {}", grade);
}Rust has several integer types like i8, i16, i32, i64, and unsigned versions like u8.
Floating-point types are f32 and f64, with f64 being more precise.
Boolean values can only be true or false.
Characters in Rust are 4 bytes and can hold Unicode symbols.
Scalar types store one simple value at a time.
Common scalar types are integers, floats, booleans, and characters.
Use scalar types to represent basic data like numbers, true/false, or single letters.
Practice
Solution
Step 1: Recall scalar data types in Rust
Scalar types include integers, floats, booleans, and characters, which hold a single value.Step 2: Identify the non-scalar type
An array holds multiple values and is a compound type, not scalar.Final Answer:
Array -> Option CQuick Check:
Scalar types exclude arrays [OK]
- Confusing arrays with scalar types
- Thinking floats are not scalar
- Mixing compound and scalar types
is_active set to true in Rust?Solution
Step 1: Check Rust boolean syntax
Rust uses lowercasetrueandfalsefor booleans, and the type isbool.Step 2: Validate each option
let is_active: bool = true; correctly declaresis_activeas aboolwith valuetrue. let is_active = True; uses uppercaseTruewhich is invalid. let is_active = 1; assigns an integer, not a boolean. let is_active: boolean = true; usesbooleanwhich is not a Rust type.Final Answer:
let is_active: bool = true; -> Option AQuick Check:
Boolean type isbooland value is lowercasetrue[OK]
- Using uppercase True/False
- Using wrong type name boolean
- Assigning integers to booleans
let x: i32 = 10;
let y: f64 = 3.5;
println!("{}", x as f64 + y);Solution
Step 1: Understand type casting in the code
The integerxis cast tof64usingas f64, so10becomes10.0.Step 2: Perform the addition
Adding10.0(castedx) and3.5(y) results in13.5.Final Answer:
13.5 -> Option BQuick Check:
Cast integer to float, then add floats = 13.5 [OK]
- Forgetting to cast integer
- Expecting integer addition
- Assuming type error without cast
let letter: char = "a";
Solution
Step 1: Check char literal syntax
In Rust,charliterals must be enclosed in single quotes, e.g.,'a', not double quotes.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.Final Answer:
char type must use single quotes, not double quotes -> Option DQuick Check:
char literals use single quotes 'a' [OK]
- Using double quotes for char
- Confusing char with string
- Ignoring type mismatch errors
Options: A) let temp: i32 = 36.6; B) let temp: char = '36'; C) let temp: bool = true; D) let temp: f64 = 36.6;
Solution
Step 1: Understand the data type needed for decimal numbers
Temperature 36.6 has a decimal part, so it requires a floating-point type likef64.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 usesf64for decimals. let temp: bool = true; because temperature is true or false uses boolean, which is unrelated. let temp: char = '36'; because characters store numbers usescharwith invalid multi-character literal.Final Answer:
let temp: f64 = 36.6; because floats store decimal numbers -> Option AQuick Check:
Decimal numbers need float types like f64 [OK]
- Assigning decimals to integers
- Using bool for numeric values
- Using char for multi-digit numbers
Ready to prove it?
Pass the test and this pattern moves unknown
