Type inference is Rust's ability to automatically figure out the type of a variable based on the value assigned to it, so you don't always have to write the type explicitly.
Click to reveal answer
beginner
How does Rust infer the type of <code>let x = 5;</code>?
Rust sees the value 5 is an integer, so it infers that x is of type i32 by default.
Click to reveal answer
intermediate
Can Rust infer types for function return values without explicit annotations?
Yes, Rust can infer the return type if it can figure it out from the function body, but sometimes you need to specify it for clarity or when inference is not possible.
Click to reveal answer
intermediate
What happens if Rust cannot infer a variable's type?
Rust will give a compile-time error asking you to specify the type explicitly because it needs to know the exact type to ensure safety.
Click to reveal answer
beginner
Example: What type does Rust infer for <code>let name = "Alice";</code>?
Rust infers the type as &str, which is a string slice pointing to a string literal.
Click to reveal answer
What type does Rust infer for let num = 10; if no type is specified?
AString
Bf64
Ci32
Dbool
✗ Incorrect
Rust defaults integer literals to type i32 unless specified otherwise.
If Rust cannot infer a variable's type, what will happen?
AIt will throw a compile-time error.
BIt will guess the type randomly.
CIt will assign the type as 'any'.
DIt will run the program anyway.
✗ Incorrect
Rust requires explicit types if it cannot infer them and will not compile without them.
Which of these can Rust infer the type for automatically?
AFunction return type when it is clear.
BType of a variable declared but never assigned.
CType of a variable assigned from user input without parsing.
DType of a variable assigned from a function with multiple return types.
✗ Incorrect
Rust can infer return types when the function body clearly shows the type.
What type does Rust infer for let flag = true;?
Ai32
Bbool
CString
Dchar
✗ Incorrect
The value true is a boolean, so Rust infers the type bool.
Why is type inference helpful in Rust?
AIt removes the need for types completely.
BIt slows down the compiler.
CIt allows variables to change type at runtime.
DIt makes code shorter and easier to read.
✗ Incorrect
Type inference reduces the need to write types explicitly, making code cleaner and easier to write.
Explain what type inference is in Rust and give an example.
Think about how Rust figures out variable types without you writing them.
You got /3 concepts.
Describe a situation where Rust cannot infer a type and what you should do.
When Rust is unsure, it asks you to help by writing the type.
You got /3 concepts.
Practice
(1/5)
1. What does Rust's type inference do when you write let x = 5; without specifying a type?
easy
A. Rust treats x as a string by default.
B. Rust throws a syntax error because the type is missing.
C. Rust requires you to write the type explicitly for all variables.
D. Rust guesses the type of x as i32 based on the value 5.
Solution
Step 1: Understand Rust's default integer type
Rust uses i32 as the default integer type when no type is specified.
Step 2: Apply type inference to let x = 5;
Since 5 is an integer literal, Rust infers x to be i32 automatically.
Final Answer:
Rust guesses the type of x as i32 based on the value 5. -> Option D
Quick Check:
Type inference = i32 for integer literals [OK]
Hint: Integer literals default to i32 if no type given [OK]
Common Mistakes:
Thinking Rust requires explicit types always
Assuming default type is string
Believing missing types cause syntax errors
2. Which of the following is the correct syntax to let Rust infer the type of a floating-point number?
easy
A. let num = "3.14";
B. let num: f64 = 3.14;
C. let num = 3.14;
D. let num = 3,14;
Solution
Step 1: Identify valid floating-point syntax
In Rust, floating-point literals use a dot (.) as decimal separator, so 3.14 is valid.
Step 2: Check type inference usage
Writing let num = 3.14; lets Rust infer f64 type automatically.
Final Answer:
let num = 3.14; -> Option C
Quick Check:
Floating-point literals infer f64 by default [OK]
Hint: Use dot for decimals; omit type to infer f64 [OK]
Common Mistakes:
Using comma instead of dot for decimals
Confusing string literals with numbers
Unnecessarily specifying type when inference works
3. What is the output of this Rust code?
let x = 10;
let y = 20.5;
let z = x as f64 + y;
println!("{}", z);
medium
A. 30.5
B. 30
C. Compilation error due to type mismatch
D. 10.20.5
Solution
Step 1: Understand type casting
Variable x is i32, y is f64. Casting x as f64 converts it to floating-point.
Step 2: Calculate the sum
After casting, 10.0 + 20.5 = 30.5. The println! prints this value.
Final Answer:
30.5 -> Option A
Quick Check:
Cast int to float, then add = 30.5 [OK]
Hint: Cast integers to float before adding floats [OK]
Common Mistakes:
Expecting integer addition without cast
Assuming implicit cast without as
Confusing output format with concatenation
4. Identify the error in this Rust code snippet:
let a = 5;
let b = 2.0;
let c = a + b;
medium
A. Cannot add integer and floating-point without casting.
B. Variable a is not initialized.
C. Missing semicolon after let b = 2.0.
D. Rust does not support addition operator.
Solution
Step 1: Check variable types
a is i32 (integer), b is f64 (floating-point).
Step 2: Understand addition rules in Rust
Rust does not allow adding different numeric types directly; explicit casting is needed.
Final Answer:
Cannot add integer and floating-point without casting. -> Option A
Quick Check:
Mixed-type addition requires cast [OK]
Hint: Add only same types or cast explicitly [OK]
Common Mistakes:
Ignoring type mismatch errors
Assuming implicit conversion between int and float
Overlooking Rust's strict type rules
5. Given this Rust code, what is the type of result?
let values = vec![1, 2, 3];
let result: Vec<f64> = values.iter().map(|&x| x as f64 * 1.5).collect();
hard
A. Vec<i32>
B. Vec<f64>
C. Vec<&i32>
D. Compilation error due to missing type annotation
Solution
Step 1: Analyze the iterator and map closure
values.iter() produces references &i32. The closure |&x| x as f64 * 1.5 dereferences and casts x to f64, then multiplies.
Step 2: Collect into specified type
The collect() call collects results into Vec<f64> as explicitly annotated.
Final Answer:
Vec<f64> -> Option B
Quick Check:
Mapping with cast and collecting matches annotated type [OK]
Hint: Check closure cast and collect type annotation [OK]
Common Mistakes:
Confusing iterator item types with collected vector type
Missing dereference in closure causing type errors
Assuming collect infers wrong type without annotation
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.