Primitive Types
Level: intro (score: 1)
🚀 These intro exercises are prep for our
Rust Intro Cohort Program.
🎯 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.
pub fn describe_types() -> String {
// TODO:
// - Declare an int, float, bool, char, and a tuple (int, &str) using type inference where possible.
// - Use the `format!` macro to produce a String in the format:
// int: 42, float: 3.14, bool: true, char: Z, tuple: (7, "Rust")
// - Remember: if the last expression in a function doesn’t end with a semicolon,
// it becomes the return value — no `return` keyword needed.
format!(
// fill in variables here
""
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_describe_types() {
let output = describe_types();
assert!(output.contains("int: 42"));
assert!(output.contains("float: 3.14"));
assert!(output.contains("bool: true"));
assert!(output.contains("char: Z"));
assert!(output.contains("tuple: (7, \"Rust\")"));
}
}