0
0
Rustprogramming~10 mins

Generic functions in Rust - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Generic functions
Define generic function with <T>
Call function with specific type
Compiler replaces T with actual type
Function runs with concrete type
Return result or perform action
This flow shows how a generic function is defined with a placeholder type T, then called with a real type, which the compiler uses to create a specific version of the function.
Execution Sample
Rust
fn print_value<T: std::fmt::Display>(value: T) {
    println!("Value: {}", value);
}

fn main() {
    print_value(42);
    print_value("hello");
}
This code defines a generic function that prints any value that can be displayed, then calls it with an integer and a string.
Execution Table
StepActionGeneric Type TConcrete TypeOutput
1Define function print_value<T>TN/ANo output
2Call print_value(42)Ti32Value: 42
3Call print_value("hello")T&strValue: hello
4End of callsN/AN/AExecution stops
💡 All calls completed, no more function calls
Variable Tracker
VariableStartAfter call 1After call 2Final
valueN/A42 (i32)"hello" (&str)N/A
Key Moments - 3 Insights
Why do we write <T> after the function name?
The <T> tells Rust this function uses a generic type placeholder T, which will be replaced by a real type when the function is called, as shown in execution_table rows 2 and 3.
How does Rust know what type T is when calling the function?
Rust infers T from the argument passed to the function call, like 42 is i32 and "hello" is &str, as seen in execution_table rows 2 and 3.
Can the generic function print_value accept any type?
No, only types that implement the Display trait, so they can be printed. This is why we write <T: std::fmt::Display> in the function definition.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the concrete type of T when print_value is called with 42?
A&str
BString
Ci32
Df64
💡 Hint
Check execution_table row 2 under Concrete Type column
At which step does the function print_value get called with a string?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look at execution_table rows and find where Output is 'Value: hello'
If we remove the Display trait bound from <T: std::fmt::Display>, what would happen?
ACompiler error because println! needs Display
BCode compiles and runs fine
CFunction runs but prints nothing
DFunction only works for integers
💡 Hint
Recall key_moments about trait bounds and printing requirements
Concept Snapshot
Generic functions use <T> to accept any type.
The compiler replaces T with the actual type at call time.
Trait bounds like <T: Display> restrict usable types.
This allows code reuse with different data types.
Calls with different types create specialized versions.
Full Transcript
This lesson shows how generic functions work in Rust. We define a function with a placeholder type T using angle brackets. When we call the function with a real value, Rust replaces T with that value's type. For example, calling print_value(42) uses i32 for T, and print_value("hello") uses &str. The function requires T to implement the Display trait so it can print the value. The execution table traces each step: defining the function, calling it with different types, and the output produced. Variable tracking shows how the value variable changes type per call. Key moments clarify why we use <T>, how Rust infers types, and why trait bounds matter. The quiz tests understanding of type substitution, call steps, and trait bounds. This helps beginners see how generic functions enable flexible, reusable code.