Concept Flow - Scalar data types
Start
Declare variable with scalar type
Assign value
Use variable in code
Program runs with scalar data
End
This flow shows how scalar data types are declared, assigned, and used in Rust programs.
fn main() {
let x: i32 = 10;
let y: f64 = 3.14;
let z: bool = true;
println!("x = {}, y = {}, z = {}", x, y, z);
}| Step | Action | Variable | Type | Value | Output |
|---|---|---|---|---|---|
| 1 | Declare variable x | x | i32 | uninitialized | |
| 2 | Assign 10 to x | x | i32 | 10 | |
| 3 | Declare variable y | y | f64 | uninitialized | |
| 4 | Assign 3.14 to y | y | f64 | 3.14 | |
| 5 | Declare variable z | z | bool | uninitialized | |
| 6 | Assign true to z | z | bool | true | |
| 7 | Print values | x,y,z | i32,f64,bool | 10,3.14,true | x = 10, y = 3.14, z = true |
| Variable | Start | After Step 2 | After Step 4 | After Step 6 | Final |
|---|---|---|---|---|---|
| x | uninitialized | 10 | 10 | 10 | 10 |
| y | uninitialized | uninitialized | 3.14 | 3.14 | 3.14 |
| z | uninitialized | uninitialized | uninitialized | true | true |
Scalar data types hold single values like numbers or true/false. Common types: i32 (integer), f64 (float), bool (true/false). Declare with let and specify type or let Rust infer it. Assign values before use. Use println! to display values. Rust enforces type safety and initialization.
is_active set to true in Rust?true and false for booleans, and the type is bool.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.bool and value is lowercase true [OK]let x: i32 = 10;
let y: f64 = 3.5;
println!("{}", x as f64 + y);x is cast to f64 using as f64, so 10 becomes 10.0.10.0 (casted x) and 3.5 (y) results in 13.5.let letter: char = "a";
char literals must be enclosed in single quotes, e.g., 'a', not double quotes."a", which is a string slice, not a char. This causes a type mismatch error.Options: A) let temp: i32 = 36.6; B) let temp: char = '36'; C) let temp: bool = true; D) let temp: f64 = 36.6;
f64.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.Pass the test and this pattern moves unknown