Bird
Raised Fist0
Rustprogramming~10 mins

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

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

Practice

(1/5)
1. Which floating point type is the default in Rust when you write a decimal number like 3.14?
easy
A. double
B. f32
C. float
D. f64

Solution

  1. Step 1: Understand Rust's default floating point type

    Rust uses f64 as the default floating point type for decimal numbers for better precision.
  2. Step 2: Compare with other types

    f32 is less precise and not the default; float and double are not Rust types.
  3. Final Answer:

    f64 -> Option D
  4. Quick Check:

    Default floating point type = f64 [OK]
Hint: Remember: Rust defaults to f64 for decimals [OK]
Common Mistakes:
  • Confusing f32 as default
  • Using non-Rust types like float or double
  • Assuming integers are default for decimals
2. Which of the following is the correct way to declare a 32-bit floating point variable named num with value 5.5 in Rust?
easy
A. let num = 5.5 f32;
B. let num: f32 = 5.5f32;
C. let num: float = 5.5;
D. let num = 5.5f64;

Solution

  1. Step 1: Check Rust syntax for f32 declaration

    Declaring a variable with type f32 uses let num: f32 = 5.5f32;.
  2. Step 2: Validate other options

    let num = 5.5 f32; is invalid syntax (should be 5.5f32 without space). let num: float = 5.5; uses float which is not a Rust type. let num = 5.5f64; declares f64, not f32.
  3. Final Answer:

    let num: f32 = 5.5f32; -> Option B
  4. Quick Check:

    Correct f32 declaration = let num: f32 = 5.5f32; [OK]
Hint: Use let var: f32 = value; for 32-bit floats [OK]
Common Mistakes:
  • Using non-existent type 'float'
  • Mixing f32 and f64 suffixes incorrectly
  • Omitting type when specific precision is needed
3. What will be the output of this Rust code?
let x = 1.0; // f64 by default
let y: f32 = 2.0f32;
println!("{}", x + y as f64);
medium
A. 3.0
B. 3
C. Error: mismatched types
D. 3.00

Solution

  1. Step 1: Understand type casting in the expression

    Variable y is f32, but it is cast to f64 with y as f64 before addition.
  2. Step 2: Calculate the sum and output format

    Adding 1.0 (f64) and 2.0 (f64) results in 3.0. The println! macro prints it as 3.0 when using {} with floating point numbers.
  3. Final Answer:

    3.0 -> Option A
  4. Quick Check:

    println!("{}", 1.0 + 2.0f32 as f64) = 3.0 [OK]
Hint: Cast f32 to f64 before adding to avoid errors [OK]
Common Mistakes:
  • Not casting f32 to f64 causing type errors
  • Expecting integer output instead of float
  • Confusing output format with extra zeros
4. Identify the error in this Rust code snippet:
let a: f32 = 3.5f32;
let b: f64 = 4.5;
let c = a + b;
medium
A. Cannot add f32 and f64 without casting
B. Variable c is not declared with a type
C. f32 cannot hold decimal values
D. Missing semicolon after let b

Solution

  1. Step 1: Check types of variables a and b

    Variable a is f32, and b is f64. Rust does not allow direct arithmetic between different float types.
  2. Step 2: Identify the error in addition

    Adding a + b causes a type mismatch error. One must cast either a or b to the other's type before adding.
  3. Final Answer:

    Cannot add f32 and f64 without casting -> Option A
  4. Quick Check:

    Mixing f32 and f64 needs casting [OK]
Hint: Always cast floats to same type before arithmetic [OK]
Common Mistakes:
  • Ignoring type mismatch between f32 and f64
  • Assuming implicit conversion happens
  • Thinking missing semicolon causes error here
5. You want to store the average of three numbers: 4.2, 5.8, and 6.0 in Rust with the highest precision. Which code snippet correctly does this?
hard
A. let avg = (4.2f32 + 5.8f32 + 6.0f32) / 3.0f32;
B. let avg = (4.2 + 5.8 + 6.0) / 3f32;
C. let avg: f64 = (4.2 + 5.8 + 6.0) / 3.0;
D. let avg: f32 = (4.2 + 5.8 + 6.0) / 3.0;

Solution

  1. Step 1: Choose the highest precision floating point type

    Rust's f64 provides higher precision than f32, so declaring avg as f64 is best.
  2. Step 2: Check the expression and types

    Using decimal literals without suffix defaults to f64. Dividing by 3.0 (also f64) keeps precision. let avg: f64 = (4.2 + 5.8 + 6.0) / 3.0; correctly declares avg: f64 and uses f64 literals.
  3. Final Answer:

    let avg: f64 = (4.2 + 5.8 + 6.0) / 3.0; -> Option C
  4. Quick Check:

    Use f64 for highest precision average [OK]
Hint: Use f64 and decimal literals without suffix for precision [OK]
Common Mistakes:
  • Using f32 when higher precision is needed
  • Dividing by integer 3 causing integer division
  • Mixing types without explicit declaration
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