What are the two main floating point types in Rust?
The two main floating point types in Rust are f32 (32-bit float) and f64 (64-bit float).
Click to reveal answer
beginner
Why might you choose f64 over f32 in Rust?
f64 provides more precision and is the default floating point type in Rust, making it better for calculations needing higher accuracy.
Click to reveal answer
beginner
How do you declare a floating point variable with a specific type in Rust?
You declare it by specifying the type after the value, like <code>let x: f32 = 3.14;</code> or <code>let y: f64 = 2.718;</code>.
Click to reveal answer
beginner
What is the default floating point type in Rust if you don't specify one?
The default floating point type in Rust is f64.
Click to reveal answer
beginner
What is a real-life example where floating point types are useful?
Floating point types are useful for measurements like temperature or scientific data where decimals are needed. For currency calculations, it's better to use integer types or specialized libraries to avoid precision errors.
Click to reveal answer
Which floating point type has more precision in Rust?
Abool
Bf32
Ci32
Df64
✗ Incorrect
f64 has 64 bits and more precision than f32, which has 32 bits.
What is the default floating point type in Rust when you write let x = 1.0;?
Af64
Bf32
Ci32
Dusize
✗ Incorrect
Rust uses f64 as the default floating point type for decimal numbers.
How do you explicitly declare a 32-bit floating point variable in Rust?
Alet x = 3.14i32;
Blet x: f64 = 3.14;
Clet x = 3.14f32;
Dlet x = 3.14f64;
✗ Incorrect
Adding f32 after the number or specifying : f32 declares a 32-bit float.
Which of these is NOT a floating point type in Rust?
Af128
Bf64
Ci64
Df32
✗ Incorrect
f128 is not a standard floating point type in Rust; Rust supports f32 and f64.
Why are floating point types important in programming?
ATo store whole numbers only
BTo store decimal numbers with fractions
CTo store text data
DTo store true or false values
✗ Incorrect
Floating point types store decimal numbers that include fractions, unlike integers.
Explain the difference between f32 and f64 in Rust and when you might use each.
Think about how much detail you need in your decimal numbers.
You got /4 concepts.
Describe how to declare a floating point variable in Rust and why specifying the type might be important.
Consider what happens if you don’t specify the type.
You got /4 concepts.
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
Step 1: Understand Rust's default floating point type
Rust uses f64 as the default floating point type for decimal numbers for better precision.
Step 2: Compare with other types
f32 is less precise and not the default; float and double are not Rust types.
Final Answer:
f64 -> Option D
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
Step 1: Check Rust syntax for f32 declaration
Declaring a variable with type f32 uses let num: f32 = 5.5f32;.
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.
Final Answer:
let num: f32 = 5.5f32; -> Option B
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
Step 1: Understand type casting in the expression
Variable y is f32, but it is cast to f64 with y as f64 before addition.
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.
Final Answer:
3.0 -> Option A
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
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.
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.
Final Answer:
Cannot add f32 and f64 without casting -> Option A
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
Step 1: Choose the highest precision floating point type
Rust's f64 provides higher precision than f32, so declaring avg as f64 is best.
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.
Final Answer:
let avg: f64 = (4.2 + 5.8 + 6.0) / 3.0; -> Option C
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 unknownstrongwhile earning +1 Grip toward your certificate.