0
0
Rustprogramming~10 mins

Floating point types in Rust - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Floating point types
Start
Declare float variable
Assign value (e.g., 3.14)
Use variable in calculations
Print or return value
End
This flow shows how a floating point variable is declared, assigned a value, used in calculations, and then output.
Execution Sample
Rust
fn main() {
    let x: f32 = 3.14;
    let y: f64 = 2.718;
    println!("x = {}, y = {}", x, y);
}
This Rust code declares two floating point variables of types f32 and f64, assigns values, and prints them.
Execution Table
StepActionVariableValueOutput
1Declare x as f32xuninitialized
2Assign 3.14 to xx3.14
3Declare y as f64yuninitialized
4Assign 2.718 to yy2.718
5Print valuesx, y3.14, 2.718x = 3.14, y = 2.718
6Program ends
💡 Program ends after printing the floating point values.
Variable Tracker
VariableStartAfter Step 2After Step 4Final
xuninitialized3.143.143.14
yuninitializeduninitialized2.7182.718
Key Moments - 3 Insights
Why do we specify f32 or f64 when declaring floating point variables?
Rust requires specifying the floating point type to know how much memory to allocate and the precision. See steps 1 and 3 in the execution_table where types are declared.
What happens if we don't assign a value after declaring a variable?
The variable remains uninitialized and cannot be used. In the table, after declaration (steps 1 and 3), values are 'uninitialized' until assigned (steps 2 and 4).
Why are the printed values exactly 3.14 and 2.718?
Because the variables hold those assigned floating point numbers. Step 5 shows the output matching the variable values.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what is the value of variable y?
Auninitialized
B2.718
C3.14
D0.0
💡 Hint
Check the 'Value' column for variable y at step 4 in the execution_table.
At which step does the program print the floating point values?
AStep 2
BStep 3
CStep 5
DStep 6
💡 Hint
Look for the 'Print values' action in the execution_table.
If we remove the type f64 from y, what would happen?
Ay will default to f64
By will default to f32
CThe program will not compile because type is missing
DThe program will print 0.0 for y
💡 Hint
Rust defaults floating point literals to f64 if no type is specified.
Concept Snapshot
Floating point types in Rust:
- Use f32 or f64 to declare floats
- f32 uses 4 bytes, f64 uses 8 bytes
- Default float type is f64
- Assign values with decimal points
- Use println! to display values
Full Transcript
This visual execution shows how floating point types work in Rust. First, variables x and y are declared with types f32 and f64. Initially, they are uninitialized. Then, values 3.14 and 2.718 are assigned. Finally, the program prints these values. The execution table tracks each step, showing variable states and output. Key moments clarify why types are needed and what happens if variables are uninitialized. The quiz tests understanding of variable values at steps and Rust's default float type. This helps beginners see how floating point variables are created, assigned, and used in Rust.