Primitive Types
Level: intro (score: 1)
π― In Python, you don’t declare variable types — the interpreter figures it out at runtime.
Rust also infers types, but it’s still statically typed, meaning types are checked at compile time.
This means:
- You can omit type annotations if the compiler can figure it out.
- If not, you must explicitly specify the type.
β Your task:
Implement the describe_types
function:
- Create variables for:
- an integer (
i32
) - a floating point number (
f64
) - a boolean
- a character
- a tuple of two values (integer and string)
- an integer (
- Use type inference where possible.
- Use the
format!
macro to produce aString
in the format:
int: 42, float: 3.14, bool: true, char: Z, tuple: (7, "Rust")
π§ In Rust, if the last expression in a function doesn’t end with a semicolon, it becomes the return value — no return
keyword needed. This is why the format!(...)
call here directly returns the String.
This exercise introduces:
- Primitive types in Rust (
i32
,f64
,bool
,char
, tuples) - Type inference vs explicit types
format!
macro for string building
π§ Hint: If Rust complains it “cannot infer type”, explicitly add : i32
or similar after your variable name.